rules_nodejs icon indicating copy to clipboard operation
rules_nodejs copied to clipboard

nodejs_binary fails to resolve workspace absolute imports / require like my_wksp/path/to/file

Open smnrd opened this issue 5 years ago • 5 comments

🐞 bug report

Affected Rule

The issue is caused by the rule: nodejs_binary

Is this a regression?

Yes, the previous version in which this bug was not present was: 3.8.0

Description

nodejs_binary fails to resolve workspace absolute imports / require like my_wksp/path/to/file (as described in nodejs_binary docs )

🔬 Minimal Reproduction

https://github.com/smnrd/repro-nodejs-binary-absolute-import-failure.git

🔥 Exception or Error


Error: Cannot find module 'mywksp/mod'
Require stack:
- /home/simon/.cache/bazel/_bazel_simon/389881ea9aac3e360f57a8c0a45336a7/execroot/mywksp/bazel-out/k8-fastbuild/bin/main/main.sh.runfiles/mywksp/main/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15)
    at Function.Module._load (internal/modules/cjs/loader.js:745:27)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object. (home/simon/.cache/bazel/_bazel_simon/389881ea9aac3e360f57a8c0a45336a7/execroot/mywksp/bazel-out/k8-fastbuild/bin/main/index.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)

🌍 Your Environment

Operating System:

  
Debian Linux on WSL2
  

Output of bazel version:

  
Bazelisk version: v1.9.0
Build label: 4.2.1
Build target: bazel-out/k8-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Mon Aug 30 15:17:47 2021 (1630336667)
Build timestamp: 1630336667
Build timestamp as int: 1630336667
  

Rules_nodejs version:

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

  
4.0.0
  

Anything else relevant?

smnrd avatar Sep 02 '21 18:09 smnrd

Interesting I just noticed the same error updating rules_nodejs to bazel 4.2.0 on windows https://buildkite.com/bazel/rules-nodejs-nodejs/builds/10480#960b3490-5840-4961-b9a4-3de9a8fa633c but no error on ubuntu. Not sure if it's related.

alexeagle avatar Sep 10 '21 00:09 alexeagle

I think this is the same issue as https://github.com/bazelbuild/rules_nodejs/issues/2970

Zemnmez avatar Sep 27 '21 21:09 Zemnmez

Adding on to this, I also ran into this with a very basic nodejs_binary project running on ubuntu 16. The ts_project build rule seems to build fine but bazel run app leads to Error: Cannot find module 'wksp/file2'

ts_project(
    name = "main",
    tsconfig = "tsconfig.json",
    srcs = ["file1.ts", "file2.ts"],
    link_workspace_root = True,
    declaration = True,
)

nodejs_binary(
    name = "app",
    entry_point = "file1.ts",
    data = [":main"],
    link_workspace_root = True,
)
// file1.ts

import { sayHi } from "wksp/file2";

sayHi();
// file2.ts

export function sayHi() {
  console.log("hi");
}

noveyak avatar Oct 12 '21 19:10 noveyak

I found a fix that works for us. I just submitted it a PR. In case someone need it before the PR is merged, here is the patch (for release 4.4.5, to be used in the WORKSPACE file with build_bazel_rules_nodejs http_archive as explained here).

smnrd avatar Nov 16 '21 16:11 smnrd

Fixed by #3093 .

smnrd avatar Dec 08 '21 20:12 smnrd