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
Bugfix: fix circular dependency loop in dev #3562
Conversation
🦋 Changeset detectedLatest commit: 8137901 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request is being automatically deployed with Vercel (learn more). 🔍 Inspect: https://vercel.com/pikapkg/snowpack/6ENtfdTXMyfVGESwUAfaao8GqnPb |
const spec = code.substring(imp.s, imp.e).replace(/(\/|\\)+$/, ''); // remove trailing slash from end of specifier (easier for Node to resolve) | ||
if (isRemoteUrl(spec)) { | ||
continue; | ||
const scannedImport = code.substring(imp.s, imp.e).replace(/(\/|\\)+$/, ''); // remove trailing slash from end of specifier (easier for Node to resolve) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor change: don’t shadow spec
in parent scope; it’s confusing.
0cedb3b
to
31a8b52
Compare
continue; | ||
} | ||
packageImports.add(spec); | ||
const [scannedPackageName] = parsePackageImportSpecifier(scannedImport); | ||
if (scannedPackageName && memoizedImportMap[scannedPackageName]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the actual fix here: using the in-memory import maps to prevent reinstalling duplicate packages (import map means install happened successfully). Otherwise, we would end up stuck in an infinite loop installing circular npm deps.
31a8b52
to
5968922
Compare
snowpack/src/sources/local.ts
Outdated
await importMapHandle.close(); | ||
let existingImportMap: ImportMap | undefined = memoizedImportMap[packageName]; | ||
if (!existingImportMap && existsSync(existingImportMapLoc)) { | ||
existingImportMap = JSON.parse(await fs.readFile(existingImportMapLoc, 'utf8')); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using existsSync
and then readFile
still scares me as the previous bug was that the file existed but was not open for writing (because currently being written) which caused it to throw at this line. Even if logic prevents the race we should still use fs.open
here as that guarantees both that the file exists and that it's finished being written to.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh you have a good point. Will change back, but probably keep the in-memory memoization for already-read files
I looked at it and it does seem to solve the bug - at least it does not keep re-resolving the same packages again and again. Who can help move it forward? :) |
5968922
to
a0e39df
Compare
b279c67
to
6b8d9aa
Compare
6b8d9aa
to
480ff48
Compare
480ff48
to
3c8cab7
Compare
@hklsiteimprove sorry about this! It fell off my plate. Updated the PR and this should go out in the next release after it merges |
3c8cab7
to
db0851e
Compare
db0851e
to
ef7a2b9
Compare
ef7a2b9
to
ebfc585
Compare
ebfc585
to
3974fb4
Compare
3974fb4
to
f72f346
Compare
f72f346
to
06ca409
Compare
06ca409
to
8137901
Compare
* Though this test is slow, it’s important to test on real npm packages and not mocked ones | ||
* as symlink behavior is really different here | ||
*/ | ||
describe.skip('@nivo/core', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the comment said, added a test here which can be run manually. But skipping this in GitHub because for some reason this test keeps flaking in GitHub CI. But I’m able to run it on my machine in Node 12, 14, and 16 without fail every time. So this does fix the issue!
@drwpow This PR fixed the circular resolving really nicely.
I can apply a fix inside the resolving of |
Changes
Fixes #3466. If an npm dependency depends on another, we were trying to install both over and over again without checking. This PR memoizes resolved npm packages so each is only hit once.
This should also improve the dev server cold start time by a little bit, as packages are better-memoized now.
Testing
Test added
Docs
Internal change; no docs needed