Skip to content

Commit

Permalink
fix: do not hard fail if Corepack home folder cannot be created (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
aduh95 authored Feb 21, 2024
1 parent 49ad334 commit 9834f57
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
32 changes: 19 additions & 13 deletions sources/Engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ export type PreparedPackageManagerInfo = Awaited<ReturnType<Engine[`ensurePackag
export function getLastKnownGoodFile(flag = `r`) {
return fs.promises.open(path.join(folderUtils.getCorepackHomeFolder(), `lastKnownGood.json`), flag);
}
async function createLastKnownGoodFile() {
await fs.promises.mkdir(folderUtils.getCorepackHomeFolder(), {recursive: true});
return getLastKnownGoodFile(`w`);
}

export async function getJSONFileContent(fh: FileHandle) {
let lastKnownGood: unknown;
Expand Down Expand Up @@ -139,17 +143,13 @@ export class Engine {
if (typeof definition === `undefined`)
throw new UsageError(`This package manager (${packageManager}) isn't supported by this corepack build`);

let emptyFile = false;
const lastKnownGoodFile = await getLastKnownGoodFile(`r+`).catch(err => {
if ((err as NodeError)?.code === `ENOENT`) {
emptyFile = true;
return getLastKnownGoodFile(`w`);
let lastKnownGoodFile = await getLastKnownGoodFile(`r+`).catch(err => {
if ((err as NodeError)?.code !== `ENOENT`) {
throw err;
}

throw err;
});
try {
const lastKnownGood = emptyFile || await getJSONFileContent(lastKnownGoodFile);
const lastKnownGood = lastKnownGoodFile == null || await getJSONFileContent(lastKnownGoodFile!);
const lastKnownGoodForThisPackageManager = getLastKnownGoodFromFileContent(lastKnownGood, packageManager);
if (lastKnownGoodForThisPackageManager)
return lastKnownGoodForThisPackageManager;
Expand All @@ -159,14 +159,20 @@ export class Engine {

const reference = await corepackUtils.fetchLatestStableVersion(definition.fetchLatestFrom);

await activatePackageManagerFromFileHandle(lastKnownGoodFile, lastKnownGood, {
name: packageManager,
reference,
});
try {
lastKnownGoodFile ??= await createLastKnownGoodFile();
await activatePackageManagerFromFileHandle(lastKnownGoodFile, lastKnownGood, {
name: packageManager,
reference,
});
} catch {
// If for some reason, we cannot update the last known good file, we
// ignore the error.
}

return reference;
} finally {
await lastKnownGoodFile.close();
await lastKnownGoodFile?.close();
}
}

Expand Down
19 changes: 19 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,22 @@ it(`should show a warning on stderr before downloading when enable`, async() =>
});
});
});

it(`should be able to show the latest version`, async () => {
process.env.COREPACK_DEFAULT_TO_LATEST = `1`;
await xfs.mktempPromise(async cwd => {
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: /^1\.\d+\.\d+\r?\n$/,
stderr: ``,
});

// Should keep working if the home folder is removed
await xfs.rmdirPromise(process.env.COREPACK_HOME as any, {recursive: true});
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
exitCode: 0,
stdout: /^1\.\d+\.\d+\r?\n$/,
stderr: ``,
});
});
});
Binary file added tests/nock/r3ZEChhqdXDvJXR4Qhvk0A-1.dat
Binary file not shown.
Binary file added tests/nock/r3ZEChhqdXDvJXR4Qhvk0A-2.dat
Binary file not shown.

0 comments on commit 9834f57

Please sign in to comment.