Skip to content

Commit ece7cb0

Browse files
Fix pip cache error handling on Windows. (#1040)
* Add error handling for Windows 'pip cache dir' execution * Add comments
1 parent 1d18d7a commit ece7cb0

2 files changed

Lines changed: 27 additions & 18 deletions

File tree

dist/setup/index.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54331,24 +54331,29 @@ class PipCache extends cache_distributor_1.default {
5433154331
this.pythonVersion = pythonVersion;
5433254332
}
5433354333
async getCacheGlobalDirectories() {
54334-
let exitCode = 1;
54334+
let exitCode = 0;
5433554335
let stdout = '';
5433654336
let stderr = '';
5433754337
// Add temporary fix for Windows
54338-
// On windows it is necessary to execute through an exec
54339-
// because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2,
54338+
// On Windows, it is necessary to execute through an exec
54339+
// because the getExecOutput gives a non-zero code or writes to stderr for pip 22.0.2,
5434054340
// or spawn must be started with the shell option enabled for getExecOutput
5434154341
// Related issue: https://github.com/actions/setup-python/issues/328
5434254342
if (utils_1.IS_WINDOWS) {
5434354343
const execPromisify = util_1.default.promisify(child_process.exec);
54344-
({ stdout: stdout, stderr: stderr } = await execPromisify('pip cache dir'));
54344+
try {
54345+
({ stdout, stderr } = await execPromisify('pip cache dir'));
54346+
}
54347+
catch (err) {
54348+
// Pip outputs warnings to stderr (e.g., --no-python-version-warning flag deprecation warning), causing false failure detection
54349+
// Related issue: https://github.com/actions/setup-python/issues/1034
54350+
// If an error occurs, capture stderr and set exitCode to 1 to indicate failure
54351+
stderr = err.stderr ?? err.message;
54352+
exitCode = 1;
54353+
}
5434554354
}
5434654355
else {
54347-
({
54348-
stdout: stdout,
54349-
stderr: stderr,
54350-
exitCode: exitCode
54351-
} = await exec.getExecOutput('pip cache dir'));
54356+
({ stdout, stderr, exitCode } = await exec.getExecOutput('pip cache dir'));
5435254357
}
5435354358
if (exitCode && stderr) {
5435454359
throw new Error(`Could not get cache folder path for pip package manager`);

src/cache-distributions/pip-cache.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,28 @@ class PipCache extends CacheDistributor {
2121
}
2222

2323
protected async getCacheGlobalDirectories() {
24-
let exitCode = 1;
24+
let exitCode = 0;
2525
let stdout = '';
2626
let stderr = '';
2727

2828
// Add temporary fix for Windows
29-
// On windows it is necessary to execute through an exec
30-
// because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2,
29+
// On Windows, it is necessary to execute through an exec
30+
// because the getExecOutput gives a non-zero code or writes to stderr for pip 22.0.2,
3131
// or spawn must be started with the shell option enabled for getExecOutput
3232
// Related issue: https://github.com/actions/setup-python/issues/328
3333
if (IS_WINDOWS) {
3434
const execPromisify = utils.promisify(child_process.exec);
35-
({stdout: stdout, stderr: stderr} = await execPromisify('pip cache dir'));
35+
try {
36+
({stdout, stderr} = await execPromisify('pip cache dir'));
37+
} catch (err) {
38+
// Pip outputs warnings to stderr (e.g., --no-python-version-warning flag deprecation warning), causing false failure detection
39+
// Related issue: https://github.com/actions/setup-python/issues/1034
40+
// If an error occurs, capture stderr and set exitCode to 1 to indicate failure
41+
stderr = (err as any).stderr ?? (err as Error).message;
42+
exitCode = 1;
43+
}
3644
} else {
37-
({
38-
stdout: stdout,
39-
stderr: stderr,
40-
exitCode: exitCode
41-
} = await exec.getExecOutput('pip cache dir'));
45+
({stdout, stderr, exitCode} = await exec.getExecOutput('pip cache dir'));
4246
}
4347

4448
if (exitCode && stderr) {

0 commit comments

Comments
 (0)