Skip to content

Commit bf68577

Browse files
devversionalexeagle
authored andcommitted
fix(npm_install): dynamic_deps attribute not working for scoped packages
1 parent aa83f90 commit bf68577

4 files changed

Lines changed: 37 additions & 16 deletions

File tree

WORKSPACE

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,14 @@ yarn_install(
217217

218218
yarn_install(
219219
name = "fine_grained_goldens",
220-
# exercise the dynamic_deps feature, even though it doesn't make sense for a real jasmine binary to depend on zone.js
221-
# This will just inject an extra data[] dependency into the jasmine_bin generated target.
222-
dynamic_deps = {"jasmine": "zone.js"},
220+
# exercise the dynamic_deps feature, even though it doesn't make sense for the targets to
221+
# depend on zone.js or Angular core. This will just inject an extra data[] dependency into
222+
# the generated binary targets. Note that we also ensure that scoped packages can be properly
223+
# modified.
224+
dynamic_deps = {
225+
"@gregmagolan/test-a": "@angular/core",
226+
"jasmine": "zone.js",
227+
},
223228
manual_build_file_contents = """
224229
filegroup(
225230
name = "golden_files",

internal/npm_install/generate_build_file.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,9 @@ function addDynamicDependencies(pkgs, dynamic_deps = DYNAMIC_DEPS) {
444444
pkgs.forEach(p => {
445445
function match(name) {
446446
// Automatically include dynamic dependency on plugins of the form pkg-plugin-foo
447-
if (name.startsWith(`${p._name}-plugin-`)) return true;
447+
if (name.startsWith(`${p._moduleName}-plugin-`)) return true;
448448

449-
const value = dynamic_deps[p._name];
449+
const value = dynamic_deps[p._moduleName];
450450
if (name === value) return true;
451451

452452
// Support wildcard match
@@ -456,8 +456,8 @@ function addDynamicDependencies(pkgs, dynamic_deps = DYNAMIC_DEPS) {
456456

457457
return false;
458458
}
459-
p._dynamicDependencies =
460-
pkgs.filter(x => !!x._name && match(x._name)).map(dyn => `//${dyn._dir}:${dyn._name}`);
459+
p._dynamicDependencies = pkgs.filter(x =>
460+
!!x._moduleName && match(x._moduleName)).map(dyn => `//${dyn._dir}:${dyn._name}`);
461461
});
462462
}
463463

@@ -485,13 +485,13 @@ function findPackages(p = 'node_modules') {
485485
packages.forEach(
486486
f => pkgs.push(parsePackage(f), ...findPackages(path.posix.join(f, 'node_modules'))));
487487

488-
addDynamicDependencies(pkgs);
489-
490488
const scopes = listing.filter(f => f.startsWith('@'))
491489
.map(f => path.posix.join(p, f))
492490
.filter(f => isDirectory(f));
493491
scopes.forEach(f => pkgs.push(...findPackages(f)));
494492

493+
addDynamicDependencies(pkgs);
494+
495495
return pkgs;
496496
}
497497

@@ -532,6 +532,10 @@ function parsePackage(p) {
532532
// Stash the package directory name for future use
533533
pkg._name = pkg._dir.split('/').pop();
534534

535+
// Module name of the package. Unlike "_name" this represents the
536+
// full package name (including scope name).
537+
pkg._moduleName = pkg.name || `${pkg._dir}/${pkg._name}`;
538+
535539
// Keep track of whether or not this is a nested package
536540
pkg._isNested = /\/node_modules\//.test(p);
537541

internal/npm_install/test/generate_build_file.spec.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,36 @@ describe('build file generator', () => {
8080

8181
describe('dynamic dependencies', () => {
8282
it('should include requested dynamic dependencies in nodejs_binary data', () => {
83-
const pkgs = [{_name: 'foo', bin: 'foobin', _dir: 'some_dir'}, {_name: 'bar', _dir: 'bar'}];
84-
addDynamicDependencies(pkgs, {'foo': 'bar'});
83+
const pkgs = [
84+
{_name: 'foo', bin: 'foobin', _dir: 'some_dir', _moduleName: 'foo'},
85+
{_name: 'bar', _dir: 'bar', _moduleName: 'bar'},
86+
{_name: 'typescript', bin: 'tsc_wrapped', _dir: 'a', _moduleName: '@bazel/typescript'},
87+
{_name: 'tsickle', _dir: 'b', _moduleName: 'tsickle'},
88+
];
89+
addDynamicDependencies(pkgs, {'foo': 'bar', '@bazel/typescript': 'tsickle'});
8590
expect(pkgs[0]._dynamicDependencies).toEqual(['//bar:bar']);
91+
expect(pkgs[2]._dynamicDependencies).toEqual(['//b:tsickle']);
8692
expect(printPackageBin(pkgs[0])).toContain('data = ["//some_dir:foo", "//bar:bar"]');
93+
expect(printPackageBin(pkgs[2])).toContain('data = ["//a:typescript", "//b:tsickle"]');
8794
});
8895
it('should support wildcard', () => {
89-
const pkgs = [{_name: 'foo', bin: 'foobin', _dir: 'some_dir'}, {_name: 'bar', _dir: 'bar'}];
96+
const pkgs = [
97+
{_name: 'foo', bin: 'foobin', _dir: 'some_dir', _moduleName: 'foo'},
98+
{_name: 'bar', _dir: 'bar', _moduleName: 'bar'}
99+
];
90100
addDynamicDependencies(pkgs, {'foo': 'b*'});
91101
expect(pkgs[0]._dynamicDependencies).toEqual(['//bar:bar']);
92102
expect(printPackageBin(pkgs[0])).toContain('data = ["//some_dir:foo", "//bar:bar"]');
93103
});
94104
it('should automatically include plugins in nodejs_binary data', () => {
95-
const pkgs =
96-
[{_name: 'foo', bin: 'foobin', _dir: 'some_dir'}, {_name: 'foo-plugin-bar', _dir: 'bar'}];
105+
const pkgs = [
106+
{_name: 'foo', bin: 'foobin', _dir: 'some_dir', _moduleName: 'foo'},
107+
{_name: 'foo-plugin-bar', _dir: 'bar', _moduleName: 'foo-plugin-bar'}
108+
];
97109
addDynamicDependencies(pkgs, {});
98110
expect(pkgs[0]._dynamicDependencies).toEqual(['//bar:foo-plugin-bar']);
99111
expect(printPackageBin(pkgs[0]))
100112
.toContain('data = ["//some_dir:foo", "//bar:foo-plugin-bar"]');
101113
});
102114
});
103-
});
115+
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ nodejs_binary(
55
name = "test",
66
entry_point = "//:node_modules/@gregmagolan/test-a/@bin/test.js",
77
install_source_map_support = False,
8-
data = ["//@gregmagolan/test-a:test-a"],
8+
data = ["//@gregmagolan/test-a:test-a", "//@angular/core:core"],
99
)

0 commit comments

Comments
 (0)