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: update package cache watcher #12772

Merged
merged 1 commit into from Apr 6, 2023
Merged

Conversation

bluwy
Copy link
Member

@bluwy bluwy commented Apr 6, 2023

Description

The watcher is incorrect after the recent refactors, I've made some fixes below which should preserve the old behaviour, except that it would also only watch package.json not inside node_modules now. Before that, we did and I think it's taxing to do so.

Additional context

I'll leave comments below to explain the changes. I've also manually tested this locally.


What is the purpose of this pull request?

  • Bug fix
  • New Feature
  • Documentation update
  • Other

Before submitting the PR, please make sure you do the following

  • Read the Contributing Guidelines.
  • Read the Pull Request Guidelines and follow the PR Title Convention.
  • Check that there isn't already a PR that solves the problem the same way to avoid creating a duplicate.
  • Provide a description in this PR that addresses what the PR is solving, or reference the issue that it solves (e.g. fixes #123).
  • Ideally, include relevant tests that fail without this PR but pass with it.

@bluwy bluwy added the p3-minor-bug An edge case that only affects very specific usage (priority) label Apr 6, 2023
@stackblitz
Copy link

stackblitz bot commented Apr 6, 2023

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

packageCache: PackageCache,
pkgPath: string,
): void {
packageCache.delete(pkgPath)
Copy link
Member Author

Choose a reason for hiding this comment

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

This isn't really working now because cache keys have rpd_ and fnpd_ prefixes. The below forEach loop should cover this instead.

Comment on lines +230 to +232
if (!isInNodeModules(pkg.dir) && !watchedDirs.has(pkg.dir)) {
watchedDirs.add(pkg.dir)
watchFile(path.join(pkg.dir, 'package.json'))
Copy link
Member Author

Choose a reason for hiding this comment

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

Make sure to only watch package.json in node_modules, also uses a watchedDirs set to cache as this .set call can be blasted with many same paths. In dev, watchFile would invoke a fs.existSync call otherwise.

Comment on lines 247 to 255
watchChange(id) {
if (id.endsWith('/package.json')) {
invalidatePackageData(packageCache, id)
invalidatePackageData(packageCache, path.normalize(id))
}
},
handleHotUpdate({ file }) {
if (file.endsWith('/package.json')) {
invalidatePackageData(packageCache, path.normalize(file))
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This plugin used to be build only, now it also handles dev by using handleHotUpdate

Comment on lines -529 to -536
const { packageCache } = config
const setPackageData = packageCache.set.bind(packageCache)
packageCache.set = (id, pkg) => {
if (id.endsWith('.json')) {
watcher.add(id)
}
return setPackageData(id, pkg)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Removed as handled in handleHotUpdate now.

Comment on lines -559 to -561
if (file.endsWith('/package.json')) {
return invalidatePackageData(packageCache, file)
}
Copy link
Member Author

Choose a reason for hiding this comment

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

This return call was blocking HMR updates for package.json, that also blocks handleHotUpdate, so removed here.

@patak-dev patak-dev merged commit a78588f into main Apr 6, 2023
12 checks passed
@patak-dev patak-dev deleted the remove-package-cache-watch branch April 6, 2023 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants