Skip to content

Commit 9834f57

Browse files
authored
fix: do not hard fail if Corepack home folder cannot be created (#382)
1 parent 49ad334 commit 9834f57

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

sources/Engine.ts

+19-13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export type PreparedPackageManagerInfo = Awaited<ReturnType<Engine[`ensurePackag
2121
export function getLastKnownGoodFile(flag = `r`) {
2222
return fs.promises.open(path.join(folderUtils.getCorepackHomeFolder(), `lastKnownGood.json`), flag);
2323
}
24+
async function createLastKnownGoodFile() {
25+
await fs.promises.mkdir(folderUtils.getCorepackHomeFolder(), {recursive: true});
26+
return getLastKnownGoodFile(`w`);
27+
}
2428

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

142-
let emptyFile = false;
143-
const lastKnownGoodFile = await getLastKnownGoodFile(`r+`).catch(err => {
144-
if ((err as NodeError)?.code === `ENOENT`) {
145-
emptyFile = true;
146-
return getLastKnownGoodFile(`w`);
146+
let lastKnownGoodFile = await getLastKnownGoodFile(`r+`).catch(err => {
147+
if ((err as NodeError)?.code !== `ENOENT`) {
148+
throw err;
147149
}
148-
149-
throw err;
150150
});
151151
try {
152-
const lastKnownGood = emptyFile || await getJSONFileContent(lastKnownGoodFile);
152+
const lastKnownGood = lastKnownGoodFile == null || await getJSONFileContent(lastKnownGoodFile!);
153153
const lastKnownGoodForThisPackageManager = getLastKnownGoodFromFileContent(lastKnownGood, packageManager);
154154
if (lastKnownGoodForThisPackageManager)
155155
return lastKnownGoodForThisPackageManager;
@@ -159,14 +159,20 @@ export class Engine {
159159

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

162-
await activatePackageManagerFromFileHandle(lastKnownGoodFile, lastKnownGood, {
163-
name: packageManager,
164-
reference,
165-
});
162+
try {
163+
lastKnownGoodFile ??= await createLastKnownGoodFile();
164+
await activatePackageManagerFromFileHandle(lastKnownGoodFile, lastKnownGood, {
165+
name: packageManager,
166+
reference,
167+
});
168+
} catch {
169+
// If for some reason, we cannot update the last known good file, we
170+
// ignore the error.
171+
}
166172

167173
return reference;
168174
} finally {
169-
await lastKnownGoodFile.close();
175+
await lastKnownGoodFile?.close();
170176
}
171177
}
172178

tests/main.test.ts

+19
Original file line numberDiff line numberDiff line change
@@ -739,3 +739,22 @@ it(`should show a warning on stderr before downloading when enable`, async() =>
739739
});
740740
});
741741
});
742+
743+
it(`should be able to show the latest version`, async () => {
744+
process.env.COREPACK_DEFAULT_TO_LATEST = `1`;
745+
await xfs.mktempPromise(async cwd => {
746+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
747+
exitCode: 0,
748+
stdout: /^1\.\d+\.\d+\r?\n$/,
749+
stderr: ``,
750+
});
751+
752+
// Should keep working if the home folder is removed
753+
await xfs.rmdirPromise(process.env.COREPACK_HOME as any, {recursive: true});
754+
await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({
755+
exitCode: 0,
756+
stdout: /^1\.\d+\.\d+\r?\n$/,
757+
stderr: ``,
758+
});
759+
});
760+
});
1.83 MB
Binary file not shown.
1.83 MB
Binary file not shown.

0 commit comments

Comments
 (0)