Skip to content

Commit

Permalink
Use correct Poetry config when collecting Poetry projects
Browse files Browse the repository at this point in the history
When collecting Poetry projects for caching, a '**/poetry.lock' glob is
used.  However, in order to process the Poetry configuration, the
"poetry" command is run from the repo's root directory; this causes
Poetry to return an invalid configuration when there is a Poetry project
inside an inner directory.

Instead of running a single Poetry command, glob for the same pattern,
and run a Poetry command for every discovered project.
  • Loading branch information
oranav committed Jun 27, 2022
1 parent 2818fdc commit 0a5c926
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 26 deletions.
4 changes: 3 additions & 1 deletion __tests__/cache-restore.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ describe('restore-cache', () => {
'd8110e0006d7fb5ee76365d565eef9d37df1d11598b912d3eb66d398d57a1121';
const requirementsLinuxHash =
'2d0ff7f46b0e120e3d3294db65768b474934242637b9899b873e6283dfd16d7c';
// poetry.lock is hashed twice:
// sha256(sha256(data/poetry.lock) || sha256(data/inner/poetry.lock))
const poetryLockHash =
'571bf984f8d210e6a97f854e479fdd4a2b5af67b5fdac109ec337a0ea16e7836';
'f24ea1ad73968e6c8d80c16a093ade72d9332c433aeef979a0dd943e6a99b2ab';
const poetryConfigOutput = `
cache-dir = "/Users/patrick/Library/Caches/pypoetry"
experimental.new-installer = false
Expand Down
1 change: 1 addition & 0 deletions __tests__/data/inner/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 31 additions & 11 deletions dist/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63871,6 +63871,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __asyncValues = (this && this.__asyncValues) || function (o) {
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
var m = o[Symbol.asyncIterator], i;
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Expand All @@ -63886,13 +63893,29 @@ class PoetryCache extends cache_distributor_1.default {
this.patterns = patterns;
}
getCacheGlobalDirectories() {
var e_1, _a;
return __awaiter(this, void 0, void 0, function* () {
const poetryConfig = yield this.getPoetryConfiguration();
const cacheDir = poetryConfig['cache-dir'];
const virtualenvsPath = poetryConfig['virtualenvs.path'].replace('{cache-dir}', cacheDir);
const paths = [virtualenvsPath];
if (poetryConfig['virtualenvs.in-project'] === true) {
paths.push(path.join(process.cwd(), '.venv'));
const paths = [];
const globber = yield glob.create(this.patterns);
try {
for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) {
const file = _c.value;
const basedir = path.dirname(file);
const poetryConfig = yield this.getPoetryConfiguration(basedir);
const cacheDir = poetryConfig['cache-dir'];
const virtualenvsPath = poetryConfig['virtualenvs.path'].replace('{cache-dir}', cacheDir);
paths.push(virtualenvsPath);
if (poetryConfig['virtualenvs.in-project'] === true) {
paths.push(path.join(basedir, '.venv'));
}
}
}
catch (e_1_1) { e_1 = { error: e_1_1 }; }
finally {
try {
if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b);
}
finally { if (e_1) throw e_1.error; }
}
return paths;
});
Expand All @@ -63908,12 +63931,9 @@ class PoetryCache extends cache_distributor_1.default {
};
});
}
getPoetryConfiguration() {
getPoetryConfiguration(basedir) {
return __awaiter(this, void 0, void 0, function* () {
const { stdout, stderr, exitCode } = yield exec.getExecOutput('poetry', [
'config',
'--list'
]);
const { stdout, stderr, exitCode } = yield exec.getExecOutput('poetry', ['config', '--list'], { cwd: basedir });
if (exitCode && stderr) {
throw new Error('Could not get cache folder path for poetry package manager');
}
Expand Down
35 changes: 21 additions & 14 deletions src/cache-distributions/poetry-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@ class PoetryCache extends CacheDistributor {
}

protected async getCacheGlobalDirectories() {
const poetryConfig = await this.getPoetryConfiguration();
const paths = [];
const globber = await glob.create(this.patterns);

const cacheDir = poetryConfig['cache-dir'];
const virtualenvsPath = poetryConfig['virtualenvs.path'].replace(
'{cache-dir}',
cacheDir
);
for await (const file of globber.globGenerator()) {
const basedir = path.dirname(file);
const poetryConfig = await this.getPoetryConfiguration(basedir);

const cacheDir = poetryConfig['cache-dir'];
const virtualenvsPath = poetryConfig['virtualenvs.path'].replace(
'{cache-dir}',
cacheDir
);

const paths = [virtualenvsPath];
paths.push(virtualenvsPath);

if (poetryConfig['virtualenvs.in-project'] === true) {
paths.push(path.join(process.cwd(), '.venv'));
if (poetryConfig['virtualenvs.in-project'] === true) {
paths.push(path.join(basedir, '.venv'));
}
}

return paths;
Expand All @@ -40,11 +46,12 @@ class PoetryCache extends CacheDistributor {
};
}

private async getPoetryConfiguration() {
const {stdout, stderr, exitCode} = await exec.getExecOutput('poetry', [
'config',
'--list'
]);
private async getPoetryConfiguration(basedir: string) {
const {stdout, stderr, exitCode} = await exec.getExecOutput(
'poetry',
['config', '--list'],
{cwd: basedir}
);

if (exitCode && stderr) {
throw new Error(
Expand Down

0 comments on commit 0a5c926

Please sign in to comment.