From f0aefb89eb3646cc8a16aca1ab28999b910be1e7 Mon Sep 17 00:00:00 2001 From: Greg Magolan Date: Mon, 24 Sep 2018 15:22:09 -0700 Subject: [PATCH] Split up npm fine grained deps BUILD file into multiple BUILD files BREAKING CHANGES: Fine grained deps targets changed from `@wksp//:pkg` to `@wksp//pkg` --- .circleci/bazel.rc | 3 - .circleci/config.yml | 4 +- README.md | 66 +- examples/bazel_managed_deps/BUILD.bazel | 4 +- examples/bazel_managed_deps/README.md | 2 +- examples/bazel_managed_deps/WORKSPACE | 2 +- examples/define_var/BUILD.bazel | 9 +- internal/e2e/fine_grained_deps/BUILD.bazel | 6 +- .../e2e/rollup_fine_grained_deps/BUILD.bazel | 12 +- internal/jasmine_node_test/jasmine_runner.js | 4 +- internal/node/node.bzl | 10 +- internal/node/test/BUILD.bazel | 8 +- internal/npm_install/generate_build_file.js | 303 +++++--- internal/npm_install/npm_install.bzl | 4 +- internal/npm_install/test/BUILD.bazel | 9 +- internal/npm_install/test/BUILD.bazel.golden | 689 ------------------ internal/npm_install/test/check.js | 27 +- .../test/generate_build_file.spec.js | 8 +- .../golden/@gregmagolan/BUILD.bazel.golden | 6 + .../@gregmagolan/test-a/BUILD.bazel.golden | 18 + .../test-a/bin/BUILD.bazel.golden | 2 + .../@gregmagolan/test-b/BUILD.bazel.golden | 18 + .../test-b/bin/BUILD.bazel.golden | 2 + .../test/golden/BUILD.bazel.golden | 25 + .../test/golden/jasmine/BUILD.bazel.golden | 18 + .../golden/jasmine/bin/BUILD.bazel.golden | 6 + .../@gregmagolan/BUILD.bazel.golden | 10 + .../@gregmagolan/test-a/BUILD.bazel.golden | 56 ++ .../@gregmagolan/test-b/BUILD.bazel.golden | 56 ++ .../node_modules/jasmine/BUILD.bazel.golden | 73 ++ .../node_modules/unidiff/BUILD.bazel.golden | 56 ++ .../test/golden/unidiff/BUILD.bazel.golden | 18 + .../golden/unidiff/bin/BUILD.bazel.golden | 2 + internal/npm_install/test/update_golden.js | 4 +- internal/npm_install/test/yarn.lock | 8 +- internal/rollup/rollup_bundle.bzl | 4 +- 36 files changed, 690 insertions(+), 862 deletions(-) delete mode 100644 internal/npm_install/test/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/@gregmagolan/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/@gregmagolan/test-a/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/@gregmagolan/test-a/bin/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/@gregmagolan/test-b/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/@gregmagolan/test-b/bin/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/jasmine/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/jasmine/bin/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/node_modules/@gregmagolan/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/node_modules/@gregmagolan/test-a/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/node_modules/@gregmagolan/test-b/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/node_modules/jasmine/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/node_modules/unidiff/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/unidiff/BUILD.bazel.golden create mode 100644 internal/npm_install/test/golden/unidiff/bin/BUILD.bazel.golden diff --git a/.circleci/bazel.rc b/.circleci/bazel.rc index db2bc43dbe..237885f131 100644 --- a/.circleci/bazel.rc +++ b/.circleci/bazel.rc @@ -4,9 +4,6 @@ # Don't be spammy in the logs build --noshow_progress -# Don't run manual tests -test --test_tag_filters=-manual - # Print all the options that apply to the build. # This helps us diagnose which options override others # (e.g. /etc/bazel.bazelrc vs. tools/bazel.rc) diff --git a/.circleci/config.yml b/.circleci/config.yml index d08b86a1b4..f26f48b5bd 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -81,7 +81,8 @@ jobs: - run: bazel run @nodejs//:yarn - run: bazel build ... - - run: bazel test ... --define=some_env=some_value + - run: bazel test ... + - run: bazel test //examples/define_var --define=some_env=some_value # TODO(alexeagle): move this into the example proper - run: bazel run examples/rollup -- --help @@ -90,6 +91,7 @@ jobs: - run: bazel run //internal/node/test:has_deps_legacy - run: bazel run //internal/node/test:has_deps - run: bazel run //internal/node/test:has_deps_hybrid + - run: bazel run @fine_grained_deps_yarn//typescript/bin:tsc # We should also be able to test targets in a different workspace - run: bazel test @program_example//... diff --git a/README.md b/README.md index 6102c51aca..96bcd090a4 100644 --- a/README.md +++ b/README.md @@ -202,8 +202,8 @@ load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") nodejs_binary( name = "bar", data = [ - "@npm//:foo", - "@npm//:baz", + "@npm//foo", + "@npm//baz", ] ... ) @@ -220,14 +220,57 @@ jasmine_node_test( name = "test", ... deps = [ - "@npm//:jasmine", - "@npm//:foo", - "@npm//:baz", + "@npm//jasmine", + "@npm//foo", + "@npm//baz", ... ], ) ``` +#### Fine-grained npm package nodejs_binary targets + +If an npm package lists one of more `bin` entry points in its `package.json`, +`nodejs_binary` targets will be generated for these. + +For example, the `protractor` package has two bin entries in its `package.json`: + +```json + "bin": { + "protractor": "bin/protractor", + "webdriver-manager": "bin/webdriver-manager" + }, +``` + +These will result in two generated `nodejs_binary` targets in the `@npm//protractor/bin` +package (if your npm deps workspace is `@npm`): + +```python +nodejs_binary( + name = "protractor", + entry_point = "protractor/bin/protractor", + data = ["//protractor:protractor"], +) + +nodejs_binary( + name = "webdriver-manager", + entry_point = "protractor/bin/webdriver-manager", + data = ["//protractor:protractor"], +) +``` + +These targets can be used as executables for actions in custom rules or can +be run by Bazel directly. For example, you can run protractor with the +following: + +```sh +$ bazel run @npm//protractor/bin:protractor +``` + +Note: These targets are in the `protractor/bin` package so they don't +conflict with the deps targets. For example, `@npm//protractor:protractor` is +the deps targets while `@npm//protractor/bin:protractor` is the binary target. + #### Coarse node_modules dependencies Using fine grained npm dependencies is recommended to minimize @@ -236,15 +279,14 @@ there are also filegroups defined by `yarn_install` and `npm_install` that include all packages under `node_modules` and which can be used with the `node_modules` attribute of nodejs rules. -* `@npm//:node_modules` is includes **all** files under `node_modules` -* `@npm//:node_modules_lite` is includes only `.js`, `.d.ts`, `.json` and `./bin/*` under `node_modules` (this reduces the number of input files) +* `@npm//:node_modules` includes all packages under `node_modules` as well as the `.bin` folder ```python load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") nodejs_binary( name = "bar", - node_modules = "@npm//:node_modules_lite", + node_modules = "@npm//:node_modules", ) ``` @@ -260,8 +302,14 @@ filegroup( name = "node_modules", srcs = glob( include = ["node_modules/**/*"], - # Files with spaces in the name are not legal Bazel labels exclude = [ + # Files under test & docs may contain file names that + # are not legal Bazel labels (e.g., + # node_modules/ecstatic/test/public/中文/檔案.html) + "node_modules/test/**", + "node_modules/docs/**", + # Files with spaces are not allowed in Bazel runfiles + # See https://github.com/bazelbuild/bazel/issues/4327 "node_modules/**/* */**", "node_modules/**/* *", ], diff --git a/examples/bazel_managed_deps/BUILD.bazel b/examples/bazel_managed_deps/BUILD.bazel index 5eab51b3ef..fe2d6e1776 100644 --- a/examples/bazel_managed_deps/BUILD.bazel +++ b/examples/bazel_managed_deps/BUILD.bazel @@ -16,7 +16,7 @@ jasmine_node_test( name = "fine_grained_test", srcs = glob(["*.spec.js"]), deps = [ - "@npm//:jasmine", - "@npm//:typescript", + "@npm//jasmine", + "@npm//typescript", ], ) diff --git a/examples/bazel_managed_deps/README.md b/examples/bazel_managed_deps/README.md index 1d98b72736..db6257a96a 100644 --- a/examples/bazel_managed_deps/README.md +++ b/examples/bazel_managed_deps/README.md @@ -9,7 +9,7 @@ Instead, we can declare individual npm packages as dependencies, e.g.: ``` nodejs_binary( name = "fast", - data = ["@npm//:jasmine"] + data = ["@npm//jasmine"] ) ``` diff --git a/examples/bazel_managed_deps/WORKSPACE b/examples/bazel_managed_deps/WORKSPACE index 9ee7c0ef84..9837978e78 100644 --- a/examples/bazel_managed_deps/WORKSPACE +++ b/examples/bazel_managed_deps/WORKSPACE @@ -14,7 +14,7 @@ node_repositories() # This runs yarn install, then our generate_build_file.js to create BUILD files # inside the resulting node_modules directory. # The name "npm" here means the resulting modules are referenced like -# @npm//:jasmine +# @npm//jasmine yarn_install( name = "npm", package_json = "//:package.json", diff --git a/examples/define_var/BUILD.bazel b/examples/define_var/BUILD.bazel index 385210b0e9..d2de3755b5 100644 --- a/examples/define_var/BUILD.bazel +++ b/examples/define_var/BUILD.bazel @@ -7,11 +7,12 @@ jasmine_node_test( name = "define_var", srcs = glob(["*.spec.js"]), # Just here as a smoke test for this attribute. - # Test must be run with --define=some_env=some_value. - # Use `bazel test ... --define=some_env=some_value` or - # `bazel test //examples/rollup:test --define=some_env=some_value`. + # This test must be run with --define=some_env=some_value. + # Use `bazel test //examples/define_var --define=some_env=some_value`. + # Note that use of --define causes the entire build to be non-incremental + # since --define can affect the output of any action. configuration_env_vars = ["some_env"], - data = ["@fine_grained_deps_yarn//:jasmine"], + data = ["@fine_grained_deps_yarn//jasmine"], # Don't include this test in //... pattern since it requires special # --define option tags = ["manual"], diff --git a/internal/e2e/fine_grained_deps/BUILD.bazel b/internal/e2e/fine_grained_deps/BUILD.bazel index 6a2ef388f6..5cbe5e9a6b 100644 --- a/internal/e2e/fine_grained_deps/BUILD.bazel +++ b/internal/e2e/fine_grained_deps/BUILD.bazel @@ -25,11 +25,11 @@ load("@build_bazel_rules_nodejs//:defs.bzl", "jasmine_node_test") "fine.spec.js", ], deps = [ - "@fine_grained_deps_%s//:jasmine" % pkgmgr, - "@fine_grained_deps_%s//:typescript" % pkgmgr, + "@fine_grained_deps_%s//jasmine" % pkgmgr, + "@fine_grained_deps_%s//typescript" % pkgmgr, # Note, test-b depends on test-a@0.0.1 which should get # hoisted to node_modules/test-b/node_modules/test-a - "@fine_grained_deps_%s//:@gregmagolan/test-b" % pkgmgr, + "@fine_grained_deps_%s//@gregmagolan/test-b" % pkgmgr, ], ) for pkgmgr in [ "yarn", diff --git a/internal/e2e/rollup_fine_grained_deps/BUILD.bazel b/internal/e2e/rollup_fine_grained_deps/BUILD.bazel index 7d39c59ee8..ae494a71fc 100644 --- a/internal/e2e/rollup_fine_grained_deps/BUILD.bazel +++ b/internal/e2e/rollup_fine_grained_deps/BUILD.bazel @@ -15,7 +15,7 @@ rollup_bundle( srcs = ["has-deps.js"], entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js", deps = [ - "@fine_grained_deps_yarn//:@gregmagolan/test-b", + "@fine_grained_deps_yarn//@gregmagolan/test-b", ], ) @@ -25,7 +25,7 @@ rollup_bundle( name = "bundle_legacy", srcs = ["has-deps.js"], entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js", - node_modules = "@fine_grained_deps_yarn//:node_modules_lite", + node_modules = "@fine_grained_deps_yarn//:node_modules", ) # You can have a rollup_bundle with both a node_modules attribute @@ -34,9 +34,9 @@ rollup_bundle( name = "bundle_hybrid", srcs = ["has-deps.js"], entry_point = "internal/e2e/rollup_fine_grained_deps/has-deps.js", - node_modules = "@fine_grained_deps_yarn//:node_modules_lite", + node_modules = "@fine_grained_deps_yarn//:node_modules", deps = [ - "@fine_grained_deps_yarn//:@gregmagolan/test-b", + "@fine_grained_deps_yarn//@gregmagolan/test-b", ], ) @@ -60,7 +60,7 @@ jasmine_node_test( ":bundle_no_deps.js", ":bundle_no_deps.min.js", ":bundle_no_deps.min_debug.js", - "@fine_grained_deps_yarn//:@gregmagolan/test-a", - "@fine_grained_deps_yarn//:jasmine", + "@fine_grained_deps_yarn//@gregmagolan/test-a", + "@fine_grained_deps_yarn//jasmine", ], ) diff --git a/internal/jasmine_node_test/jasmine_runner.js b/internal/jasmine_node_test/jasmine_runner.js index e5b388027d..760422369b 100644 --- a/internal/jasmine_node_test/jasmine_runner.js +++ b/internal/jasmine_node_test/jasmine_runner.js @@ -30,8 +30,8 @@ function main(args) { .filter(l => l.length > 0) // Filter here so that only files ending in `spec.js` and `test.js` // are added to jasmine as spec files. This is important as other - // deps such as "@npm//:typescript" if executed may cause the test to - // fail or have unexpected side-effects. "@npm//:typescript" would + // deps such as "@npm//typescript" if executed may cause the test to + // fail or have unexpected side-effects. "@npm//typescript" would // try to execute tsc, print its help, and process.exit(1) .filter(f => /[^a-zA-Z0-9](spec|test)\.js$/i.test(f)) // Filter out files from node_modules that match test.js or spec.js diff --git a/internal/node/node.bzl b/internal/node/node.bzl index 7f1fcb35c1..6c6aa61d1f 100644 --- a/internal/node/node.bzl +++ b/internal/node/node.bzl @@ -198,8 +198,8 @@ _NODEJS_EXECUTABLE_ATTRS = { name = "my_binary", ... data = [ - "@npm//:foo", - "@npm//:bar", + "@npm//foo", + "@npm//bar", ... ], ) @@ -228,9 +228,9 @@ _NODEJS_EXECUTABLE_ATTRS = { name = "my_test", ... deps = [ - "@npm//:jasmine", - "@npm//:foo", - "@npm//:bar", + "@npm//jasmine", + "@npm//foo", + "@npm//bar", ... ], ) diff --git a/internal/node/test/BUILD.bazel b/internal/node/test/BUILD.bazel index 8515be8c18..d926216f6f 100644 --- a/internal/node/test/BUILD.bazel +++ b/internal/node/test/BUILD.bazel @@ -14,7 +14,7 @@ nodejs_binary( name = "has_deps_legacy", data = ["has-deps.js"], entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps", - node_modules = "@fine_grained_deps_yarn//:node_modules_lite", + node_modules = "@fine_grained_deps_yarn//:node_modules", ) # You can have a nodejs_binary with no node_modules attribute @@ -23,7 +23,7 @@ nodejs_binary( name = "has_deps", data = [ "has-deps.js", - "@fine_grained_deps_yarn//:typescript", + "@fine_grained_deps_yarn//typescript", ], entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps", ) @@ -34,8 +34,8 @@ nodejs_binary( name = "has_deps_hybrid", data = [ "has-deps.js", - "@fine_grained_deps_yarn//:typescript", + "@fine_grained_deps_yarn//typescript", ], entry_point = "build_bazel_rules_nodejs/internal/node/test/has-deps", - node_modules = "@fine_grained_deps_yarn//:node_modules_lite", + node_modules = "@fine_grained_deps_yarn//:node_modules", ) diff --git a/internal/npm_install/generate_build_file.js b/internal/npm_install/generate_build_file.js index df0fec21f6..54160dc75e 100644 --- a/internal/npm_install/generate_build_file.js +++ b/internal/npm_install/generate_build_file.js @@ -15,46 +15,30 @@ * limitations under the License. */ /** - * @fileoverview This script generates a BUILD.bazel file by analyzing + * @fileoverview This script generates BUILD.bazel files by analyzing * the node_modules folder layed out by yarn or npm. It generates * fine grained Bazel filegroup targets for each root npm package * and all files for that package and its transitive deps are included - * in the filegroup. For example, `@//:jasmine` would + * in the filegroup. For example, `@//jasmine` would * include all files in the jasmine npm package and all of its * transitive dependencies. * * nodejs_binary targets are also generated for all `bin` scripts - * in each package. For example, the `@//:jasmine/jasmine` + * in each package. For example, the `@//jasmine/bin:jasmine` * target will be generated for the `jasmine` binary in the `jasmine` * npm package: * * ``` * nodejs_binary( - * name = "jasmine/jasmine", - * entry_point = "jasmine/bin/jasmine.js", - * data = [":jasmine"], + * name = "jasmine", + * entry_point = "jasmine/bin/jasmine.js", + * data = ["//jasmine:jasmine"], * ) * ``` * - * Additionally, the following coarse grained filegroup targets - * are also generated for backward compatibility with the node_modules - * attribute of nodejs_binary and other rules that take that - * attribute: - * - * `@//:node_modules`: The entire node_modules directory in one - * catch-all filegroup. NB: Using this target may have bad performance - * implications if there are many files in filegroup. - * See https://github.com/bazelbuild/bazel/issues/5153. - * - * `@//:node_modules_lite`: A lite version of the node_modules filegroup - * that includes only js, d.ts and json files as well as the .bin folder. This - * can be used in some cases to improve performance by reducing the number - * of runfiles. The recommended approach to reducing performance - * is to use fine grained deps such as ["@npm//:a", "@npm://b", ...]. - * There are cases where the node_modules_lite filegroup will - * not include files with no extension that are needed. The feature request - * https://github.com/bazelbuild/bazel/issues/5769 would allow this - * filegroup to include those files. + * Additionally, a `@//:node_modules` filegroup + * is generated that includes all packages under node_modules + * as well as the .bin folder. * * This work is based off the fine grained deps concepts in * https://github.com/pubref/rules_node developed by @pcj. @@ -66,75 +50,33 @@ const fs = require('fs'); const path = require('path'); -const BUILD_FILE_HEADER = `# Generated file from yarn_install rule. +const BUILD_FILE_HEADER = `# Generated file from yarn_install/npm_install rule. # See $(bazel info output_base)/external/build_bazel_rules_nodejs/internal/npm_install/generate_build_file.js # All rules in other repositories can use these targets package(default_visibility = ["//visibility:public"]) -load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") - -# The entire node_modules directory in one catch-all filegroup. -# NB: Using this target may have bad performance implications if -# there are many files in filegroup. -# See https://github.com/bazelbuild/bazel/issues/5153. -filegroup( - name = "node_modules", - srcs = glob( - include = ["node_modules/**/*"], - exclude = [ - # Files under test & docs may contain file names that - # are not legal Bazel labels (e.g., - # node_modules/ecstatic/test/public/中文/檔案.html) - "node_modules/**/test/**", - "node_modules/**/docs/**", - # Files with spaces in the name are not legal Bazel labels - "node_modules/**/* */**", - "node_modules/**/* *", - ], - ), -) - -# A lite version of the node_modules filegroup that includes -# only js, d.ts and json files as well as the .bin folder. This can -# be used in some cases to improve performance by reducing the number -# of runfiles. The recommended approach to reducing performance -# is to use fine grained deps such as ["@npm//:a", "@npm://b", ...]. -# There are cases where the node_modules_lite filegroup will -# not include files with no extension that are needed. The feature request -# https://github.com/bazelbuild/bazel/issues/5769 would allow this -# filegroup to include those files. -filegroup( - name = "node_modules_lite", - srcs = glob( - include = [ - "node_modules/**/*.js", - "node_modules/**/*.d.ts", - "node_modules/**/*.json", - "node_modules/.bin/*", - ], - exclude = [ - # Files under test & docs may contain file names that - # are not legal Bazel labels (e.g., - # node_modules/ecstatic/test/public/中文/檔案.html) - "node_modules/**/test/**", - "node_modules/**/docs/**", - # Files with spaces in the name are not legal Bazel labels - "node_modules/**/* */**", - "node_modules/**/* *", - ], - ), -) - ` if (require.main === module) { main(); } +function mkdirp(dirname) { + if (!fs.existsSync(dirname)) { + mkdirp(path.dirname(dirname)); + fs.mkdirSync(dirname); + } +} + +function writeFileSync(filePath, contents) { + mkdirp(path.dirname(filePath)); + fs.writeFileSync(filePath, contents); +} + /** * Main entrypoint. - * Write BUILD file. + * Write BUILD files. */ function main() { // find all packages (including packages in nested node_modules) @@ -146,26 +88,76 @@ function main() { pkgs.forEach(pkg => pkgsMap.set(pkg._dir, pkg)); pkgs.forEach(pkg => flattenDependencies(pkg, pkg, pkgsMap)); - // generate the BUILD file - let buildFile = BUILD_FILE_HEADER; - pkgs.filter(pkg => !pkg._isNested).forEach(pkg => buildFile += printPackage(pkg)); - scopes.forEach(scope => buildFile += printScope(scope, pkgs)); + // generate BUILD files + generateRootBuildFile(pkgs) + pkgs.filter(pkg => !pkg._isNested).forEach(pkg => generatePackageBuildFiles(pkg)); + scopes.forEach(scope => generateScopeBuildFiles(scope, pkgs)); +} + +module.exports = {main}; + +function generateRootBuildFile(pkgs) { + const srcs = pkgs.filter(pkg => !pkg._isNested) + .map(pkg => `"//node_modules/${pkg._dir}:${pkg._name}__lite",`) + .join('\n '); + + let buildFile = BUILD_FILE_HEADER + `# The node_modules directory in one catch-all filegroup. +# NB: Using this target may have bad performance implications if +# there are many files in filegroup. +# See https://github.com/bazelbuild/bazel/issues/5153. +# +# This filegroup includes only js, d.ts, json and proto files as well as the +# pkg/bin folders and .bin folder. This can be used in some cases to improve +# performance by reducing the number of runfiles. The recommended approach +# to reducing performance is to use fine grained deps such as +# ["@npm//a", "@npm//b", ...]. There are cases where the node_modules +# filegroup will not include files with no extension that are needed. The +# feature request https://github.com/bazelbuild/bazel/issues/5769 would allow +# this filegroup to include those files. +filegroup( + name = "node_modules", + srcs = glob(["node_modules/.bin/*"]) + [ + ${srcs} + ], +) + +` + + // Add the manual build file contents if they exists try { - const manualContents = fs.readFileSync(`manual_build_file_contents`, {encoding: 'utf8'}); - buildFile += '\n\n'; - buildFile += manualContents; + buildFile += fs.readFileSync(`manual_build_file_contents`, {encoding: 'utf8'}); } catch (e) { } - fs.writeFileSync('BUILD.bazel', buildFile); + + writeFileSync('BUILD.bazel', buildFile); } -module.exports = {main}; +function generatePackageBuildFiles(pkg) { + const buildFile = + BUILD_FILE_HEADER + `load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") + +` + printPackage(pkg); + writeFileSync(path.posix.join('node_modules', pkg._dir, 'BUILD.bazel'), buildFile); + + const aliasBuildFile = BUILD_FILE_HEADER + printPackageAlias(pkg); + writeFileSync(path.posix.join(pkg._dir, 'BUILD.bazel'), aliasBuildFile); + + const binAliasesBuildFile = BUILD_FILE_HEADER + printPackageBinAliases(pkg); + writeFileSync(path.posix.join(pkg._dir, 'bin', 'BUILD.bazel'), binAliasesBuildFile); +} + +function generateScopeBuildFiles(scope, pkgs) { + const buildFile = BUILD_FILE_HEADER + printScope(scope, pkgs); + writeFileSync(path.posix.join('node_modules', scope, 'BUILD.bazel'), buildFile); + + const aliasBuildFile = BUILD_FILE_HEADER + printScopeAlias(scope); + writeFileSync(path.posix.join(scope, 'BUILD.bazel'), aliasBuildFile); +} /** * Checks if a path is an npm package which is is a directory with a package.json file. */ function isPackage(p) { - // const packageJson = path.posix.join(p, 'package.json'); return fs.statSync(p).isDirectory() && fs.existsSync(packageJson) && fs.statSync(packageJson).isFile(); @@ -226,6 +218,9 @@ function parsePackage(p) { // assign to _dir for future use pkg._dir = p.replace(/^node_modules\//, ''); + // Stash the package directory name for future use + pkg._name = pkg._dir.split('/').pop(); + // Keep track of whether or not this is a nested package pkg._isNested = p.match(/\/node_modules\//); @@ -340,68 +335,141 @@ function printJson(pkg) { */ function printPackage(pkg) { let result = ` -# Generated target for npm package "${pkg._dir}" +# Generated targets for npm package "${pkg._dir}" ${printJson(pkg)} + filegroup( - name = "${pkg._dir}", + name = "${pkg._name}__pkg", srcs = [ # ${pkg._dir} package contents (and contents of nested node_modules) - ":${pkg._dir}__files", + ":${pkg._name}__files", # direct or transitive dependencies hoisted to root by the package manager ${ pkg._dependencies.filter(dep => dep != pkg) .filter(dep => !dep._isNested) - .map(dep => `":${dep._dir}__files",`) + .map(dep => `"//node_modules/${dep._dir}:${dep._name}__files",`) .join('\n ')} ], tags = ["NODE_MODULE_MARKER"], ) filegroup( - name = "${pkg._dir}__files", + name = "${pkg._name}__files", srcs = glob( - include = ["node_modules/${pkg._dir}/**/*"], + include = ["**/*"], exclude = [ # Files under test & docs may contain file names that # are not legal Bazel labels (e.g., # node_modules/ecstatic/test/public/中文/檔案.html) - "node_modules/${pkg._dir}/test/**", - "node_modules/${pkg._dir}/docs/**", - # Files with spaces in the name are not legal Bazel labels - "node_modules/${pkg._dir}/**/* */**", - "node_modules/${pkg._dir}/**/* *", + "test/**", + "docs/**", + # Files with spaces are not allowed in Bazel runfiles + # See https://github.com/bazelbuild/bazel/issues/4327 + "**/* */**", + "**/* *", ], ), tags = ["NODE_MODULE_MARKER"], ) filegroup( - name = "${pkg._dir}__typings", + name = "${pkg._name}__lite", srcs = glob( - include = ["node_modules/${pkg._dir}/**/*.d.ts"], + include = [ + "**/*.js", + "**/*.d.ts", + "**/*.json", + "**/*.proto", + "bin/**/*", + ], exclude = [ # Files under test & docs may contain file names that # are not legal Bazel labels (e.g., # node_modules/ecstatic/test/public/中文/檔案.html) - "node_modules/${pkg._dir}/test/**", - "node_modules/${pkg._dir}/docs/**", - # Files with spaces in the name are not legal Bazel labels - "node_modules/${pkg._dir}/**/* */**", - "node_modules/${pkg._dir}/**/* *", + "test/**", + "docs/**", + # Files with spaces are not allowed in Bazel runfiles + # See https://github.com/bazelbuild/bazel/issues/4327 + "**/* */**", + "**/* *", ], ), tags = ["NODE_MODULE_MARKER"], ) + +filegroup( + name = "${pkg._name}__typings", + srcs = glob( + include = ["**/*.d.ts"], + exclude = [ + # Files under test & docs may contain file names that + # are not legal Bazel labels (e.g., + # node_modules/ecstatic/test/public/中文/檔案.html) + "test/**", + "docs/**", + # Files with spaces are not allowed in Bazel runfiles + # See https://github.com/bazelbuild/bazel/issues/4327 + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) + `; if (pkg._executables) { for (const [name, path] of pkg._executables.entries()) { result += `# Wire up the \`bin\` entry \`${name}\` nodejs_binary( - name = "${pkg._dir}/${name}", + name = "${name}__bin", entry_point = "${pkg._dir}/${path}", install_source_map_support = False, - data = [":${pkg._dir}"], + data = [":${pkg._name}__pkg"], +) + +`; + } + } + + return result; +} + +function printPackageAlias(pkg) { + return ` +# Generated target alias for npm package "${pkg._dir}" +${printJson(pkg)} +alias( + name = "${pkg._name}", + actual = "//node_modules/${pkg._dir}:${pkg._name}__pkg" +) + +alias( + name = "${pkg._name}__files", + actual = "//node_modules/${pkg._dir}:${pkg._name}__files" +) + +alias( + name = "${pkg._name}__lite", + actual = "//node_modules/${pkg._dir}:${pkg._name}__lite" +) + +alias( + name = "${pkg._name}__typings", + actual = "//node_modules/${pkg._dir}:${pkg._name}__typings" +) +`; +} + +function printPackageBinAliases(pkg) { + let result = ''; + + if (pkg._executables) { + for (const [name, path] of pkg._executables.entries()) { + result += `# Wire up the \`bin\` entry \`${name}\` +alias( + name = "${name}", + actual = "//node_modules/${pkg._dir}:${name}__bin", ) `; @@ -421,10 +489,21 @@ function printScope(scope, pkgs) { filegroup( name = "${scope}", srcs = [ - ${scopePkgs.map(pkg => `":${pkg._dir}",`).join('\n ')} + ${scopePkgs.map(pkg => `"//node_modules/${pkg._dir}",`).join('\n ')} ], tags = ["NODE_MODULE_MARKER"], ) `; } + +function printScopeAlias(scope) { + return ` +# Generated alias target for npm scope ${scope} +alias( + name = "${scope}", + actual = "//node_modules/${scope}", +) + +`; +} diff --git a/internal/npm_install/npm_install.bzl b/internal/npm_install/npm_install.bzl index 98818df8b4..d334bbf0bb 100644 --- a/internal/npm_install/npm_install.bzl +++ b/internal/npm_install/npm_install.bzl @@ -27,12 +27,12 @@ load("//internal/common:os_name.bzl", "os_name") def _create_build_file(repository_ctx, node): if repository_ctx.attr.manual_build_file_contents: repository_ctx.file("manual_build_file_contents", repository_ctx.attr.manual_build_file_contents) - result = repository_ctx.execute([node, "internal/generate_build_file.js"]) + result = repository_ctx.execute([node, "generate_build_file.js"]) if result.return_code: fail("node failed: \nSTDOUT:\n%s\nSTDERR:\n%s" % (result.stdout, result.stderr)) def _add_build_file_generator(repository_ctx): - repository_ctx.template("internal/generate_build_file.js", + repository_ctx.template("generate_build_file.js", repository_ctx.path(Label("//internal/npm_install:generate_build_file.js")), {}) def _add_data_dependencies(repository_ctx): diff --git a/internal/npm_install/test/BUILD.bazel b/internal/npm_install/test/BUILD.bazel index b92a7d0eb0..7b5babcd09 100644 --- a/internal/npm_install/test/BUILD.bazel +++ b/internal/npm_install/test/BUILD.bazel @@ -5,12 +5,17 @@ filegroup( srcs = glob(["node_modules/**"]), ) +filegroup( + name = "goldens", + srcs = glob(["golden/**"]), +) + jasmine_node_test( name = "test", srcs = glob(["*.spec.js"]), data = [ - ":BUILD.bazel.golden", ":check.js", + ":goldens", ":node_modules", ":yarn.lock", ], @@ -21,8 +26,8 @@ jasmine_node_test( nodejs_binary( name = "test.accept", data = [ - ":BUILD.bazel.golden", ":check.js", + ":goldens", ":node_modules", ":update_golden.js", ":yarn.lock", diff --git a/internal/npm_install/test/BUILD.bazel.golden b/internal/npm_install/test/BUILD.bazel.golden deleted file mode 100644 index 5381ef2e3e..0000000000 --- a/internal/npm_install/test/BUILD.bazel.golden +++ /dev/null @@ -1,689 +0,0 @@ - -package(default_visibility = ["//visibility:public"]) -load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") -filegroup( - name = "node_modules", - srcs = glob( - include = ["node_modules/**/*"], - exclude = [ - "node_modules/**/test/**", - "node_modules/**/docs/**", - "node_modules/**/* */**", - "node_modules/**/* *", - ], - ), -) -filegroup( - name = "node_modules_lite", - srcs = glob( - include = [ - "node_modules/**/*.js", - "node_modules/**/*.d.ts", - "node_modules/**/*.json", - "node_modules/.bin/*", - ], - exclude = [ - "node_modules/**/test/**", - "node_modules/**/docs/**", - "node_modules/**/* */**", - "node_modules/**/* *", - ], - ), -) -filegroup( - name = "balanced-match", - srcs = [ - ":balanced-match__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "balanced-match__files", - srcs = glob( - include = ["node_modules/balanced-match/**/*"], - exclude = [ - "node_modules/balanced-match/test/**", - "node_modules/balanced-match/docs/**", - "node_modules/balanced-match/**/* */**", - "node_modules/balanced-match/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "balanced-match__typings", - srcs = glob( - include = ["node_modules/balanced-match/**/*.d.ts"], - exclude = [ - "node_modules/balanced-match/test/**", - "node_modules/balanced-match/docs/**", - "node_modules/balanced-match/**/* */**", - "node_modules/balanced-match/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "brace-expansion", - srcs = [ - ":brace-expansion__files", - ":balanced-match__files", - ":concat-map__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "brace-expansion__files", - srcs = glob( - include = ["node_modules/brace-expansion/**/*"], - exclude = [ - "node_modules/brace-expansion/test/**", - "node_modules/brace-expansion/docs/**", - "node_modules/brace-expansion/**/* */**", - "node_modules/brace-expansion/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "brace-expansion__typings", - srcs = glob( - include = ["node_modules/brace-expansion/**/*.d.ts"], - exclude = [ - "node_modules/brace-expansion/test/**", - "node_modules/brace-expansion/docs/**", - "node_modules/brace-expansion/**/* */**", - "node_modules/brace-expansion/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "concat-map", - srcs = [ - ":concat-map__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "concat-map__files", - srcs = glob( - include = ["node_modules/concat-map/**/*"], - exclude = [ - "node_modules/concat-map/test/**", - "node_modules/concat-map/docs/**", - "node_modules/concat-map/**/* */**", - "node_modules/concat-map/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "concat-map__typings", - srcs = glob( - include = ["node_modules/concat-map/**/*.d.ts"], - exclude = [ - "node_modules/concat-map/test/**", - "node_modules/concat-map/docs/**", - "node_modules/concat-map/**/* */**", - "node_modules/concat-map/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "diff", - srcs = [ - ":diff__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "diff__files", - srcs = glob( - include = ["node_modules/diff/**/*"], - exclude = [ - "node_modules/diff/test/**", - "node_modules/diff/docs/**", - "node_modules/diff/**/* */**", - "node_modules/diff/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "diff__typings", - srcs = glob( - include = ["node_modules/diff/**/*.d.ts"], - exclude = [ - "node_modules/diff/test/**", - "node_modules/diff/docs/**", - "node_modules/diff/**/* */**", - "node_modules/diff/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "fs.realpath", - srcs = [ - ":fs.realpath__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "fs.realpath__files", - srcs = glob( - include = ["node_modules/fs.realpath/**/*"], - exclude = [ - "node_modules/fs.realpath/test/**", - "node_modules/fs.realpath/docs/**", - "node_modules/fs.realpath/**/* */**", - "node_modules/fs.realpath/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "fs.realpath__typings", - srcs = glob( - include = ["node_modules/fs.realpath/**/*.d.ts"], - exclude = [ - "node_modules/fs.realpath/test/**", - "node_modules/fs.realpath/docs/**", - "node_modules/fs.realpath/**/* */**", - "node_modules/fs.realpath/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "glob", - srcs = [ - ":glob__files", - ":fs.realpath__files", - ":inflight__files", - ":once__files", - ":wrappy__files", - ":inherits__files", - ":minimatch__files", - ":brace-expansion__files", - ":balanced-match__files", - ":concat-map__files", - ":path-is-absolute__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "glob__files", - srcs = glob( - include = ["node_modules/glob/**/*"], - exclude = [ - "node_modules/glob/test/**", - "node_modules/glob/docs/**", - "node_modules/glob/**/* */**", - "node_modules/glob/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "glob__typings", - srcs = glob( - include = ["node_modules/glob/**/*.d.ts"], - exclude = [ - "node_modules/glob/test/**", - "node_modules/glob/docs/**", - "node_modules/glob/**/* */**", - "node_modules/glob/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inflight", - srcs = [ - ":inflight__files", - ":once__files", - ":wrappy__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inflight__files", - srcs = glob( - include = ["node_modules/inflight/**/*"], - exclude = [ - "node_modules/inflight/test/**", - "node_modules/inflight/docs/**", - "node_modules/inflight/**/* */**", - "node_modules/inflight/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inflight__typings", - srcs = glob( - include = ["node_modules/inflight/**/*.d.ts"], - exclude = [ - "node_modules/inflight/test/**", - "node_modules/inflight/docs/**", - "node_modules/inflight/**/* */**", - "node_modules/inflight/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inherits", - srcs = [ - ":inherits__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inherits__files", - srcs = glob( - include = ["node_modules/inherits/**/*"], - exclude = [ - "node_modules/inherits/test/**", - "node_modules/inherits/docs/**", - "node_modules/inherits/**/* */**", - "node_modules/inherits/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "inherits__typings", - srcs = glob( - include = ["node_modules/inherits/**/*.d.ts"], - exclude = [ - "node_modules/inherits/test/**", - "node_modules/inherits/docs/**", - "node_modules/inherits/**/* */**", - "node_modules/inherits/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "jasmine", - srcs = [ - ":jasmine__files", - ":glob__files", - ":fs.realpath__files", - ":inflight__files", - ":once__files", - ":wrappy__files", - ":inherits__files", - ":minimatch__files", - ":brace-expansion__files", - ":balanced-match__files", - ":concat-map__files", - ":path-is-absolute__files", - ":jasmine-core__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "jasmine__files", - srcs = glob( - include = ["node_modules/jasmine/**/*"], - exclude = [ - "node_modules/jasmine/test/**", - "node_modules/jasmine/docs/**", - "node_modules/jasmine/**/* */**", - "node_modules/jasmine/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "jasmine__typings", - srcs = glob( - include = ["node_modules/jasmine/**/*.d.ts"], - exclude = [ - "node_modules/jasmine/test/**", - "node_modules/jasmine/docs/**", - "node_modules/jasmine/**/* */**", - "node_modules/jasmine/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -nodejs_binary( - name = "jasmine/jasmine", - entry_point = "jasmine/bin/jasmine.js", - install_source_map_support = False, - data = [":jasmine"], -) -filegroup( - name = "jasmine-core", - srcs = [ - ":jasmine-core__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "jasmine-core__files", - srcs = glob( - include = ["node_modules/jasmine-core/**/*"], - exclude = [ - "node_modules/jasmine-core/test/**", - "node_modules/jasmine-core/docs/**", - "node_modules/jasmine-core/**/* */**", - "node_modules/jasmine-core/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "jasmine-core__typings", - srcs = glob( - include = ["node_modules/jasmine-core/**/*.d.ts"], - exclude = [ - "node_modules/jasmine-core/test/**", - "node_modules/jasmine-core/docs/**", - "node_modules/jasmine-core/**/* */**", - "node_modules/jasmine-core/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "minimatch", - srcs = [ - ":minimatch__files", - ":brace-expansion__files", - ":balanced-match__files", - ":concat-map__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "minimatch__files", - srcs = glob( - include = ["node_modules/minimatch/**/*"], - exclude = [ - "node_modules/minimatch/test/**", - "node_modules/minimatch/docs/**", - "node_modules/minimatch/**/* */**", - "node_modules/minimatch/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "minimatch__typings", - srcs = glob( - include = ["node_modules/minimatch/**/*.d.ts"], - exclude = [ - "node_modules/minimatch/test/**", - "node_modules/minimatch/docs/**", - "node_modules/minimatch/**/* */**", - "node_modules/minimatch/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "once", - srcs = [ - ":once__files", - ":wrappy__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "once__files", - srcs = glob( - include = ["node_modules/once/**/*"], - exclude = [ - "node_modules/once/test/**", - "node_modules/once/docs/**", - "node_modules/once/**/* */**", - "node_modules/once/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "once__typings", - srcs = glob( - include = ["node_modules/once/**/*.d.ts"], - exclude = [ - "node_modules/once/test/**", - "node_modules/once/docs/**", - "node_modules/once/**/* */**", - "node_modules/once/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "path-is-absolute", - srcs = [ - ":path-is-absolute__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "path-is-absolute__files", - srcs = glob( - include = ["node_modules/path-is-absolute/**/*"], - exclude = [ - "node_modules/path-is-absolute/test/**", - "node_modules/path-is-absolute/docs/**", - "node_modules/path-is-absolute/**/* */**", - "node_modules/path-is-absolute/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "path-is-absolute__typings", - srcs = glob( - include = ["node_modules/path-is-absolute/**/*.d.ts"], - exclude = [ - "node_modules/path-is-absolute/test/**", - "node_modules/path-is-absolute/docs/**", - "node_modules/path-is-absolute/**/* */**", - "node_modules/path-is-absolute/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "unidiff", - srcs = [ - ":unidiff__files", - ":diff__files", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "unidiff__files", - srcs = glob( - include = ["node_modules/unidiff/**/*"], - exclude = [ - "node_modules/unidiff/test/**", - "node_modules/unidiff/docs/**", - "node_modules/unidiff/**/* */**", - "node_modules/unidiff/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "unidiff__typings", - srcs = glob( - include = ["node_modules/unidiff/**/*.d.ts"], - exclude = [ - "node_modules/unidiff/test/**", - "node_modules/unidiff/docs/**", - "node_modules/unidiff/**/* */**", - "node_modules/unidiff/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "wrappy", - srcs = [ - ":wrappy__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "wrappy__files", - srcs = glob( - include = ["node_modules/wrappy/**/*"], - exclude = [ - "node_modules/wrappy/test/**", - "node_modules/wrappy/docs/**", - "node_modules/wrappy/**/* */**", - "node_modules/wrappy/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "wrappy__typings", - srcs = glob( - include = ["node_modules/wrappy/**/*.d.ts"], - exclude = [ - "node_modules/wrappy/test/**", - "node_modules/wrappy/docs/**", - "node_modules/wrappy/**/* */**", - "node_modules/wrappy/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-a", - srcs = [ - ":@gregmagolan/test-a__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-a__files", - srcs = glob( - include = ["node_modules/@gregmagolan/test-a/**/*"], - exclude = [ - "node_modules/@gregmagolan/test-a/test/**", - "node_modules/@gregmagolan/test-a/docs/**", - "node_modules/@gregmagolan/test-a/**/* */**", - "node_modules/@gregmagolan/test-a/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-a__typings", - srcs = glob( - include = ["node_modules/@gregmagolan/test-a/**/*.d.ts"], - exclude = [ - "node_modules/@gregmagolan/test-a/test/**", - "node_modules/@gregmagolan/test-a/docs/**", - "node_modules/@gregmagolan/test-a/**/* */**", - "node_modules/@gregmagolan/test-a/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-b", - srcs = [ - ":@gregmagolan/test-b__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-b__files", - srcs = glob( - include = ["node_modules/@gregmagolan/test-b/**/*"], - exclude = [ - "node_modules/@gregmagolan/test-b/test/**", - "node_modules/@gregmagolan/test-b/docs/**", - "node_modules/@gregmagolan/test-b/**/* */**", - "node_modules/@gregmagolan/test-b/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan/test-b__typings", - srcs = glob( - include = ["node_modules/@gregmagolan/test-b/**/*.d.ts"], - exclude = [ - "node_modules/@gregmagolan/test-b/test/**", - "node_modules/@gregmagolan/test-b/docs/**", - "node_modules/@gregmagolan/test-b/**/* */**", - "node_modules/@gregmagolan/test-b/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@yarnpkg/lockfile", - srcs = [ - ":@yarnpkg/lockfile__files", - - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@yarnpkg/lockfile__files", - srcs = glob( - include = ["node_modules/@yarnpkg/lockfile/**/*"], - exclude = [ - "node_modules/@yarnpkg/lockfile/test/**", - "node_modules/@yarnpkg/lockfile/docs/**", - "node_modules/@yarnpkg/lockfile/**/* */**", - "node_modules/@yarnpkg/lockfile/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@yarnpkg/lockfile__typings", - srcs = glob( - include = ["node_modules/@yarnpkg/lockfile/**/*.d.ts"], - exclude = [ - "node_modules/@yarnpkg/lockfile/test/**", - "node_modules/@yarnpkg/lockfile/docs/**", - "node_modules/@yarnpkg/lockfile/**/* */**", - "node_modules/@yarnpkg/lockfile/**/* *", - ], - ), - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@gregmagolan", - srcs = [ - ":@gregmagolan/test-a", - ":@gregmagolan/test-b", - ], - tags = ["NODE_MODULE_MARKER"], -) -filegroup( - name = "@yarnpkg", - srcs = [ - ":@yarnpkg/lockfile", - ], - tags = ["NODE_MODULE_MARKER"], -) diff --git a/internal/npm_install/test/check.js b/internal/npm_install/test/check.js index 9dd8629b18..660f491ac5 100644 --- a/internal/npm_install/test/check.js +++ b/internal/npm_install/test/check.js @@ -3,10 +3,7 @@ const fs = require('fs'); const path = require('path'); const unidiff = require('unidiff') -const actual = 'BUILD.bazel'; -const golden = 'BUILD.bazel.golden'; - -function check(updateGolden = false) { +function check(actual, updateGolden = false) { // We must change the directory to the BUILD file path // so the generator is able to run process.chdir(path.dirname(__filename)); @@ -24,6 +21,7 @@ function check(updateGolden = false) { .replace(/[\n]+/g, '\n'); // Load the golden file for comparison + const golden = path.posix.join('golden', actual + '.golden'); const goldenContents = fs.readFileSync(golden, {encoding: 'utf-8'}).replace(/\r\n/g, '\n'); // Check if actualContents matches golden file @@ -48,4 +46,23 @@ Update the golden file: } } -module.exports = check; \ No newline at end of file +module.exports = { + check, + files: [ + 'BUILD.bazel', + '@gregmagolan/BUILD.bazel', + '@gregmagolan/test-a/BUILD.bazel', + '@gregmagolan/test-a/bin/BUILD.bazel', + '@gregmagolan/test-b/BUILD.bazel', + '@gregmagolan/test-b/bin/BUILD.bazel', + 'jasmine/BUILD.bazel', + 'jasmine/bin/BUILD.bazel', + 'unidiff/BUILD.bazel', + 'unidiff/bin/BUILD.bazel', + 'node_modules/@gregmagolan/BUILD.bazel', + 'node_modules/@gregmagolan/test-a/BUILD.bazel', + 'node_modules/@gregmagolan/test-b/BUILD.bazel', + 'node_modules/jasmine/BUILD.bazel', + 'node_modules/unidiff/BUILD.bazel', + ], +}; \ No newline at end of file diff --git a/internal/npm_install/test/generate_build_file.spec.js b/internal/npm_install/test/generate_build_file.spec.js index dd1fa56e9b..81fe61e090 100644 --- a/internal/npm_install/test/generate_build_file.spec.js +++ b/internal/npm_install/test/generate_build_file.spec.js @@ -1,7 +1,9 @@ -const check = require('./check'); +const {check, files} = require('./check'); describe('build file generator', () => { - it('should produce a BUILD file from the node_modules file structure', () => { - check(); + files.forEach(file => { + it(`should produce a BUILD file for ${file}`, () => { + check(file); + }); }); }); diff --git a/internal/npm_install/test/golden/@gregmagolan/BUILD.bazel.golden b/internal/npm_install/test/golden/@gregmagolan/BUILD.bazel.golden new file mode 100644 index 0000000000..050cd0a460 --- /dev/null +++ b/internal/npm_install/test/golden/@gregmagolan/BUILD.bazel.golden @@ -0,0 +1,6 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "@gregmagolan", + actual = "//node_modules/@gregmagolan", +) diff --git a/internal/npm_install/test/golden/@gregmagolan/test-a/BUILD.bazel.golden b/internal/npm_install/test/golden/@gregmagolan/test-a/BUILD.bazel.golden new file mode 100644 index 0000000000..d97670cc49 --- /dev/null +++ b/internal/npm_install/test/golden/@gregmagolan/test-a/BUILD.bazel.golden @@ -0,0 +1,18 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "test-a", + actual = "//node_modules/@gregmagolan/test-a:test-a__pkg" +) +alias( + name = "test-a__files", + actual = "//node_modules/@gregmagolan/test-a:test-a__files" +) +alias( + name = "test-a__lite", + actual = "//node_modules/@gregmagolan/test-a:test-a__lite" +) +alias( + name = "test-a__typings", + actual = "//node_modules/@gregmagolan/test-a:test-a__typings" +) diff --git a/internal/npm_install/test/golden/@gregmagolan/test-a/bin/BUILD.bazel.golden b/internal/npm_install/test/golden/@gregmagolan/test-a/bin/BUILD.bazel.golden new file mode 100644 index 0000000000..ec33b04a28 --- /dev/null +++ b/internal/npm_install/test/golden/@gregmagolan/test-a/bin/BUILD.bazel.golden @@ -0,0 +1,2 @@ + +package(default_visibility = ["//visibility:public"]) diff --git a/internal/npm_install/test/golden/@gregmagolan/test-b/BUILD.bazel.golden b/internal/npm_install/test/golden/@gregmagolan/test-b/BUILD.bazel.golden new file mode 100644 index 0000000000..b8e465adf7 --- /dev/null +++ b/internal/npm_install/test/golden/@gregmagolan/test-b/BUILD.bazel.golden @@ -0,0 +1,18 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "test-b", + actual = "//node_modules/@gregmagolan/test-b:test-b__pkg" +) +alias( + name = "test-b__files", + actual = "//node_modules/@gregmagolan/test-b:test-b__files" +) +alias( + name = "test-b__lite", + actual = "//node_modules/@gregmagolan/test-b:test-b__lite" +) +alias( + name = "test-b__typings", + actual = "//node_modules/@gregmagolan/test-b:test-b__typings" +) diff --git a/internal/npm_install/test/golden/@gregmagolan/test-b/bin/BUILD.bazel.golden b/internal/npm_install/test/golden/@gregmagolan/test-b/bin/BUILD.bazel.golden new file mode 100644 index 0000000000..ec33b04a28 --- /dev/null +++ b/internal/npm_install/test/golden/@gregmagolan/test-b/bin/BUILD.bazel.golden @@ -0,0 +1,2 @@ + +package(default_visibility = ["//visibility:public"]) diff --git a/internal/npm_install/test/golden/BUILD.bazel.golden b/internal/npm_install/test/golden/BUILD.bazel.golden new file mode 100644 index 0000000000..d5a6056d42 --- /dev/null +++ b/internal/npm_install/test/golden/BUILD.bazel.golden @@ -0,0 +1,25 @@ + +package(default_visibility = ["//visibility:public"]) +filegroup( + name = "node_modules", + srcs = glob(["node_modules/.bin/*"]) + [ + "//node_modules/balanced-match:balanced-match__lite", + "//node_modules/brace-expansion:brace-expansion__lite", + "//node_modules/concat-map:concat-map__lite", + "//node_modules/diff:diff__lite", + "//node_modules/fs.realpath:fs.realpath__lite", + "//node_modules/glob:glob__lite", + "//node_modules/inflight:inflight__lite", + "//node_modules/inherits:inherits__lite", + "//node_modules/jasmine:jasmine__lite", + "//node_modules/jasmine-core:jasmine-core__lite", + "//node_modules/minimatch:minimatch__lite", + "//node_modules/once:once__lite", + "//node_modules/path-is-absolute:path-is-absolute__lite", + "//node_modules/unidiff:unidiff__lite", + "//node_modules/wrappy:wrappy__lite", + "//node_modules/@gregmagolan/test-a:test-a__lite", + "//node_modules/@gregmagolan/test-b:test-b__lite", + "//node_modules/@yarnpkg/lockfile:lockfile__lite", + ], +) diff --git a/internal/npm_install/test/golden/jasmine/BUILD.bazel.golden b/internal/npm_install/test/golden/jasmine/BUILD.bazel.golden new file mode 100644 index 0000000000..0a585f0158 --- /dev/null +++ b/internal/npm_install/test/golden/jasmine/BUILD.bazel.golden @@ -0,0 +1,18 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "jasmine", + actual = "//node_modules/jasmine:jasmine__pkg" +) +alias( + name = "jasmine__files", + actual = "//node_modules/jasmine:jasmine__files" +) +alias( + name = "jasmine__lite", + actual = "//node_modules/jasmine:jasmine__lite" +) +alias( + name = "jasmine__typings", + actual = "//node_modules/jasmine:jasmine__typings" +) diff --git a/internal/npm_install/test/golden/jasmine/bin/BUILD.bazel.golden b/internal/npm_install/test/golden/jasmine/bin/BUILD.bazel.golden new file mode 100644 index 0000000000..7e20318fcd --- /dev/null +++ b/internal/npm_install/test/golden/jasmine/bin/BUILD.bazel.golden @@ -0,0 +1,6 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "jasmine", + actual = "//node_modules/jasmine:jasmine__bin", +) diff --git a/internal/npm_install/test/golden/node_modules/@gregmagolan/BUILD.bazel.golden b/internal/npm_install/test/golden/node_modules/@gregmagolan/BUILD.bazel.golden new file mode 100644 index 0000000000..f7142117e9 --- /dev/null +++ b/internal/npm_install/test/golden/node_modules/@gregmagolan/BUILD.bazel.golden @@ -0,0 +1,10 @@ + +package(default_visibility = ["//visibility:public"]) +filegroup( + name = "@gregmagolan", + srcs = [ + "//node_modules/@gregmagolan/test-a", + "//node_modules/@gregmagolan/test-b", + ], + tags = ["NODE_MODULE_MARKER"], +) diff --git a/internal/npm_install/test/golden/node_modules/@gregmagolan/test-a/BUILD.bazel.golden b/internal/npm_install/test/golden/node_modules/@gregmagolan/test-a/BUILD.bazel.golden new file mode 100644 index 0000000000..942aa63cbe --- /dev/null +++ b/internal/npm_install/test/golden/node_modules/@gregmagolan/test-a/BUILD.bazel.golden @@ -0,0 +1,56 @@ + +package(default_visibility = ["//visibility:public"]) +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") +filegroup( + name = "test-a__pkg", + srcs = [ + ":test-a__files", + + ], + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-a__files", + srcs = glob( + include = ["**/*"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-a__lite", + srcs = glob( + include = [ + "**/*.js", + "**/*.d.ts", + "**/*.json", + "**/*.proto", + "bin/**/*", + ], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-a__typings", + srcs = glob( + include = ["**/*.d.ts"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) diff --git a/internal/npm_install/test/golden/node_modules/@gregmagolan/test-b/BUILD.bazel.golden b/internal/npm_install/test/golden/node_modules/@gregmagolan/test-b/BUILD.bazel.golden new file mode 100644 index 0000000000..e52f7fef46 --- /dev/null +++ b/internal/npm_install/test/golden/node_modules/@gregmagolan/test-b/BUILD.bazel.golden @@ -0,0 +1,56 @@ + +package(default_visibility = ["//visibility:public"]) +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") +filegroup( + name = "test-b__pkg", + srcs = [ + ":test-b__files", + + ], + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-b__files", + srcs = glob( + include = ["**/*"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-b__lite", + srcs = glob( + include = [ + "**/*.js", + "**/*.d.ts", + "**/*.json", + "**/*.proto", + "bin/**/*", + ], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "test-b__typings", + srcs = glob( + include = ["**/*.d.ts"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) diff --git a/internal/npm_install/test/golden/node_modules/jasmine/BUILD.bazel.golden b/internal/npm_install/test/golden/node_modules/jasmine/BUILD.bazel.golden new file mode 100644 index 0000000000..06444f366a --- /dev/null +++ b/internal/npm_install/test/golden/node_modules/jasmine/BUILD.bazel.golden @@ -0,0 +1,73 @@ + +package(default_visibility = ["//visibility:public"]) +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") +filegroup( + name = "jasmine__pkg", + srcs = [ + ":jasmine__files", + "//node_modules/glob:glob__files", + "//node_modules/fs.realpath:fs.realpath__files", + "//node_modules/inflight:inflight__files", + "//node_modules/once:once__files", + "//node_modules/wrappy:wrappy__files", + "//node_modules/inherits:inherits__files", + "//node_modules/minimatch:minimatch__files", + "//node_modules/brace-expansion:brace-expansion__files", + "//node_modules/balanced-match:balanced-match__files", + "//node_modules/concat-map:concat-map__files", + "//node_modules/path-is-absolute:path-is-absolute__files", + "//node_modules/jasmine-core:jasmine-core__files", + ], + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "jasmine__files", + srcs = glob( + include = ["**/*"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "jasmine__lite", + srcs = glob( + include = [ + "**/*.js", + "**/*.d.ts", + "**/*.json", + "**/*.proto", + "bin/**/*", + ], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "jasmine__typings", + srcs = glob( + include = ["**/*.d.ts"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +nodejs_binary( + name = "jasmine__bin", + entry_point = "jasmine/bin/jasmine.js", + install_source_map_support = False, + data = [":jasmine__pkg"], +) diff --git a/internal/npm_install/test/golden/node_modules/unidiff/BUILD.bazel.golden b/internal/npm_install/test/golden/node_modules/unidiff/BUILD.bazel.golden new file mode 100644 index 0000000000..5c12f994cf --- /dev/null +++ b/internal/npm_install/test/golden/node_modules/unidiff/BUILD.bazel.golden @@ -0,0 +1,56 @@ + +package(default_visibility = ["//visibility:public"]) +load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary") +filegroup( + name = "unidiff__pkg", + srcs = [ + ":unidiff__files", + "//node_modules/diff:diff__files", + ], + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "unidiff__files", + srcs = glob( + include = ["**/*"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "unidiff__lite", + srcs = glob( + include = [ + "**/*.js", + "**/*.d.ts", + "**/*.json", + "**/*.proto", + "bin/**/*", + ], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) +filegroup( + name = "unidiff__typings", + srcs = glob( + include = ["**/*.d.ts"], + exclude = [ + "test/**", + "docs/**", + "**/* */**", + "**/* *", + ], + ), + tags = ["NODE_MODULE_MARKER"], +) diff --git a/internal/npm_install/test/golden/unidiff/BUILD.bazel.golden b/internal/npm_install/test/golden/unidiff/BUILD.bazel.golden new file mode 100644 index 0000000000..50a7902938 --- /dev/null +++ b/internal/npm_install/test/golden/unidiff/BUILD.bazel.golden @@ -0,0 +1,18 @@ + +package(default_visibility = ["//visibility:public"]) +alias( + name = "unidiff", + actual = "//node_modules/unidiff:unidiff__pkg" +) +alias( + name = "unidiff__files", + actual = "//node_modules/unidiff:unidiff__files" +) +alias( + name = "unidiff__lite", + actual = "//node_modules/unidiff:unidiff__lite" +) +alias( + name = "unidiff__typings", + actual = "//node_modules/unidiff:unidiff__typings" +) diff --git a/internal/npm_install/test/golden/unidiff/bin/BUILD.bazel.golden b/internal/npm_install/test/golden/unidiff/bin/BUILD.bazel.golden new file mode 100644 index 0000000000..ec33b04a28 --- /dev/null +++ b/internal/npm_install/test/golden/unidiff/bin/BUILD.bazel.golden @@ -0,0 +1,2 @@ + +package(default_visibility = ["//visibility:public"]) diff --git a/internal/npm_install/test/update_golden.js b/internal/npm_install/test/update_golden.js index f2224532c4..ce22df4630 100644 --- a/internal/npm_install/test/update_golden.js +++ b/internal/npm_install/test/update_golden.js @@ -1,3 +1,3 @@ -const check = require('./check'); +const {check, files} = require('./check'); -check(true); +files.forEach(file => check(file, true)); diff --git a/internal/npm_install/test/yarn.lock b/internal/npm_install/test/yarn.lock index 1c91ad5093..4acacd1b8a 100644 --- a/internal/npm_install/test/yarn.lock +++ b/internal/npm_install/test/yarn.lock @@ -17,8 +17,8 @@ "@gregmagolan/test-a" "0.0.1" "@yarnpkg/lockfile@^1.0.2": - version "1.0.2" - resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.0.2.tgz#833d163680a151d2441a2489f5fe5fa87ac87726" + version "1.1.0" + resolved "https://registry.yarnpkg.com/@yarnpkg/lockfile/-/lockfile-1.1.0.tgz#e77a97fbd345b76d83245edcd17d393b1b41fb31" balanced-match@^1.0.0: version "1.0.0" @@ -44,8 +44,8 @@ fs.realpath@^1.0.0: resolved "https://registry.yarnpkg.com/fs.realpath/-/fs.realpath-1.0.0.tgz#1504ad2523158caa40db4a2787cb01411994ea4f" glob@^7.0.6: - version "7.1.2" - resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.2.tgz#c19c9df9a028702d678612384a6552404c636d15" + version "7.1.3" + resolved "https://registry.yarnpkg.com/glob/-/glob-7.1.3.tgz#3960832d3f1574108342dafd3a67b332c0969df1" dependencies: fs.realpath "^1.0.0" inflight "^1.0.4" diff --git a/internal/rollup/rollup_bundle.bzl b/internal/rollup/rollup_bundle.bzl index 8ae263bc03..783e02108b 100644 --- a/internal/rollup/rollup_bundle.bzl +++ b/internal/rollup/rollup_bundle.bzl @@ -513,8 +513,8 @@ ROLLUP_ATTRS = { name = "bundle", ... deps = [ - "@npm//:foo", - "@npm//:bar", + "@npm//foo", + "@npm//bar", ... ], )