Repository navigation
Description
Description of the problem / feature request:
rules_nodejs yarn_install & npm_install with managed directories can't handle deleted or manually regenerated node_modules folder. See bazel-contrib/rules_nodejs#802 for full description.
Feature requests: what underlying problem are you trying to solve with this feature?
When using managed directories with yarn_install & npm_install in rules_nodejs 0.30.0+, when a user deletes the node_modules folder and runs bazel the repository rule should re-run (managed directory missing) which would recreate the node_modules folder from scratch.
If a user deletes the node_modules folder and recreates it manually using yarn or npm install then the repository rule should also re-run in that case as well (managed directory timestamp changed).
Currently after deleting node_modules, the only way to get back to a working build (besides modifying package.json or the lock file) is to run bazel clean --expunge to clear out the external repository and force the yarn_install or npm_install rule to rerun.
Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.
case 1:
git clone https://github.com/gregmagolan/bazel_with_users_node_modules
cd bazel_with_users_node_modules
bazel version
bazel test ...
rm -rf ./node_modules
bazel test ...
case 2:
git clone https://github.com/gregmagolan/bazel_with_users_node_modules
cd bazel_with_users_node_modules
bazel version
bazel test ...
rm -rf ./node_modules
yarn install
bazel test ...
error in both cases:
greg@macbook bazel_with_users_node_modules (master) $ bazel test ...
ERROR: /private/var/tmp/_bazel_greg/8cb9ee79286b251e74f070e4ed053074/external/npm/typescript/BUILD.bazel:123:1: no such package '@npm//node_modules/typescript': BUILD file not found on package path and referenced by '@npm//typescript:typescript'
ERROR: Analysis of target '//:main' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.205s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
FAILED: Build did NOT complete successfully (0 packages loaded, 0 targets configured)
What operating system are you running Bazel on?
OSX but issue is OS agnostic.
What's the output of bazel info release?
release 0.26.0-homebrew
Activity
gregmagolan commentedon May 29, 2019
/cc @irengrig @alexeagle
Treat existence of managed directories as a part of repository dirtin…
build(bazel): update to nodejs rules 0.31.1 (#30627)
build: set symlink_node_modules = False for yarn_install in main WORK…
build(bazel): update to nodejs rules 0.31.1 & bazel 0.26.1 (#30965)
Treat existence of managed directories as a part of repository dirtin…
gregmagolan commentedon Jun 12, 2019
Tested 0.27.0rc5 in my repro repo and the first case of the node_modules folder being deleted is fixed:
gregmagolan commentedon Jun 12, 2019
The 2nd case described where node_modules is deleted and then manually re-created with
yarn installornpm installoutside of Bazel is still broken.Repro is:
=>
Treat existence of managed directories as a part of repository dirtin…
Treat existence of managed directories as a part of repository dirtin…
schroederc commentedon Aug 6, 2019
Is this fixed in any released version?
irengrig commentedon Aug 8, 2019
The fix to determine if the node_modules (managed directory) exists is in Bazel 0.27.1.
The other part, about the node_modules (managed directories) being deleted and re-created, is tracked here: #9080
kylecordes commentedon Oct 30, 2019
I think this is a potentially big problem - is it really solved by the proposed solution?
Out in the wild, it's common for developers to perform operations that mutate contents of node_modules. It's not rare for Node-based software to write to node_modules. Therefore, it seems insufficient (for a correct and reliable development experience) to just look for a missing or time stamped node_modules as a signal to recreate.
So I wonder if this is really a case echoing some other discussion (forget where) about the need for users to have (read-only) access to intermediate build output like generated code.
How can a writable node_modules exposed to users or tools be trusted to contain the results of a yarn-install?
At the moment, my impression from all this (maybe wrong) is that to get a reliable build, a user must freshly:
rm -rf node_modules(sometimes more than one in a complex project)bazel build --expungeWhat can be done to make node_modules (both what users see, and what rules see) reliably contain a pure function of the contents of package.json, lock file, and upstream NPM repos?
migueloller commentedon Mar 31, 2020
@kylecordes, if Node-based software truly writes to node_modules then there would be an issue whether managed directories is being used or not.
More info here: https://docs.bazel.build/versions/master/skylark/repository_rules.html#when-is-the-implementation-function-executed
A way to avoid having to run those commands is to just call
bazel sync. This will take care of refetching the repositories. According to the help documentation this command seems to be experimental and the semantics of it might change but it seems like the tool for solving this specific job. A big improvement to this command would be the ability to sync a specific label to avoid fetching all the repos in the workspace.alexeagle commentedon Sep 14, 2021
One possible way to resolve some of the user pain is for
bazel clean --expungeto delete the managed directory in the user's sources at the same time the external repository is deleted. At least this way there's a single method for uncorrupting the external repo state, rather than also requiringrm -rf node_modulesgregmagolan commentedon Jan 17, 2023
No longer needed as managed directories is removed