From ece7cb06caefa5fff74198d8649806c4678c61a1 Mon Sep 17 00:00:00 2001 From: Priya Gupta <147705955+priyagupta108@users.noreply.github.com> Date: Tue, 23 Jun 2026 23:31:57 +0530 Subject: [PATCH] Fix pip cache error handling on Windows. (#1040) * Add error handling for Windows 'pip cache dir' execution * Add comments --- dist/setup/index.js | 23 ++++++++++++++--------- src/cache-distributions/pip-cache.ts | 22 +++++++++++++--------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/dist/setup/index.js b/dist/setup/index.js index ce067506..6ac18cd5 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -54331,24 +54331,29 @@ class PipCache extends cache_distributor_1.default { this.pythonVersion = pythonVersion; } async getCacheGlobalDirectories() { - let exitCode = 1; + let exitCode = 0; let stdout = ''; let stderr = ''; // Add temporary fix for Windows - // On windows it is necessary to execute through an exec - // because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2, + // On Windows, it is necessary to execute through an exec + // because the getExecOutput gives a non-zero code or writes to stderr for pip 22.0.2, // or spawn must be started with the shell option enabled for getExecOutput // Related issue: https://github.com/actions/setup-python/issues/328 if (utils_1.IS_WINDOWS) { const execPromisify = util_1.default.promisify(child_process.exec); - ({ stdout: stdout, stderr: stderr } = await execPromisify('pip cache dir')); + try { + ({ stdout, stderr } = await execPromisify('pip cache dir')); + } + catch (err) { + // Pip outputs warnings to stderr (e.g., --no-python-version-warning flag deprecation warning), causing false failure detection + // Related issue: https://github.com/actions/setup-python/issues/1034 + // If an error occurs, capture stderr and set exitCode to 1 to indicate failure + stderr = err.stderr ?? err.message; + exitCode = 1; + } } else { - ({ - stdout: stdout, - stderr: stderr, - exitCode: exitCode - } = await exec.getExecOutput('pip cache dir')); + ({ stdout, stderr, exitCode } = await exec.getExecOutput('pip cache dir')); } if (exitCode && stderr) { throw new Error(`Could not get cache folder path for pip package manager`); diff --git a/src/cache-distributions/pip-cache.ts b/src/cache-distributions/pip-cache.ts index 012a5622..1006a950 100644 --- a/src/cache-distributions/pip-cache.ts +++ b/src/cache-distributions/pip-cache.ts @@ -21,24 +21,28 @@ class PipCache extends CacheDistributor { } protected async getCacheGlobalDirectories() { - let exitCode = 1; + let exitCode = 0; let stdout = ''; let stderr = ''; // Add temporary fix for Windows - // On windows it is necessary to execute through an exec - // because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2, + // On Windows, it is necessary to execute through an exec + // because the getExecOutput gives a non-zero code or writes to stderr for pip 22.0.2, // or spawn must be started with the shell option enabled for getExecOutput // Related issue: https://github.com/actions/setup-python/issues/328 if (IS_WINDOWS) { const execPromisify = utils.promisify(child_process.exec); - ({stdout: stdout, stderr: stderr} = await execPromisify('pip cache dir')); + try { + ({stdout, stderr} = await execPromisify('pip cache dir')); + } catch (err) { + // Pip outputs warnings to stderr (e.g., --no-python-version-warning flag deprecation warning), causing false failure detection + // Related issue: https://github.com/actions/setup-python/issues/1034 + // If an error occurs, capture stderr and set exitCode to 1 to indicate failure + stderr = (err as any).stderr ?? (err as Error).message; + exitCode = 1; + } } else { - ({ - stdout: stdout, - stderr: stderr, - exitCode: exitCode - } = await exec.getExecOutput('pip cache dir')); + ({stdout, stderr, exitCode} = await exec.getExecOutput('pip cache dir')); } if (exitCode && stderr) {