rules_js
rules_js copied to clipboard
Fix broken symlinks during js_run_devserver runs.
This fixes an issue I've been facing for a while now while trying to run the js_run_devserver
target in any of the examples on a windows machine see https://github.com/bazelbuild/examples/issues/327
The core of the issue is that windows treats symlinks differently than other OS's, for better or worse. The nodejs fs.symlink
needs to be aware, at least on widows machines, what kind of symlink it's attempting to create. Otherwise at least in my testing it will assume file
which causes any node_modules/$PACKAGE
to be symlinked to the run directory as a file, which in turn prevents node from finding anything.
My change here should only affect windows machines and will fallback to the default behavior for all other OS's.
Type of change
- Bug fix (change which fixes an issue)
Test plan
- Manual testing; please provide instructions so we can reproduce:
To reproduce
- clone https://github.com/bazelbuild/examples
- patch in the follow: we do this because the default
command = "node_modules/.bin/react-scripts",
just wont work on windows machines as that is generated from https://github.com/aspect-build/rules_js/blob/89e9065dc7ad3b456694dc2f3f37cfe4cb0b7778/npm/private/npm_link_package_store.bzl#L69 a different issue than the one i'm attempting to fix here though maybe with the addition of https://github.com/pnpm/pnpm/issues/5131 or a more sophisticated OS aware templating or even an option to the end user to eagerly fetch all bins could fix this.
# frontend/react/BUILD.bazel
js_run_devserver(
name = "start",
args = [
"node_modules/react-scripts/bin/react-scripts.js", "start",
],
command = "C:\\Program Files\\nodejs\\node.exe", # OR WHERE EVER NODE IS FOR YOU
chdir = package_name(),
data = CRA_DEPS,
)
- After patching in the above attempt to run
bazel run react:start
- Here you should receive a failure, scroll up in the terminal and look for a line like
Running 'C:\Program Files\nodejs\node.exe node_modules/react-scripts/bin/react-scripts.js start' in C:\Users\SOMEUSER\AppData\Local\Temp\js_run_devserver-SOMEHASH\aspect_rules_js\frontend
- Open that directory and navigate to the
node_modules
you should see something like this
Those packages are not valid symlinks, they do point to the correct locations, however from windows perspective and other processes trying to open those, they are invalid.
After reproducing the failure you should be able to patch in this PR with something like
# frontend/MODULE.bazel
single_version_override(
module_name = "aspect_rules_js",
patch_strip = 1,
patches = ["//:aspect_rules_js_1.32.2.patch"],
)
Then re-run the bazel run react:start
this should now succeed and viewing the run directory should now look similar to this: