rules_nodejs icon indicating copy to clipboard operation
rules_nodejs copied to clipboard

"duplicate module mapping" with ts_project and nodejs_binary

Open jfirebaugh opened this issue 3 years ago • 2 comments

🐞 bug report

Affected Rule

The issue is caused by the rules: ts_project and nodejs_binary.

Is this a regression?

No

Description

When you have:

  • Two ts_projects which both depend on an npm module (say @types/node), possibly transitively
  • The second ts_project depends on the first
  • A nodejs_binary which depends on the second ts_project

Then when you attempt to build or run the nodejs_binary, bazel produces a "duplicate module mapping" error.

🔬 Minimal Reproduction

https://github.com/jfirebaugh/rules_nodejs_duplicate_module_mapping

🔥 Exception or Error


$ bazel build //b:b
ERROR: /Users/john/figma/rules_rust_duplicate_module_mapping/b/BUILD.bazel:4:11: in @build_bazel_rules_nodejs//internal/common:module_mappings.bzl%module_mappings_runtime_aspect aspect on ts_project rule //b:tsc:
Traceback (most recent call last):
	File "/private/var/tmp/_bazel_johnfirebaugh/2eba95c510d0d05d3c27e1afb2df478f/external/build_bazel_rules_nodejs/internal/common/module_mappings.bzl", line 106, column 36, in _module_mappings_runtime_aspect_impl
		mappings = _get_module_mappings(target, ctx)
	File "/private/var/tmp/_bazel_johnfirebaugh/2eba95c510d0d05d3c27e1afb2df478f/external/build_bazel_rules_nodejs/internal/common/module_mappings.bzl", line 67, column 21, in _get_module_mappings
		fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
Error in fail: duplicate module mapping at //b:tsc: @types/node maps to both npm_a/node_modules/@types/node and npm_b/node_modules/@types/node deps
ERROR: Analysis of target '//b:b' failed; build aborted:
INFO: Elapsed time: 7.890s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (53 packages loaded, 452 targets configured)

🌍 Your Environment

Operating System:

macOS

Output of bazel version:

  
Bazelisk version: development
Build label: 5.1.1
Build target: bazel-out/darwin-opt/bin/src/main/java/com/google/devtools/build/lib/bazel/BazelServer_deploy.jar
Build time: Fri Apr 8 15:57:36 2022 (1649433456)
Build timestamp: 1649433456
Build timestamp as int: 1649433456
  

Rules_nodejs version:

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

  
5.4.2
  

Anything else relevant?

I encountered this with @types/node, which is a common dependency for TypeScript CLI applications, but it presumably happens whenever the two ts_projects share a module dependency. Note that in this example, b does not even depend directly on @types/node -- it's a transitive dependency of @types/glob.

Bazel builds //b:tsc successfully. It's only the nodejs_binary target that produces the error. Which is confounding because @types packages are not even used at runtime. But even if it wasn't a @types package, I think rules_nodejs should be able to handle this scenario.

Node.js itself can handle this situation: when a file in a uses require, node searches in a/node_modules. Similarly for b and b/node_modules. There's no requirement that a/node_modules and b/node_modules are disjoint.

jfirebaugh avatar May 10 '22 21:05 jfirebaugh

I've also tried indirecting the b -> a dependency through js_library. This produces the same error.

jfirebaugh avatar May 10 '22 22:05 jfirebaugh

I got this same issue when attempting to upgrade bazel esbuild etc https://github.com/Zemnmez/monorepo/pull/262

Zemnmez avatar May 26 '22 00:05 Zemnmez

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 Nov 25 '22 02:11 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 Dec 26 '22 02:12 github-actions[bot]

@jfirebaugh any idea if this can be reproduced with aspect_rules_js / aspect_rules_ts ?

alexeagle avatar Dec 27 '22 16:12 alexeagle

I haven't seen it. I think closing this issue is appropriate.

jfirebaugh avatar Dec 27 '22 20:12 jfirebaugh

Our team is having the exact same issues, also with esbuild

   !   bazel run //services/portal:dev                                                               ~/Code/core testeranto4 -!
INFO: Build option --action_env has changed, discarding analysis cache.
ERROR: /Users/adam/Code/core/services/component-library/BUILD.bazel:46:11: in @build_bazel_rules_nodejs//internal/common:module_mappings.bzl%module_mappings_runtime_aspect aspect on _js_library rule //services/component-library:components: 
Traceback (most recent call last):
        File "/private/var/tmp/_bazel_adam/5be3520a29293765ab5fb28ebee5483d/external/build_bazel_rules_nodejs/internal/common/module_mappings.bzl", line 106, column 36, in _module_mappings_runtime_aspect_impl
                mappings = _get_module_mappings(target, ctx)
        File "/private/var/tmp/_bazel_adam/5be3520a29293765ab5fb28ebee5483d/external/build_bazel_rules_nodejs/internal/common/module_mappings.bzl", line 67, column 21, in _get_module_mappings
                fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
Error in fail: duplicate module mapping at //services/component-library:components: esbuild maps to both npm/node_modules/esbuild and esbuild_npm/node_modules/esbuild deps
ERROR: Analysis of target '//services/portal:dev' failed; build aborted: 
INFO: Elapsed time: 2.099s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (1359 packages loaded, 6532 targets configured)
FAILED: Build did NOT complete successfully (1359 packages loaded, 6532 targets configured)

adamwong246 avatar Feb 27 '23 02:02 adamwong246