Skip to content

Commit a13f2b6

Browse files
authored
fix: @npm//foobar:foobar__files target no longer includes nested node_modules (#1390)
* fix: @npm//foobar:foobar__files target no longer includes nested node_modules * refactor: make npm targets `foo__nested_node_modules`, `foo__all_files` & `foo__contents` private
1 parent bfb4b10 commit a13f2b6

12 files changed

Lines changed: 205 additions & 75 deletions

File tree

internal/npm_install/generate_build_file.js

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -172,10 +172,10 @@ for installation instructions.`);
172172
`;
173173
});
174174
});
175-
let srcsStarlark = '';
175+
let filesStarlark = '';
176176
if (pkgs.length) {
177-
const list = pkgs.map(pkg => `"//${pkg._dir}:${pkg._name}__files",`).join('\n ');
178-
srcsStarlark = `
177+
const list = pkgs.map(pkg => `"//${pkg._dir}:${pkg._name}__all_files",`).join('\n ');
178+
filesStarlark = `
179179
# direct sources listed for strict deps support
180180
srcs = [
181181
${list}
@@ -201,7 +201,7 @@ ${exportsStarlark}])
201201
# there are many files in target.
202202
# See https://github.com/bazelbuild/bazel/issues/5153.
203203
node_module_library(
204-
name = "node_modules",${srcsStarlark}${depsStarlark}
204+
name = "node_modules",${filesStarlark}${depsStarlark}
205205
)
206206
207207
`;
@@ -775,6 +775,8 @@ def _maybe(repo_rule, name, **kwargs):
775775
*/
776776
function printPackage(pkg) {
777777
const sources = filterFiles(pkg._files, INCLUDED_FILES);
778+
const files = sources.filter((f) => !f.startsWith('node_modules/'));
779+
const nestedNodeModules = sources.filter((f) => f.startsWith('node_modules/'));
778780
const dtsSources = filterFiles(pkg._files, ['.d.ts']);
779781
// TODO(gmagolan): add UMD & AMD scripts to scripts even if not an APF package _but_ only if they
780782
// are named?
@@ -788,12 +790,21 @@ def _maybe(repo_rule, name, **kwargs):
788790
${namedSources.map((f) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
789791
],`;
790792
}
791-
let srcsStarlark = '';
792-
if (sources.length) {
793-
srcsStarlark = `
793+
let filesStarlark = '';
794+
if (files.length) {
795+
filesStarlark = `
794796
# ${pkg._dir} package files (and files in nested node_modules)
795797
srcs = [
796-
${sources.map((f) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
798+
${files.map((f) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
799+
],`;
800+
}
801+
let nestedNodeModulesStarlark = '';
802+
if (nestedNodeModules.length) {
803+
nestedNodeModulesStarlark = `
804+
# ${pkg._dir} package files (and files in nested node_modules)
805+
srcs = [
806+
${nestedNodeModules.map((f) => `"//:node_modules/${pkg._dir}/${f}",`)
807+
.join('\n ')}
797808
],`;
798809
}
799810
let depsStarlark = '';
@@ -819,20 +830,32 @@ def _maybe(repo_rule, name, **kwargs):
819830
${printJson(pkg)}
820831
821832
filegroup(
822-
name = "${pkg._name}__files",${srcsStarlark}
833+
name = "${pkg._name}__files",${filesStarlark}
834+
)
835+
836+
filegroup(
837+
name = "${pkg._name}__nested_node_modules",${nestedNodeModulesStarlark}
838+
visibility = ["//visibility:private"],
839+
)
840+
841+
filegroup(
842+
name = "${pkg._name}__all_files",
843+
srcs = [":${pkg._name}__files", ":${pkg._name}__nested_node_modules"],
844+
visibility = ["//:__subpackages__"],
823845
)
824846
825847
node_module_library(
826848
name = "${pkg._name}",
827849
# direct sources listed for strict deps support
828-
srcs = [":${pkg._name}__files"],${depsStarlark}
850+
srcs = [":${pkg._name}__all_files"],${depsStarlark}
829851
)
830852
831853
# ${pkg._name}__contents target is used as dep for main targets to prevent
832854
# circular dependencies errors
833855
node_module_library(
834856
name = "${pkg._name}__contents",
835-
srcs = [":${pkg._name}__files"],${namedSourcesStarlark}
857+
srcs = [":${pkg._name}__all_files"],${namedSourcesStarlark}
858+
visibility = ["//:__subpackages__"],
836859
)
837860
838861
# ${pkg._name}__typings is the subset of ${pkg._name}__contents that are declarations
@@ -987,10 +1010,10 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
9871010
});
9881011
// filter out duplicate deps
9891012
deps = [...pkgs, ...new Set(deps)];
990-
let srcsStarlark = '';
1013+
let filesStarlark = '';
9911014
if (deps.length) {
992-
const list = deps.map(dep => `"//${dep._dir}:${dep._name}__files",`).join('\n ');
993-
srcsStarlark = `
1015+
const list = deps.map(dep => `"//${dep._dir}:${dep._name}__all_files",`).join('\n ');
1016+
filesStarlark = `
9941017
# direct sources listed for strict deps support
9951018
srcs = [
9961019
${list}
@@ -1009,7 +1032,7 @@ def ${name.replace(/-/g, '_')}_test(**kwargs):
10091032
10101033
# Generated target for npm scope ${scope}
10111034
node_module_library(
1012-
name = "${scope}",${srcsStarlark}${depsStarlark}
1035+
name = "${scope}",${filesStarlark}${depsStarlark}
10131036
)
10141037
10151038
`;

internal/npm_install/generate_build_file.ts

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -176,10 +176,10 @@ function generateRootBuildFile(pkgs: Dep[]) {
176176
`;
177177
})});
178178

179-
let srcsStarlark = '';
179+
let filesStarlark = '';
180180
if (pkgs.length) {
181-
const list = pkgs.map(pkg => `"//${pkg._dir}:${pkg._name}__files",`).join('\n ');
182-
srcsStarlark = `
181+
const list = pkgs.map(pkg => `"//${pkg._dir}:${pkg._name}__all_files",`).join('\n ');
182+
filesStarlark = `
183183
# direct sources listed for strict deps support
184184
srcs = [
185185
${list}
@@ -207,7 +207,7 @@ ${exportsStarlark}])
207207
# there are many files in target.
208208
# See https://github.com/bazelbuild/bazel/issues/5153.
209209
node_module_library(
210-
name = "node_modules",${srcsStarlark}${depsStarlark}
210+
name = "node_modules",${filesStarlark}${depsStarlark}
211211
)
212212
213213
`
@@ -859,6 +859,8 @@ function findFile(pkg: Dep, m: string) {
859859
*/
860860
function printPackage(pkg: Dep) {
861861
const sources = filterFiles(pkg._files, INCLUDED_FILES);
862+
const files = sources.filter((f: string) => !f.startsWith('node_modules/'));
863+
const nestedNodeModules = sources.filter((f: string) => f.startsWith('node_modules/'));
862864
const dtsSources = filterFiles(pkg._files, ['.d.ts']);
863865
// TODO(gmagolan): add UMD & AMD scripts to scripts even if not an APF package _but_ only if they
864866
// are named?
@@ -874,12 +876,23 @@ function printPackage(pkg: Dep) {
874876
],`;
875877
}
876878

877-
let srcsStarlark = '';
878-
if (sources.length) {
879-
srcsStarlark = `
879+
let filesStarlark = '';
880+
if (files.length) {
881+
filesStarlark = `
880882
# ${pkg._dir} package files (and files in nested node_modules)
881883
srcs = [
882-
${sources.map((f: string) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
884+
${files.map((f: string) => `"//:node_modules/${pkg._dir}/${f}",`).join('\n ')}
885+
],`;
886+
}
887+
888+
let nestedNodeModulesStarlark = '';
889+
if (nestedNodeModules.length) {
890+
nestedNodeModulesStarlark = `
891+
# ${pkg._dir} package files (and files in nested node_modules)
892+
srcs = [
893+
${
894+
nestedNodeModules.map((f: string) => `"//:node_modules/${pkg._dir}/${f}",`)
895+
.join('\n ')}
883896
],`;
884897
}
885898

@@ -909,20 +922,32 @@ function printPackage(pkg: Dep) {
909922
${printJson(pkg)}
910923
911924
filegroup(
912-
name = "${pkg._name}__files",${srcsStarlark}
925+
name = "${pkg._name}__files",${filesStarlark}
926+
)
927+
928+
filegroup(
929+
name = "${pkg._name}__nested_node_modules",${nestedNodeModulesStarlark}
930+
visibility = ["//visibility:private"],
931+
)
932+
933+
filegroup(
934+
name = "${pkg._name}__all_files",
935+
srcs = [":${pkg._name}__files", ":${pkg._name}__nested_node_modules"],
936+
visibility = ["//:__subpackages__"],
913937
)
914938
915939
node_module_library(
916940
name = "${pkg._name}",
917941
# direct sources listed for strict deps support
918-
srcs = [":${pkg._name}__files"],${depsStarlark}
942+
srcs = [":${pkg._name}__all_files"],${depsStarlark}
919943
)
920944
921945
# ${pkg._name}__contents target is used as dep for main targets to prevent
922946
# circular dependencies errors
923947
node_module_library(
924948
name = "${pkg._name}__contents",
925-
srcs = [":${pkg._name}__files"],${namedSourcesStarlark}
949+
srcs = [":${pkg._name}__all_files"],${namedSourcesStarlark}
950+
visibility = ["//:__subpackages__"],
926951
)
927952
928953
# ${pkg._name}__typings is the subset of ${pkg._name}__contents that are declarations
@@ -1099,10 +1124,10 @@ function printScope(scope: string, pkgs: Dep[]) {
10991124
// filter out duplicate deps
11001125
deps = [...pkgs, ...new Set(deps)];
11011126

1102-
let srcsStarlark = '';
1127+
let filesStarlark = '';
11031128
if (deps.length) {
1104-
const list = deps.map(dep => `"//${dep._dir}:${dep._name}__files",`).join('\n ');
1105-
srcsStarlark = `
1129+
const list = deps.map(dep => `"//${dep._dir}:${dep._name}__all_files",`).join('\n ');
1130+
filesStarlark = `
11061131
# direct sources listed for strict deps support
11071132
srcs = [
11081133
${list}
@@ -1123,7 +1148,7 @@ function printScope(scope: string, pkgs: Dep[]) {
11231148
11241149
# Generated target for npm scope ${scope}
11251150
node_module_library(
1126-
name = "${scope}",${srcsStarlark}${depsStarlark}
1151+
name = "${scope}",${filesStarlark}${depsStarlark}
11271152
)
11281153
11291154
`;

internal/npm_install/test/golden/@angular/core/BUILD.bazel.golden

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -664,9 +664,18 @@ filegroup(
664664
"//:node_modules/@angular/core/testing/testing.metadata.json",
665665
],
666666
)
667+
filegroup(
668+
name = "core__nested_node_modules",
669+
visibility = ["//visibility:private"],
670+
)
671+
filegroup(
672+
name = "core__all_files",
673+
srcs = [":core__files", ":core__nested_node_modules"],
674+
visibility = ["//:__subpackages__"],
675+
)
667676
node_module_library(
668677
name = "core",
669-
srcs = [":core__files"],
678+
srcs = [":core__all_files"],
670679
deps = [
671680
"//@angular/core:core__contents",
672681
"//tslib:tslib__contents",
@@ -676,11 +685,12 @@ node_module_library(
676685
)
677686
node_module_library(
678687
name = "core__contents",
679-
srcs = [":core__files"],
688+
srcs = [":core__all_files"],
680689
named_module_srcs = [
681690
"//:node_modules/@angular/core/bundles/core-testing.umd.js",
682691
"//:node_modules/@angular/core/bundles/core.umd.js",
683692
],
693+
visibility = ["//:__subpackages__"],
684694
)
685695
node_module_library(
686696
name = "core__typings",

internal/npm_install/test/golden/@gregmagolan/BUILD.bazel.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ load("@build_bazel_rules_nodejs//internal/npm_install:node_module_library.bzl",
44
node_module_library(
55
name = "@gregmagolan",
66
srcs = [
7-
"//@gregmagolan/test-a:test-a__files",
8-
"//@gregmagolan/test-b:test-b__files",
7+
"//@gregmagolan/test-a:test-a__all_files",
8+
"//@gregmagolan/test-b:test-b__all_files",
99
],
1010
deps = [
1111
"//@gregmagolan/test-a:test-a__contents",

internal/npm_install/test/golden/@gregmagolan/test-a/BUILD.bazel.golden

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,26 @@ filegroup(
99
"//:node_modules/@gregmagolan/test-a/package.json",
1010
],
1111
)
12+
filegroup(
13+
name = "test-a__nested_node_modules",
14+
visibility = ["//visibility:private"],
15+
)
16+
filegroup(
17+
name = "test-a__all_files",
18+
srcs = [":test-a__files", ":test-a__nested_node_modules"],
19+
visibility = ["//:__subpackages__"],
20+
)
1221
node_module_library(
1322
name = "test-a",
14-
srcs = [":test-a__files"],
23+
srcs = [":test-a__all_files"],
1524
deps = [
1625
"//@gregmagolan/test-a:test-a__contents",
1726
],
1827
)
1928
node_module_library(
2029
name = "test-a__contents",
21-
srcs = [":test-a__files"],
30+
srcs = [":test-a__all_files"],
31+
visibility = ["//:__subpackages__"],
2232
)
2333
node_module_library(
2434
name = "test-a__typings",

internal/npm_install/test/golden/@gregmagolan/test-b/BUILD.bazel.golden

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,33 @@ filegroup(
55
name = "test-b__files",
66
srcs = [
77
"//:node_modules/@gregmagolan/test-b/main.js",
8+
"//:node_modules/@gregmagolan/test-b/package.json",
9+
],
10+
)
11+
filegroup(
12+
name = "test-b__nested_node_modules",
13+
srcs = [
814
"//:node_modules/@gregmagolan/test-b/node_modules/@gregmagolan/test-a/main.js",
915
"//:node_modules/@gregmagolan/test-b/node_modules/@gregmagolan/test-a/package.json",
10-
"//:node_modules/@gregmagolan/test-b/package.json",
1116
],
17+
visibility = ["//visibility:private"],
18+
)
19+
filegroup(
20+
name = "test-b__all_files",
21+
srcs = [":test-b__files", ":test-b__nested_node_modules"],
22+
visibility = ["//:__subpackages__"],
1223
)
1324
node_module_library(
1425
name = "test-b",
15-
srcs = [":test-b__files"],
26+
srcs = [":test-b__all_files"],
1627
deps = [
1728
"//@gregmagolan/test-b:test-b__contents",
1829
],
1930
)
2031
node_module_library(
2132
name = "test-b__contents",
22-
srcs = [":test-b__files"],
33+
srcs = [":test-b__all_files"],
34+
visibility = ["//:__subpackages__"],
2335
)
2436
node_module_library(
2537
name = "test-b__typings",

0 commit comments

Comments
 (0)