Skip to content

Commit

Permalink
feat(builtin): add validate attribute on pkg_npm
Browse files Browse the repository at this point in the history
Currently just validates that the package_name attribute matches the name in package.json (if there is one)

Fixes bazel-contrib#2782
  • Loading branch information
alexeagle authored and twheys committed Jan 13, 2022
1 parent ea365ae commit b4aae38
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 1 deletion.
17 changes: 16 additions & 1 deletion internal/pkg_npm/packager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
Expand Down
8 changes: 8 additions & 0 deletions internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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.""",
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions internal/pkg_npm/test/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ node_context_data(

pkg_npm(
name = "test_pkg",
package_name = "test-pkg",
srcs = [
"package.json",
"some_file",
Expand Down
1 change: 1 addition & 0 deletions internal/pkg_npm/test/linking/bar/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ pkg_npm(
"main.js",
"package.json",
],
validate = False,
visibility = ["//internal/pkg_npm/test/linking:__pkg__"],
)
1 change: 1 addition & 0 deletions internal/pkg_npm/test/linking/feb/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
1 change: 1 addition & 0 deletions internal/pkg_npm/test/linking/fez/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
1 change: 1 addition & 0 deletions internal/pkg_npm/test/linking/foo/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
1 change: 1 addition & 0 deletions internal/pkg_npm/test/linking/fub/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
1 change: 1 addition & 0 deletions internal/pkg_npm/test/tgz_out/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit b4aae38

Please sign in to comment.