Skip to content

Commit 7ebadf5

Browse files
authored
fix: handle multiple exclude patterns in exclude package contents (#2131)
1 parent fbb2456 commit 7ebadf5

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

e2e/npm_translate_lock_exclude_package_contents/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ npm_link_all_packages(name = "node_modules")
44

55
sh_test(
66
name = "test_sh",
7-
srcs = ["is-odd-exclude-test.sh"],
7+
srcs = ["exclude-test.sh"],
88
data = [
99
":node_modules/is-odd",
1010
],

e2e/npm_translate_lock_exclude_package_contents/MODULE.bazel

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ npm.npm_translate_lock(
1717
name = "npm",
1818
data = ["//:package.json"],
1919
exclude_package_contents = {
20-
"[email protected]": ["**/README*"],
20+
21+
"**/README*",
22+
"",
23+
"**/LICENSE*",
24+
],
2125
},
2226
npmrc = "//:.npmrc",
2327
pnpm_lock = "//:pnpm-lock.yaml",

e2e/npm_translate_lock_exclude_package_contents/WORKSPACE

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@ load("@aspect_rules_js//npm:repositories.bzl", "npm_translate_lock")
1616
npm_translate_lock(
1717
name = "npm",
1818
exclude_package_contents = {
19-
"[email protected]": ["**/README*"],
19+
20+
"**/README*",
21+
"",
22+
"**/LICENSE*",
23+
],
2024
},
2125
npmrc = "//:.npmrc",
2226
pnpm_lock = "//:pnpm-lock.yaml",

e2e/npm_translate_lock_exclude_package_contents/is-odd-exclude-test.sh renamed to e2e/npm_translate_lock_exclude_package_contents/exclude-test.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ if ls "$TARGET_DIR"/README* >/dev/null 2>&1; then
2121
exit 1
2222
fi
2323

24+
# Check if any LICENSE file exists
25+
if ls "$TARGET_DIR"/LICENSE* >/dev/null 2>&1; then
26+
echo "Error: LICENSE file found in $TARGET_DIR, exclusion did not work."
27+
exit 1
28+
fi
29+
2430
echo "All tests passed"

npm/private/npm_import.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,16 @@ def _download_and_extract_archive(rctx, package_json_only):
455455
msg = "Failed to create package directory. '{}' exited with {}: \nSTDOUT:\n{}\nSTDERR:\n{}".format(" ".join(mkdir_args), result.return_code, result.stdout, result.stderr)
456456
fail(msg)
457457

458+
exclude_pattern_args = []
459+
if rctx.attr.exclude_package_contents:
460+
for pattern in rctx.attr.exclude_package_contents:
461+
if pattern == "":
462+
continue
463+
exclude_pattern_args.append("--exclude")
464+
exclude_pattern_args.append(pattern)
465+
458466
# npm packages are always published with one top-level directory inside the tarball, tho the name is not predictable
459467
# so we use tar here which takes a --strip-components N argument instead of rctx.download_and_extract
460-
exclude_pattern_args = ["--exclude", rctx.attr.exclude_package_contents] if rctx.attr.exclude_package_contents else []
461468
tar_args = ["tar", "-xf", _TARBALL_FILENAME] + ["--strip-components", "1", "-C", _EXTRACT_TO_DIRNAME, "--no-same-owner", "--no-same-permissions"] + exclude_pattern_args
462469

463470
system_tar = detect_system_tar(rctx) if rctx.attr.system_tar == "auto" else rctx.attr.system_tar

npm/private/npm_package_store.bzl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,15 @@ def _npm_package_store_impl(ctx):
229229
# tar to strip one directory level. Some packages have directory permissions missing
230230
# executable which make the directories not listable ([email protected] for example).
231231
bsdtar = ctx.toolchains["@aspect_bazel_lib//lib:tar_toolchain_type"]
232-
tar_exclude_package_contents = (["--exclude"] + ctx.attr.exclude_package_contents) if ctx.attr.exclude_package_contents else []
232+
233+
tar_exclude_package_contents = []
234+
if ctx.attr.exclude_package_contents:
235+
for pattern in ctx.attr.exclude_package_contents:
236+
if pattern == "":
237+
continue
238+
tar_exclude_package_contents.append("--exclude")
239+
tar_exclude_package_contents.append(pattern)
240+
233241
ctx.actions.run(
234242
executable = bsdtar.tarinfo.binary,
235243
inputs = depset(direct = [src], transitive = [bsdtar.default.files]),

0 commit comments

Comments
 (0)