Skip to content

Commit 110e00e

Browse files
committed
fix: allow "src" and "bin" module mappings to win over "runfiles"
This fixes an issue in the Angular repo where the duplicate `runfiles` mappings: ``` ["runfiles", "angular/packages/compiler"] and ["bin", "angular/packages/compiler"] ``` sneaks through this nodejs_binaries: ``` nodejs_binary( name = "ngc_bin", data = [ "//packages/compiler-cli", "@npm//chokidar", "@npm//reflect-metadata", ], entry_point = "//packages/compiler-cli:src/main.ts", ) nodejs_binary( name = "ng_xi18n", data = [ "//packages/compiler-cli", "@npm//chokidar", "@npm//reflect-metadata", ], entry_point = "//packages/compiler-cli:src/extract_i18n.ts", ) ``` which depends on `//packages/compiler-cli` which depends on `//packages/compiler`. So when building the ng_test target: ``` nodejs_test( name = "integrationtest", data = [ ":ngc_bin", ":ng_xi18n", "@nodejs//:node", "@npm//domino", "@npm//chokidar", "@npm//source-map-support", "@npm//shelljs", "@npm//typescript", "@npm//reflect-metadata", "@npm//rxjs", "@npm//tslib", "@npm//jasmine/bin:jasmine", "@npm//xhr2", "@npm//@types/node", "@npm//@types/jasmine", # we need to reference zone.d.ts typing file from zone.js build target # instead of npm because angular repo will not depends on npm zone.js # any longer. "//packages/zone.js/lib:zone_d_ts", # we need to reference zone.js npm_package build target # instead of npm because angular repo will not depends on npm zone.js # any longer, so we need to build a zone.js npm release first. "//packages/zone.js:npm_package", "//packages/animations:npm_package", "//packages/common:npm_package", "//packages/compiler:npm_package", "//packages/compiler-cli:npm_package", "//packages/core:npm_package", "//packages/forms:npm_package", "//packages/http:npm_package", "//packages/platform-browser:npm_package", "//packages/platform-browser-dynamic:npm_package", "//packages/platform-server:npm_package", "//packages/router:npm_package", ] + glob(["**/*"]), entry_point = "test.js", tags = ["no-ivy-aot"], ) ``` it has both mappings causing the linker to error out with conflicting module mappings `["runfiles", "angular/packages/compiler"] and ["bin", "angular/packages/compiler"]`. Fix is to just allow this and allow both "src" and "bin" to win a conflicting over "runfiles" mapping.
1 parent 765914e commit 110e00e

1 file changed

Lines changed: 24 additions & 17 deletions

File tree

internal/linker/link_node_modules.bzl

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ def add_arg(args, arg):
3434
else:
3535
args.add(arg)
3636

37+
def _link_mapping(label, mappings, k, v):
38+
if k in mappings and mappings[k] != v:
39+
if v[1] == mappings[k][1]:
40+
# Allow "src" and "bin" to win over "runfiles"
41+
# For example,
42+
# mappings[k] = ["runfiles", "angular/packages/compiler"]
43+
# v = ["bin", "angular/packages/compiler"]
44+
if mappings[k][0] == "runfiles":
45+
return True
46+
elif v[0] != "runfiles":
47+
return False
48+
fail(("conflicting mapping at %s: %s maps to both %s and %s" % (label, k, mappings[k], v)), "deps")
49+
else:
50+
return True
51+
3752
def write_node_modules_manifest(ctx, extra_data = []):
3853
"""Writes a manifest file read by the linker, containing info about resolving runtime dependencies
3954
@@ -62,11 +77,9 @@ def write_node_modules_manifest(ctx, extra_data = []):
6277

6378
# ...first-party packages to be linked into the node_modules tree
6479
for k, v in getattr(dep, _ASPECT_RESULT_NAME, {}).items():
65-
if k in mappings and mappings[k] != v:
66-
fail(("conflicting module mapping at %s: %s maps to both %s and %s" %
67-
(dep.label, k, mappings[k], v)), "deps")
68-
_debug(ctx.var, "Linking %s: %s" % (k, v))
69-
mappings[k] = v
80+
if _link_mapping(dep.label, mappings, k, v):
81+
_debug(ctx.var, "Linking %s: %s" % (k, v))
82+
mappings[k] = v
7083

7184
# Write the result to a file, and use the magic node option --bazel_node_modules_manifest
7285
# The launcher.sh will peel off this argument and pass it to the linker rather than the program.
@@ -102,11 +115,6 @@ def get_module_mappings(label, attrs, vars, rule_kind, srcs = [], workspace_name
102115
for name in _MODULE_MAPPINGS_DEPS_NAMES:
103116
for dep in getattr(attrs, name, []):
104117
for k, v in getattr(dep, _ASPECT_RESULT_NAME, {}).items():
105-
if k in mappings and mappings[k] != v:
106-
fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
107-
(label, k, mappings[k], v)), "deps")
108-
_debug(vars, "target %s propagating module mapping %s: %s" % (dep, k, v))
109-
110118
# A package which was reachable transitively via a *_binary or *_test
111119
# rule is assumed to be in the runfiles of that binary,
112120
# so we switch the linker target root.
@@ -115,9 +123,10 @@ def get_module_mappings(label, attrs, vars, rule_kind, srcs = [], workspace_name
115123
# but it seems that this info is only available in Bazel Java internals.
116124
# TODO: revisit whether this is the correct condition after downstream testing
117125
if rule_kind.endswith("_binary") or rule_kind.endswith("_test"):
118-
mappings[k] = ["runfiles", v[1]]
119-
else:
126+
v = ["runfiles", v[1]]
127+
if _link_mapping(label, mappings, k, v):
120128
mappings[k] = v
129+
_debug(vars, "target %s propagating module mapping %s: %s" % (dep, k, v))
121130

122131
if not getattr(attrs, "module_name", None) and not getattr(attrs, "module_root", None):
123132
# No mappings contributed here, short-circuit with the transitive ones we collected
@@ -141,12 +150,10 @@ def get_module_mappings(label, attrs, vars, rule_kind, srcs = [], workspace_name
141150
else:
142151
mr = ["bin", mr]
143152

144-
if mn in mappings and mappings[mn] != mr:
145-
fail(("duplicate module mapping at %s: %s maps to both %s and %s" %
146-
(label, mn, mappings[mn], mr)), "deps")
147-
_debug(vars, "target %s adding module mapping %s: %s" % (label, mn, mr))
153+
if _link_mapping(label, mappings, mn, mr):
154+
_debug(vars, "target %s adding module mapping %s: %s" % (label, mn, mr))
155+
mappings[mn] = mr
148156

149-
mappings[mn] = mr
150157
return mappings
151158

152159
# When building a mapping for use at runtime, we need paths to be relative to

0 commit comments

Comments
 (0)