rules_nodejs icon indicating copy to clipboard operation
rules_nodejs copied to clipboard

`js_library` exports using `package_path` don't seem to work from external workspaces.

Open psigen opened this issue 3 years ago β€’ 1 comments

🐞 bug report

Affected Rule

This issue is probably either in:

  • js_library
  • nodejs_binary

Is this a regression?

Not sure, I did not immediately find a working older version.

Description

The package_path directive seems to only work on references within the same repository, and does not work when the same library is referenced externally.

Suppose there are two workspaces, ext_ws and main_ws. In ext_ws a simple js_library is defined with a package_path="@ext_ws/foo" and can be imported by nodejs_binary targets within the workspace using import foo from "@ext_ws/foo".

However, this fails to work when the same library is referenced from a nodejs_library in another workspace, main_ws, that is loading ext_ws. Note that this target can contains identical source code and BUILD definition to the target in ext_ws that works.

πŸ”¬ Minimal Reproduction

https://github.com/psigen/rules_nodejs-issue-3479

πŸ”₯ Exception or Error

Instead of importing successfully, running the affected code returns a generic import failure:

$ cd main_ws
$ bazel run //:bin
INFO: Analyzed target //:bin (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //:bin up-to-date:
  bazel-bin/bin.sh
  bazel-bin/bin_loader.cjs
  bazel-bin/bin_require_patch.cjs
INFO: Elapsed time: 0.245s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action
Error: Cannot find module '@ext/ws/lib'
Require stack:
- /home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/bin.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (bin.js:1:13)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

🌍 Your Environment

Operating System:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.4 LTS"

Output of bazel version:

Build label: 5.1.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Apr 8 15:49:48 2022 (1649432988)
Build timestamp: 1649432988
Build timestamp as int: 1649432988

Rules_nodejs version:

(Please check that you have matching versions between WORKSPACE file and @bazel/* npm packages.)

http_archive(
    name = "rules_nodejs",
    sha256 = "4d48998e3fa1e03c684e6bdf7ac98051232c7486bfa412e5b5475bbaec7bb257",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.0/rules_nodejs-core-5.5.0.tar.gz"],
)

http_archive(
    name = "build_bazel_rules_nodejs",
    sha256 = "0fad45a9bda7dc1990c47b002fd64f55041ea751fafc00cd34efb96107675778",
    urls = ["https://github.com/bazelbuild/rules_nodejs/releases/download/5.5.0/rules_nodejs-5.5.0.tar.gz"],
)

Anything else relevant?

If we compare the execroots in the local vs external workspace cases, we see that a node_modules entry is populated in the former case, but only an empty root directory is created in the latter:

# Execroot from a run of `@ext_ws//:bin
$ find /home/pras/.cache/bazel/_bazel_pras/92b5a6b34dd09a1331f2af4192a3d949/execroot/ | grep @ext
/home/pras/.cache/bazel/_bazel_pras/92b5a6b34dd09a1331f2af4192a3d949/execroot/ext_ws/node_modules/@ext
/home/pras/.cache/bazel/_bazel_pras/92b5a6b34dd09a1331f2af4192a3d949/execroot/ext_ws/node_modules/@ext/ws
# Execroot from a run of `@main_ws//:bin
$ find /home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/ | grep @ext
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/node_modules/@ext

psigen avatar Jun 05 '22 04:06 psigen

An additional discovery: including any source file from an external repository in js_library simply leads to it being omitted. I added a new target to the minimal reproduction linked in the ticket.

cd main_ws && bazel run //:bin2

INFO: Analyzed target //:bin2 (2 packages loaded, 4 targets configured).
INFO: Found 1 target...
Target //:bin2 up-to-date:
  bazel-bin/bin2.sh
  bazel-bin/bin2_loader.cjs
  bazel-bin/bin2_require_patch.cjs
INFO: Elapsed time: 0.209s, Critical Path: 0.01s
INFO: 7 processes: 7 internal.
INFO: Build completed successfully, 7 total actions
INFO: Build completed successfully, 7 total actions
Error: Cannot find module '@ext/ws/lib'
Require stack:
- /home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin2.sh.runfiles/main_ws/bin.js
    at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
    at Function.Module._load (node:internal/modules/cjs/loader:778:27)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at Object.<anonymous> (bin.js:1:13)
    at Module._compile (node:internal/modules/cjs/loader:1105:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)

If we inspect the execroot here, we can see that the module is created, but does not include source file from the external repository.

# Execroot from a run of `@main_ws//:bin2
$ find -L /home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/ | grep node_modules
find: File system loop detected; β€˜/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/bin.sh.runfiles’ is part of the same file system loop as β€˜/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/’.
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/bin_loader.cjs
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/bin_require_patch.cjs
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/bin.sh
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/_bin.module_mappings.json
/home/pras/.cache/bazel/_bazel_pras/df2aff05c573141092662ae5db828664/execroot/main_ws/bazel-out/k8-fastbuild/bin/bin.sh.runfiles/main_ws/node_modules/@ext/ws/bin.sh.runfiles_manifest

psigen avatar Jun 05 '22 05:06 psigen

This issue has been automatically marked as stale because it has not had any activity for 6 months. It will be closed if no further activity occurs in 30 days. Collaborators can add a "cleanup" or "need: discussion" label to keep it open indefinitely. Thanks for your contributions to rules_nodejs!

github-actions[bot] avatar Dec 11 '22 02:12 github-actions[bot]

This issue was automatically closed because it went 30 days without any activity since it was labeled "Can Close?"

github-actions[bot] avatar Jan 11 '23 02:01 github-actions[bot]