Skip to content

Commit

Permalink
fix: guard node_modules roots for dynamic multi-linked npm deps (#3248)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Jan 15, 2022
1 parent 4fd864c commit 5ad9753
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
21 changes: 10 additions & 11 deletions internal/node/node.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def _compute_node_modules_roots(ctx, data):
"""Computes the node_modules root (if any) from data attribute."""
node_modules_roots = {}

# Add in roots from third-party deps
# Add in roots from non-exports_diretories_only npm deps
for d in data:
if ExternalNpmPackageInfo in d:
path = getattr(d[ExternalNpmPackageInfo], "path", "")
Expand All @@ -56,17 +56,16 @@ def _compute_node_modules_roots(ctx, data):
fail("All npm dependencies at the path '%s' must come from a single workspace. Found '%s' and '%s'." % (path, other_workspace, workspace))
node_modules_roots[path] = workspace

# Add in roots for multi-linked first party deps
# Add in roots for multi-linked npm deps
for dep in data:
if not LinkerPackageMappingInfo in dep:
continue

for k, v in dep[LinkerPackageMappingInfo].mappings.items():
map_key_split = k.split(":")
package_name = map_key_split[0]
package_path = map_key_split[1] if len(map_key_split) > 1 else ""
if package_path not in node_modules_roots:
node_modules_roots[package_path] = ""
if LinkerPackageMappingInfo in dep:
for k, v in dep[LinkerPackageMappingInfo].mappings.items():
map_key_split = k.split(":")
package_name = map_key_split[0]
package_path = map_key_split[1] if len(map_key_split) > 1 else ""
if package_path not in node_modules_roots:
node_modules_roots[package_path] = ""

return node_modules_roots

def _write_require_patch_script(ctx, data, node_modules_root):
Expand Down
16 changes: 15 additions & 1 deletion internal/providers/node_runtime_deps_info.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
load("@bazel_skylib//lib:types.bzl", "types")
load("@rules_nodejs//nodejs:providers.bzl", "StampSettingInfo")
load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles")
load("//internal/linker:link_node_modules.bzl", "add_arg", "write_node_modules_manifest")
load("//internal/linker:link_node_modules.bzl", "LinkerPackageMappingInfo", "add_arg", "write_node_modules_manifest")
load("//internal/providers:external_npm_package_info.bzl", "ExternalNpmPackageInfo")

NodeRuntimeDepsInfo = provider(
Expand Down Expand Up @@ -45,11 +45,14 @@ do the same.
def _compute_node_modules_roots(ctx):
"""Computes the node_modules root (if any) from data & deps targets."""
node_modules_roots = {}

deps = []
if hasattr(ctx.attr, "data"):
deps += ctx.attr.data
if hasattr(ctx.attr, "deps"):
deps += ctx.attr.deps

# Add in roots from non-exports_diretories_only npm deps
for d in deps:
if ExternalNpmPackageInfo in d:
path = getattr(d[ExternalNpmPackageInfo], "path", "")
Expand All @@ -59,6 +62,17 @@ def _compute_node_modules_roots(ctx):
if other_workspace != workspace:
fail("All npm dependencies at the path '%s' must come from a single workspace. Found '%s' and '%s'." % (path, other_workspace, workspace))
node_modules_roots[path] = workspace

# Add in roots for multi-linked npm deps
for dep in deps:
if LinkerPackageMappingInfo in dep:
for k, v in dep[LinkerPackageMappingInfo].mappings.items():
map_key_split = k.split(":")
package_name = map_key_split[0]
package_path = map_key_split[1] if len(map_key_split) > 1 else ""
if package_path not in node_modules_roots:
node_modules_roots[package_path] = ""

return node_modules_roots

def run_node(ctx, inputs, arguments, executable, chdir = None, **kwargs):
Expand Down

0 comments on commit 5ad9753

Please sign in to comment.