Skip to content

Commit 7f80679

Browse files
Add fix for Windows caching of pip (#332)
1 parent dc9de69 commit 7f80679

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

__tests__/cache-restore.test.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ describe('restore-cache', () => {
9696
expect(infoSpy).toHaveBeenCalledWith(
9797
`Cache restored from key: setup-python-${process.env['RUNNER_OS']}-python-${pythonVersion}-${packageManager}-${fileHash}`
9898
);
99-
}
99+
},
100+
30000
100101
);
101102

102103
it.each([

dist/setup/index.js

+22-1
Original file line numberDiff line numberDiff line change
@@ -34463,17 +34463,38 @@ Object.defineProperty(exports, "__esModule", { value: true });
3446334463
const glob = __importStar(__webpack_require__(281));
3446434464
const core = __importStar(__webpack_require__(470));
3446534465
const exec = __importStar(__webpack_require__(986));
34466+
const child_process = __importStar(__webpack_require__(129));
34467+
const util_1 = __importDefault(__webpack_require__(669));
3446634468
const path = __importStar(__webpack_require__(622));
3446734469
const os_1 = __importDefault(__webpack_require__(87));
3446834470
const cache_distributor_1 = __importDefault(__webpack_require__(435));
34471+
const utils_1 = __webpack_require__(163);
3446934472
class PipCache extends cache_distributor_1.default {
3447034473
constructor(pythonVersion, cacheDependencyPath = '**/requirements.txt') {
3447134474
super('pip', cacheDependencyPath);
3447234475
this.pythonVersion = pythonVersion;
3447334476
}
3447434477
getCacheGlobalDirectories() {
3447534478
return __awaiter(this, void 0, void 0, function* () {
34476-
const { stdout, stderr, exitCode } = yield exec.getExecOutput('pip cache dir');
34479+
let exitCode = 1;
34480+
let stdout = '';
34481+
let stderr = '';
34482+
// Add temporary fix for Windows
34483+
// On windows it is necessary to execute through an exec
34484+
// because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2,
34485+
// or spawn must be started with the shell option enabled for getExecOutput
34486+
// Related issue: https://github.com/actions/setup-python/issues/328
34487+
if (utils_1.IS_WINDOWS) {
34488+
const execPromisify = util_1.default.promisify(child_process.exec);
34489+
({ stdout: stdout, stderr: stderr } = yield execPromisify('pip cache dir'));
34490+
}
34491+
else {
34492+
({
34493+
stdout: stdout,
34494+
stderr: stderr,
34495+
exitCode: exitCode
34496+
} = yield exec.getExecOutput('pip cache dir'));
34497+
}
3447734498
if (exitCode && stderr) {
3447834499
throw new Error(`Could not get cache folder path for pip package manager`);
3447934500
}

src/cache-distributions/pip-cache.ts

+22-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import * as glob from '@actions/glob';
22
import * as core from '@actions/core';
33
import * as exec from '@actions/exec';
4-
4+
import * as child_process from 'child_process';
5+
import utils from 'util';
56
import * as path from 'path';
67
import os from 'os';
78

89
import CacheDistributor from './cache-distributor';
10+
import {IS_WINDOWS} from '../utils';
911

1012
class PipCache extends CacheDistributor {
1113
constructor(
@@ -16,9 +18,25 @@ class PipCache extends CacheDistributor {
1618
}
1719

1820
protected async getCacheGlobalDirectories() {
19-
const {stdout, stderr, exitCode} = await exec.getExecOutput(
20-
'pip cache dir'
21-
);
21+
let exitCode = 1;
22+
let stdout = '';
23+
let stderr = '';
24+
25+
// Add temporary fix for Windows
26+
// On windows it is necessary to execute through an exec
27+
// because the getExecOutput gives a non zero code or writes to stderr for pip 22.0.2,
28+
// or spawn must be started with the shell option enabled for getExecOutput
29+
// Related issue: https://github.com/actions/setup-python/issues/328
30+
if (IS_WINDOWS) {
31+
const execPromisify = utils.promisify(child_process.exec);
32+
({stdout: stdout, stderr: stderr} = await execPromisify('pip cache dir'));
33+
} else {
34+
({
35+
stdout: stdout,
36+
stderr: stderr,
37+
exitCode: exitCode
38+
} = await exec.getExecOutput('pip cache dir'));
39+
}
2240

2341
if (exitCode && stderr) {
2442
throw new Error(

0 commit comments

Comments
 (0)