From d86f6f5aedc1bc0a72404b00fc70f2c471a4741e Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Mon, 21 Jun 2021 13:53:51 -0700 Subject: [PATCH] feat(builtin): add validate attribute on pkg_npm Currently just validates that the package_name attribute matches the name in package.json (if there is one) Fixes #2782 --- internal/pkg_npm/packager.js | 17 ++++++++++++++++- internal/pkg_npm/pkg_npm.bzl | 8 ++++++++ internal/pkg_npm/test/BUILD.bazel | 1 + internal/pkg_npm/test/linking/bar/BUILD.bazel | 1 + internal/pkg_npm/test/linking/feb/BUILD.bazel | 1 + internal/pkg_npm/test/linking/fez/BUILD.bazel | 1 + internal/pkg_npm/test/linking/foo/BUILD.bazel | 1 + internal/pkg_npm/test/linking/fub/BUILD.bazel | 1 + internal/pkg_npm/test/tgz_out/BUILD.bazel | 1 + 9 files changed, 31 insertions(+), 1 deletion(-) diff --git a/internal/pkg_npm/packager.js b/internal/pkg_npm/packager.js index f9041a3a92..b24c1b72cb 100644 --- a/internal/pkg_npm/packager.js +++ b/internal/pkg_npm/packager.js @@ -93,7 +93,7 @@ function main(args) { args = fs.readFileSync(args[0], {encoding: 'utf-8'}).split('\n').map(unquoteArgs); const [outDir, baseDir, srcsArg, binDir, genDir, depsArg, packagesArg, substitutionsArg, - volatileFile, infoFile, vendorExternalArg] = args; + volatileFile, infoFile, vendorExternalArg, target, validate, packageNameArg] = args; const substitutions = [ // Strip content between BEGIN-INTERNAL / END-INTERNAL comments @@ -140,6 +140,21 @@ function main(args) { `${src} in 'srcs' does not reside in the base directory, ` + `generated file should belong in 'deps' instead.`); } + + if (validate === "true" && path.relative(baseDir, src) === "package.json") { + const packageJson = JSON.parse(fs.readFileSync(src)); + if (packageJson['name'] !== packageNameArg) { + console.error(`ERROR: pkg_npm rule ${ + target} was configured with attributes that don't match the package.json`); + console.error(` - attribute package_name=${packageNameArg} does not match package.json name=${packageJson['name']}`) + console.error('You can automatically fix this by running:'); + console.error( + ` npx @bazel/buildozer 'set package_name "${packageJson['name']}"' ${target}`); + console.error('Or to suppress this error, run:'); + console.error(` npx @bazel/buildozer 'set validate False' ${target}`); + return 1; + } + } copyWithReplace(src, path.join(outDir, path.relative(baseDir, src)), substitutions); } } diff --git a/internal/pkg_npm/pkg_npm.bzl b/internal/pkg_npm/pkg_npm.bzl index dacf2590f2..33dadc5e3c 100644 --- a/internal/pkg_npm/pkg_npm.bzl +++ b/internal/pkg_npm/pkg_npm.bzl @@ -129,6 +129,11 @@ See the section on stamping in the [README](stamping) NOTE: If this attribute is set, a valid `package.json` file must be included in the sources of this target """, ), + "validate": attr.bool( + doc = "Whether to check that the attributes match the package.json", + # TODO(4.0) flip default to True + default = False, + ), "vendor_external": attr.string_list( doc = """External workspaces whose contents should be vendored into this workspace. Avoids `external/foo` path segments in the resulting package.""", @@ -245,6 +250,9 @@ def create_package(ctx, deps_files, nested_packages): args.add_all(["", ""]) args.add_joined(ctx.attr.vendor_external, join_with = ",", omit_if_empty = False) + args.add(str(ctx.label)) + args.add(ctx.attr.validate) + args.add(ctx.attr.package_name) ctx.actions.run( progress_message = "Assembling npm package %s" % package_dir.short_path, diff --git a/internal/pkg_npm/test/BUILD.bazel b/internal/pkg_npm/test/BUILD.bazel index 3b33003e0a..b554b3d16e 100644 --- a/internal/pkg_npm/test/BUILD.bazel +++ b/internal/pkg_npm/test/BUILD.bazel @@ -40,6 +40,7 @@ node_context_data( pkg_npm( name = "test_pkg", + package_name = "test-pkg", srcs = [ "package.json", "some_file", diff --git a/internal/pkg_npm/test/linking/bar/BUILD.bazel b/internal/pkg_npm/test/linking/bar/BUILD.bazel index 183b885f5d..404b7c0bcd 100644 --- a/internal/pkg_npm/test/linking/bar/BUILD.bazel +++ b/internal/pkg_npm/test/linking/bar/BUILD.bazel @@ -19,5 +19,6 @@ pkg_npm( "main.js", "package.json", ], + validate = False, visibility = ["//internal/pkg_npm/test/linking:__pkg__"], ) diff --git a/internal/pkg_npm/test/linking/feb/BUILD.bazel b/internal/pkg_npm/test/linking/feb/BUILD.bazel index 85ad50eb90..8e72b4380b 100644 --- a/internal/pkg_npm/test/linking/feb/BUILD.bazel +++ b/internal/pkg_npm/test/linking/feb/BUILD.bazel @@ -21,6 +21,7 @@ pkg_npm( name = "scoped_feb", package_name = "@scoped/feb", srcs = ["package.json"], + validate = False, visibility = ["//internal/pkg_npm/test/linking:__pkg__"], deps = [":feb_lib"], ) diff --git a/internal/pkg_npm/test/linking/fez/BUILD.bazel b/internal/pkg_npm/test/linking/fez/BUILD.bazel index ead4a4a2cb..411747dd28 100644 --- a/internal/pkg_npm/test/linking/fez/BUILD.bazel +++ b/internal/pkg_npm/test/linking/fez/BUILD.bazel @@ -18,6 +18,7 @@ pkg_npm( pkg_npm( name = "scoped_fez", package_name = "@scoped/fez", + validate = False, visibility = ["//internal/pkg_npm/test/linking:__pkg__"], deps = [":fez_lib"], ) diff --git a/internal/pkg_npm/test/linking/foo/BUILD.bazel b/internal/pkg_npm/test/linking/foo/BUILD.bazel index aeee98166e..9b305a2cd2 100644 --- a/internal/pkg_npm/test/linking/foo/BUILD.bazel +++ b/internal/pkg_npm/test/linking/foo/BUILD.bazel @@ -36,6 +36,7 @@ pkg_npm( pkg_npm( name = "scoped_foo", package_name = "@scoped/foo", + validate = False, visibility = ["//internal/pkg_npm/test/linking:__pkg__"], deps = [":scoped_foo_lib"], ) diff --git a/internal/pkg_npm/test/linking/fub/BUILD.bazel b/internal/pkg_npm/test/linking/fub/BUILD.bazel index 46d7cc84f8..eedfbb9736 100644 --- a/internal/pkg_npm/test/linking/fub/BUILD.bazel +++ b/internal/pkg_npm/test/linking/fub/BUILD.bazel @@ -32,6 +32,7 @@ pkg_npm( name = "scoped_fub", package_name = "@scoped/fub", srcs = ["package.json"], + validate = False, visibility = ["//internal/pkg_npm/test/linking:__pkg__"], deps = [":fub_lib"], ) diff --git a/internal/pkg_npm/test/tgz_out/BUILD.bazel b/internal/pkg_npm/test/tgz_out/BUILD.bazel index d944d87e0f..5fb098aacf 100644 --- a/internal/pkg_npm/test/tgz_out/BUILD.bazel +++ b/internal/pkg_npm/test/tgz_out/BUILD.bazel @@ -2,6 +2,7 @@ load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm") pkg_npm( name = "pkg", + package_name = "awesome-package", srcs = [ "main.js", "package.json",