From 5ad9753fda3cb464b3d555bc2e56ad1d30647328 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Sat, 15 Jan 2022 12:33:49 -0800 Subject: [PATCH] fix: guard node_modules roots for dynamic multi-linked npm deps (#3248) --- internal/node/node.bzl | 21 +++++++++---------- internal/providers/node_runtime_deps_info.bzl | 16 +++++++++++++- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 063e9b3fcc..c6d732d49f 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -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", "") @@ -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): diff --git a/internal/providers/node_runtime_deps_info.bzl b/internal/providers/node_runtime_deps_info.bzl index a7a85a1a99..96226905b0 100644 --- a/internal/providers/node_runtime_deps_info.bzl +++ b/internal/providers/node_runtime_deps_info.bzl @@ -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( @@ -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", "") @@ -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):