Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs_binary failed to resolve transitive @npm dependencies #443

Closed
ashi009 opened this issue Dec 3, 2018 · 6 comments
Closed

nodejs_binary failed to resolve transitive @npm dependencies #443

ashi009 opened this issue Dec 3, 2018 · 6 comments
Labels

Comments

@ashi009
Copy link
Contributor

ashi009 commented Dec 3, 2018

I'm trying to create a nodejs_binary target with ts_library outputs with the latest nodejs/typescript rules releases.

    git_repository(
        name = "build_bazel_rules_nodejs",
        remote = "https://github.com/bazelbuild/rules_nodejs.git",
        tag = "0.16.2",  # latest as of 2018-11-27
    )

    git_repository(
        name = "build_bazel_rules_typescript",
        remote = "https://github.com/bazelbuild/rules_typescript",
        # After modifying the version below, use
        # `bazel run @nodejs//:yarn -- add @bazel/typescript@<version> --dev`
        # to update package.json and yarn.lock.
        tag = "0.21.0",  # latest as of 2018-11-27
    )

//test/index.ts

import {Map} from 'google-protobuf';

console.log(Map);

//test/BUILD.bazel

load("@build_bazel_rules_typescript//:defs.bzl", "ts_devserver", "ts_library")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")

ts_library(
    name = "test",
    srcs = ["index.ts"],
    deps = ["@npm//google-protobuf"],
)

nodejs_binary(
    name = "main",
    data = [":test"],
    entry_point = "ws/test",
)

Output from bazel run

INFO: Analysed target //test:main (0 packages loaded, 0 targets configured).
INFO: Found 1 target...
Target //test:main up-to-date:
  bazel-bin/test/main_bin.sh
  bazel-bin/test/main
INFO: Elapsed time: 0.159s, Critical Path: 0.00s, Remote (0.00% of the time): [queue: 0.00%, setup: 0.00%, process: 0.00%]
INFO: 0 processes.
INFO: Build completed successfully, 1 total action
INFO: Build completed successfully, 1 total action

node_loader: running //test:main_bin with
  MODULE_ROOTS: []
  BOOTSTRAP: []
  NODE_MODULES_ROOT: build_bazel_rules_nodejs/node_modules
  BIN_DIR: bazel-out/darwin-fastbuild/bin
  GEN_DIR: bazel-out/darwin-fastbuild/genfiles

node_loader: resolve source-map-support from /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main_bin_loader.js
node_loader: try to resolve in runfiles /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/source-map-support
node_loader: try to resolve in runfiles /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/build_bazel_rules_nodejs/node_modules/source-map-support
WARNING: source-map-support module not installed.
    Stack traces from languages like TypeScript will point to generated .js files.
    Set install_source_map_support = False in //test:main_bin to turn off this warning.

node_loader: resolve ws/test from undefined
node_loader: try to resolve in runfiles /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test
node_loader: resolved via directory /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index
node_loader: resolved ws/test within runfiles to /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js from undefined
node_loader: resolve google-protobuf from /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js
node_loader: try to resolve in runfiles /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/google-protobuf
node_loader: try to resolve in runfiles /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/build_bazel_rules_nodejs/node_modules/google-protobuf
Error: //test:main_bin cannot find module 'google-protobuf' required by '/private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js'
  looked in:
   built-in, relative, absolute, nested node_modules - Error: Not a built-in module, relative or absolute import
,
   runfiles - Error: Cannot find module '/private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/google-protobuf'
,
   node_modules attribute (build_bazel_rules_nodejs/node_modules) - Error: Cannot find module '/private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/build_bazel_rules_nodejs/node_modules/google-protobuf'

    at Function.module.constructor._resolveFilename (/private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main_bin_loader.js:454:17)
    at Function.Module._load (internal/modules/cjs/loader.js:506:25)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js:12:29
    at /private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js:3:17
    at Object.<anonymous> (/private/var/tmp/_bazel_xiaoyi/2258f168242f85dcc9f2c0d3e9d2258a/execroot/ws/bazel-out/darwin-fastbuild/bin/test/main.runfiles/ws/test/index.js:9:3)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Module.load (internal/modules/cjs/loader.js:598:32)

