aspect-build / rules_js

High-performance Bazel rules for running Node.js tools and building JavaScript projects
https://docs.aspect.build/rules/aspect_rules_js
Apache License 2.0
302 stars 105 forks source link

[Bug]: `public_hoist_packages` does not hoist dependencies in `bazel-bin/node_modules` #1419

Open zhihao-gong opened 9 months ago

zhihao-gong commented 9 months ago

What happened?

I am migrating my vue project to bazel and rules_js.

I used @vue/cli-service@4.5.19 which transitively uses plugins such as @vue/cli-plugin-babel, but without declaring it in dependencies. So I plan to use public_hoist_packages to make @vue/cli-service aware of @vue/cli-plugin-babel. But the public_hoist_packages field did not hoist @vue/cli-plugin-babel in bazel-bin/node_modules.

Based on docs, public_hoist_packages will do the same thing as public-hoist-pattern in pnpm. I also tried bazelbuild/examples/frontend and didn't see changes in bazel-bin/node_modules either.

Version

Development (host) and target OS/architectures: x86_64 GNU/Linux ubuntu focal

Output of bazel --version: bazel 7.0.0

Version of the Aspect rules, or other relevant rules from your WORKSPACE or MODULE.bazel file: rules_js version is 1.34.1

How to reproduce

I built all targets in bazelbuild/examples/frontend and didn't see hoisting in bazel-bin/node_modules

tusen@desk-0181:~/examples/frontend$ bazel build ...
INFO: Analyzed 69 targets (1236 packages loaded, 10039 targets configured).
INFO: Found 69 targets...
INFO: Elapsed time: 9.104s, Critical Path: 0.09s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
tusen@desk-0181:~/examples/frontend$ ls bazel-bin/node_modules/
jsdom  vitest

I tried pnpm i with public_hoist_pattern which did the hoisting in node_modules directory.

tusen@desk-0181:~/examples/frontend$ pnpm i
Scope: all 7 workspace projects
Lockfile is up to date, resolution step is skipped
Already up to date
Done in 1.6s
tusen@desk-0181:~/examples/frontend$ ls node_modules/
@next                        eslint-import-resolver-typescript  eslint-plugin-jsx-a11y         eslint-utils
eslint                       eslint-module-utils                eslint-plugin-react            eslint-visitor-keys
eslint-config-next           eslint-plugin-flowtype             eslint-plugin-react-hooks      jsdom
eslint-config-react-app      eslint-plugin-import               eslint-plugin-testing-library  vitest
eslint-import-resolver-node  eslint-plugin-jest                 eslint-scope
tusen@desk-0181:~/examples/frontend$ cat .npmrc 
# Disabling pnpm [hoisting](https://pnpm.io/npmrc#hoist) by setting `hoist=false` is recommended on
# projects using rules_js so that pnpm outside of Bazel lays out a node_modules tree similar to what
# rules_js lays out under Bazel (without a hidden node_modules/.pnpm/node_modules). See
# https://github.com/aspect-build/rules_js/blob/7377f2d0387cc2a9251137929b1c53ccdb3fbcf0/docs/npm_import.md#npm_translate_lock
# documentation for more information.
hoist=false

public-hoist-pattern[]=@next/eslint-plugin-next*
public-hoist-pattern[]=eslint*
fa93hws commented 2 months ago

Hi just got the same issue in https://github.com/aspect-build/rules_js/issues/1884 and I notice that you need to reference the hoisted dependency in root package instead of that in sub package. e.g. //:node_modules/@vue/cli-plugin-babel instead of //my-vue-project:node_modules/@vue/cli-plugin-babel.

Note: No need to install @vue/cli-plugin-babel to root package.