8000 poetry: Run `poetry env use` only after cache is loaded · actions/setup-python@a957cf8 · GitHub
[go: up one dir, main page]

Skip to content

Commit a957cf8

Browse files
committed
poetry: Run poetry env use only after cache is loaded
The virtualenv cache might contain invalid entries, such as virtualenvs built in previous, buggy versions of this action. The `poetry env use` command will recreate virtualenvs in case they are invalid, but it has to be run only *after* the cache is loaded. Refactor `CacheDistributor` a bit such that the validation (and possible recreation) of virtualenvs happens only after the cache is loaded.
1 parent bc3992e commit a957cf8

File tree

2 files changed

+33
-20
lines changed

2 files changed

+33
-20
lines changed

src/cache-distributions/cache-distributor.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class CacheDistributor {
1919
primaryKey: string;
2020
restoreKey: string[] | undefined;
2121
}>;
22+
protected async handleLoadedCache() {}
2223

2324
public async restoreCache() {
2425
const {primaryKey, restoreKey} = await this.computeKeys();
@@ -41,6 +42,8 @@ abstract class CacheDistributor {
4142
restoreKey
4243
);
4344

45+
await this.handleLoadedCache();
46+
4447
this.handleMatchResult(matchedKey, primaryKey);
4548
}
4649

src/cache-distributions/poetry-cache.ts

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import {logWarning} from '../utils';
1010
class PoetryCache extends CacheDistributor {
1111
constructor(
1212
private pythonVersion: string,
13-
protected patterns: string = '**/poetry.lock'
13+
protected patterns: string = '**/poetry.lock',
14+
protected poetryProjects: Set<string> = new Set<string>()
1415
) {
1516
super('poetry', patterns);
1617
}
@@ -20,16 +21,10 @@ class PoetryCache extends CacheDistributor {
2021
const paths = new Set<string>();
2122
const globber = await glob.create(this.patterns);
2223

23-
const pythonLocation = await io.which('python');
24-
if (pythonLocation) {
25-
core.debug(`pythonLocation is ${pythonLocation}`);
26-
} else {
27-
logWarning('python binaries were not found in PATH');
28-
}
29-
3024
for await (const file of globber.globGenerator()) {
3125
const basedir = path.dirname(file);
3226
core.debug(`Processing Poetry project at ${basedir}`);
27+
this.poetryProjects.add(basedir);
3328

3429
const poetryConfig = await this.getPoetryConfiguration(basedir);
3530

@@ -44,18 +39,6 @@ class PoetryCache extends CacheDistributor {
4439
if (poetryConfig['virtualenvs.in-project']) {
4540
paths.add(path.join(basedir, '.venv'));
4641
}
47-
48-
if (pythonLocation) {
49-
const {exitCode, stderr} = await exec.getExecOutput(
50-
'poetry',
51-
['env', 'use', pythonLocation],
52-
{ignoreReturnCode: true, cwd: basedir}
53-
);
54-
55-
if (exitCode) {
56-
logWarning(stderr);
57-
}
58-
}
5942
}
6043

6144
return [...paths];
@@ -71,6 +54,33 @@ class PoetryCache extends CacheDistributor {
7154
};
7255
}
7356

57+
protected async handleLoadedCache() {
58+
await super.handleLoadedCache();
59+
60+
// After the cache is loaded -- make sure virtualenvs use the correct Python version (the one that we have just installed).
61+
// This will handle invalid caches, recreating virtualenvs if necessary.
62+
63+
const pythonLocation = await io.which('python');
64+
if (pythonLocation) {
65+
core.debug(`pythonLocation is ${pythonLocation}`);
66+
} else {
67+
logWarning('python binaries were not found in PATH');
68+
return;
69+
}
70+
71+
for (const poetryProject of this.poetryProjects) {
72+
const {exitCode, stderr} = await exec.getExecOutput(
73+
'poetry',
74+
['env', 'use', pythonLocation],
75+
{ignoreReturnCode: true, cwd: poetryProject}
76+
);
77+
78+
if (exitCode) {
79+
logWarning(stderr);
80+
}
81+
}
82+
}
83+
7484
private async getPoetryConfiguration(basedir: string) {
7585
const {stdout, stderr, exitCode} = await exec.getExecOutput(
7686
'poetry',

0 commit comments

Comments
 (0)
0