WARNING: Due to a breaking change in rules_nodejs 0.13.0, target //test:main_bin
must now declare either an explicit node_modules attribute, or
list explicit deps[] or data[] fine grained dependencies on npm labels
if it has any node_modules dependencies.
See https://github.com/bazelbuild/rules_nodejs/wiki#migrating-to-rules_nodejs-013
@ashi009
Copy link
Contributor Author

ashi009 commented Dec 3, 2018

The workaround I have ATM is to add a @npm//something dependency to the data attribute, which will make node.bzl to infer a correct node_modules_root to use.

@alexeagle alexeagle added the bug label Dec 4, 2018
@gregmagolan
Copy link
Collaborator

@alexeagle Correct me if I'm wrong but if the npm package google-protobuf is a runtime dependency of the nodejs_binary rule then it needs to be in the data attribute. Or do we want to pick up all transitive @npm dependencies downstream in the nodejs_binary rule? Could cause an issue with multiple roots if different upstream rules use different node_modules.

@fstephany
Copy link

I face the same issue with a ts_library (dependent on @npm//uuid) used from a nodejs_binary.
It seems that the rules looks in the wrong directory:

Error: Cannot find module 'uuid'
    runfiles - Error: Cannot find module '/private/var/tmp/_bazel_fstephany/2568291785a61e5fb8e927e31fac1df5/execroot/demo/bazel-out/darwin-fastbuild/bin/app/program.runfiles/uuid'
    node_modules attribute (build_bazel_rules_nodejs/node_modules) - Error: Cannot find module '/private/var/tmp/_bazel_fstephany/2568291785a61e5fb8e927e31fac1df5/execroot/demo/bazel-out/darwin-fastbuild/bin/app/program.runfiles/build_bazel_rules_nodejs/node_modules/uuid'

while the module is in

`bazel-out/darwin-fastbuild/bin/app/program.runfiles/npm/node_modules/uuid

Adding the dep in the nodejs_binary rules fixes the issue:

nodejs_binary(
  name = "program",
  data = [
    ":app", 
    "@npm//uuid" 
  ],
  entry_point = "demo/app/main.js",
)

@ashi009 Did you find a better way to solve this? Manually adding all the deps on data attribute of nodejs_binary feels a bit cumbersome.

@fstephany
Copy link

fstephany commented Apr 17, 2019

It seems that 0.27.12 solved this for me. I can now build without specifying the @npm//uuid dependency in the nodejs_binary. Thanks @alexeagle !

ts_library(
  name = "cli",
  srcs = ["main.ts"],
  deps = ["//greeter"],
)

# combine the individual modules into a single file
rollup_bundle(
  name = "bundle",
  entry_point = "cli/main.js",
  deps = [":cli"],
)

nodejs_binary(
  name = "app",
  data = [
    ":cli", 
  ],
  entry_point = "demo/cli/main.js",
)

@alexeagle
Copy link
Collaborator

Great, I didn't even think about nodejs_binary also now receiving the transitive data

@fstephany
Copy link

Lucky fix then ;)

alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 17, 2020
We used a depset for a collection which is only used within the context of a single target: the files[] block in the generated tsconfig. As documented on https://docs.bazel.build/versions/master/skylark/depsets.html:
"If you don’t need the merge operation, consider using another type, such as list or dict."

The problem is exposed by --incompatible_depset_is_not_iterable which fails at the spot where we iterated the depset of files, showing that it should have been a list.

Also clean up a warning about the deprecated load of jasmine_node_test

See bazel-contrib#443

Closes bazel-contrib#451

PiperOrigin-RevId: 249758830
alexeagle added a commit to alexeagle/rules_nodejs that referenced this issue Oct 18, 2020
We used a depset for a collection which is only used within the context of a single target: the files[] block in the generated tsconfig. As documented on https://docs.bazel.build/versions/master/skylark/depsets.html:
"If you don’t need the merge operation, consider using another type, such as list or dict."

The problem is exposed by --incompatible_depset_is_not_iterable which fails at the spot where we iterated the depset of files, showing that it should have been a list.

Also clean up a warning about the deprecated load of jasmine_node_test

See bazel-contrib#443

Closes bazel-contrib#451

PiperOrigin-RevId: 249758830
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants