Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix race condition reading existing import-map.json files #3453

Merged
merged 2 commits into from Jun 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 9 additions & 3 deletions snowpack/src/sources/local.ts
Expand Up @@ -493,9 +493,15 @@ export class PackageSourceLocal implements PackageSource {
const lineBullet = colors.dim(depth === 0 ? '+' : '└──'.padStart(depth * 2 + 1, ' '));
let packageFormatted = spec + colors.dim('@' + packageVersion);
const existingImportMapLoc = path.join(installDest, 'import-map.json');
const existingImportMap: ImportMap | null =
(await fs.stat(existingImportMapLoc).catch(() => null)) &&
JSON.parse(await fs.readFile(existingImportMapLoc, 'utf8'));
const importMapHandle = await fs.open(existingImportMapLoc, 'r+').catch(() => null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like the PR notes said, it’s hard for me to test/confirm this but overall this seems like a pretty safe change to me, so I’m +1


let existingImportMap: ImportMap | null = null;
if(importMapHandle) {
const importMapData = await importMapHandle.readFile('utf-8');
existingImportMap = importMapData ? JSON.parse(importMapData) : null;
await importMapHandle.close();
}

// Kick off a build, if needed.
let importMap = existingImportMap;
let needsBuild = !existingImportMap?.imports[spec];
Expand Down