I am trying to sort out how to correctly use rollup_bundle() in bazel in a workspace using multiple languages, including JavaScript, and defining multiple npm packages in different directories.
It appears that the rollup bazel rules assume a more centralized setup than I would like: in the context of this workspace I would like each npm package to manage its own node_modules (see WORKSPACE.bazel in reproducible).
Small reproducible (toy example):
$ bazel build //js/alpha
Starting local Bazel server and connecting to it...
ERROR: /path/to/bazel-nodejs-rollup-multi-package/js/alpha/BUILD.bazel:6:14: every rule of type rollup_bundle implicitly depends upon the target '//@bazel/rollup/bin:rollup-worker', but this target could not be found because of: no such package '@bazel/rollup/bin': BUILD file not found in any of the following directories. Add a BUILD file to a directory to mark it as a package.
- /path/to/bazel-nodejs-rollup-multi-package/@bazel/rollup/bin
Documentation for implicit attribute rollup_worker_bin of rules of type rollup_bundle:
Internal use only
ERROR: Analysis of target '//js/alpha:alpha' failed; build aborted: Analysis failed
INFO: Elapsed time: 2.363s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (4 packages loaded, 5 targets configured)
currently loading: @rules_nodejs//nodejs/stamp
This error seems to suggest that I may need to specify rollup_bin and/or rollup_worker_bin, but I have had no luck attempting to refer to sources in //js/alpha/node_modules/@bazel/... or //js/alpha/node_modules/rollup/.... The npm documentation for @bazel/rollup suggests that because I am using npm_install() to set up my in-tree npm packages I should not need to set rollup_bin (because related instructions begin with "If you didn't use the yarn_install or npm_install rule, [...]").
How do I keep my multiple-npm-packages-in-same-workspace setup while using bazel rules to run rollup?
I posted a viable solution here:
https://github.com/mdittmer/bazel-nodejs-rollup-multi-package/commit/07c0810a7e2d4185311f4bab4f36555648585b97
The issue appears to stem from the
@build_bazel_rules_nodejsrules hard-coding@npm//...dependencies, i.e., they assume that the correct repository for loading npm module dependencies must be named@npm. To get multiple notions of@npm(theoretically--I only bothered to remap one such repository), I declared a local repository in the root//WORKSPACE.bazelfile://WORKSPACE.bazel:The
repo_mappingmaps my choice of repository to the name@npmin the@alpharepository. Presumably, repeating this with similarly configured@betaand@npm_betarepositories will achieve the desired configuration across multiple npm modules. In this setup the root//WORKSPACE.bazeldoes most of the "heavy lifting" and//js/alpha/WORKSPACE.bazelis practically empty.Aside: It appears that re-defining
@npmwith the configuration up to and including thenpm_install()with appropriately renamedname,package_json, andpackage_lock_jsonin//js/alpha/WORKSPACE.bazeldoes not interfere with the root repository working as intended and has the side benefit of the//js/alpharepository working correctly as a root repository if desired.One unresolved issue I was surprised to encounter was this:
https://github.com/mdittmer/bazel-nodejs-rollup-multi-package/commit/c2bde0fc02bce8f4b1b42dc5881a4f9bc26186db
switching from
npm_install()toyarn_install()did not work (see commit message in linked commit for details).