Skip to content

Commit 4d8f15c

Browse files
authored
fix(builtin): only generate a .tar pkg_npm output when requested (#2428)
1 parent 5463416 commit 4d8f15c

File tree

6 files changed

+110
-19
lines changed

6 files changed

+110
-19
lines changed

internal/pkg_npm/pkg_npm.bzl

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,18 @@ You can pass arguments to npm by escaping them from Bazel using a double-hyphen,
7171
7272
`bazel run my_package.publish -- --tag=next`
7373
74-
It is also possible to use the resulting tar file file from the `.pack` as an action input via the `.tar` label:
74+
It is also possible to use the resulting tar file file from the `.pack` as an action input via the `.tar` label.
75+
To make use of this label, the `tgz` attribute must be set, and the generating `pkg_npm` rule must have a valid `package.json` file
76+
as part of its sources:
7577
7678
```python
79+
pkg_npm(
80+
name = "my_package",
81+
srcs = ["package.json"],
82+
deps = [":my_typescript_lib"],
83+
tgz = "my_package.tgz",
84+
)
85+
7786
my_rule(
7887
name = "foo",
7988
srcs = [
@@ -109,6 +118,12 @@ You can use values from the workspace status command using curly braces, for exa
109118
See the section on stamping in the [README](stamping)
110119
""",
111120
),
121+
"tgz": attr.string(
122+
doc = """If set, will create a `.tgz` file that can be used as an input to another rule, the tar will be given the name assigned to this attribute.
123+
124+
NOTE: If this attribute is set, a valid `package.json` file must be included in the sources of this target
125+
""",
126+
),
112127
"vendor_external": attr.string_list(
113128
doc = """External workspaces whose contents should be vendored into this workspace.
114129
Avoids `external/foo` path segments in the resulting package.""",
@@ -313,7 +328,14 @@ pkg_npm = rule(
313328
outputs = PKG_NPM_OUTPUTS,
314329
)
315330

316-
def pkg_npm_macro(name, **kwargs):
331+
def pkg_npm_macro(name, tgz = None, **kwargs):
332+
"""Wrapper macro around pkg_npm
333+
334+
Args:
335+
name: Unique name for this target
336+
tgz: If provided, creates a `.tar` target that can be used as an action input version of `.pack`
337+
**kwargs: All other args forwarded to pkg_npm
338+
"""
317339
pkg_npm(
318340
name = name,
319341
**kwargs
@@ -335,20 +357,25 @@ def pkg_npm_macro(name, **kwargs):
335357
}),
336358
)
337359

338-
native.genrule(
339-
name = "%s.tar" % name,
340-
outs = ["%s.tgz" % name],
341-
cmd = "$(location :%s.pack) | xargs -I {} cp {} $@" % name,
342-
# NOTE(mattem): on windows, it seems to output a buch of other stuff on stdout when piping, so pipe to tail
343-
# and grab the last line
344-
cmd_bat = "$(location :%s.pack) | tail -1 | xargs -I {} cp {} $@" % name,
345-
tools = [
346-
":%s.pack" % name,
347-
],
348-
# tagged as manual so this doesn't case two actions for each input with builds for "host" (as used as a tool)
349-
tags = [
350-
"local",
351-
"manual",
352-
],
353-
visibility = kwargs.get("visibility"),
354-
)
360+
if tgz != None:
361+
if not tgz.endswith(".tgz"):
362+
fail("tgz output for pkg_npm %s must produce a .tgz file" % name)
363+
364+
native.genrule(
365+
name = "%s.tar" % name,
366+
outs = [tgz],
367+
cmd = "$(location :%s.pack) | xargs -I {} cp {} $@" % name,
368+
# NOTE(mattem): on windows, it seems to output a buch of other stuff on stdout when piping, so pipe to tail
369+
# and grab the last line
370+
cmd_bat = "$(location :%s.pack) | tail -1 | xargs -I {} cp {} $@" % name,
371+
srcs = [
372+
name,
373+
],
374+
tools = [
375+
":%s.pack" % name,
376+
],
377+
tags = [
378+
"local",
379+
],
380+
visibility = kwargs.get("visibility"),
381+
)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
2+
3+
pkg_npm(
4+
name = "pkg",
5+
srcs = [
6+
"main.js",
7+
"package.json",
8+
],
9+
tgz = "my_tar.tgz",
10+
)
11+
12+
sh_test(
13+
name = "pkg_test",
14+
size = "small",
15+
srcs = [
16+
"test.sh",
17+
],
18+
data = [
19+
"my_tar.tgz",
20+
"//third_party/github.com/bazelbuild/bazel-skylib:tests/unittest.bash",
21+
],
22+
# Disabled on windows due to how tar interprets the colons in file paths as remotes, and the --force-local
23+
# only being an option in GNU tar...
24+
# This feature has additional coverage as it used by all bazel tests for examples
25+
tags = [
26+
"fix-windows",
27+
],
28+
deps = [
29+
"@build_bazel_rules_nodejs//third_party/github.com/bazelbuild/bazel/tools/bash/runfiles",
30+
],
31+
)

internal/pkg_npm/test/tgz_out/main.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const MAIN = 'MAIN';
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "awesome-package",
3+
"private": true,
4+
"version": "0.0.0-PLACEHOLDER"
5+
}

internal/pkg_npm/test/tgz_out/test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# --- begin runfiles.bash initialization v2 ---
2+
# Copy-pasted from the Bazel Bash runfiles library v2.
3+
set -uo pipefail; f=build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel/tools/bash/runfiles/runfiles.bash
4+
source "${RUNFILES_DIR:-/dev/null}/$f" 2>/dev/null || \
5+
source "$(grep -sm1 "^$f " "${RUNFILES_MANIFEST_FILE:-/dev/null}" | cut -f2- -d' ')" 2>/dev/null || \
6+
source "$0.runfiles/$f" 2>/dev/null || \
7+
source "$(grep -sm1 "^$f " "$0.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
8+
source "$(grep -sm1 "^$f " "$0.exe.runfiles_manifest" | cut -f2- -d' ')" 2>/dev/null || \
9+
{ echo>&2 "ERROR: cannot find $f"; exit 1; }; f=; set -e
10+
# --- end runfiles.bash initialization v2 ---
11+
12+
source "$(rlocation build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash)" \
13+
|| { echo "Could not source build_bazel_rules_nodejs/third_party/github.com/bazelbuild/bazel-skylib/tests/unittest.bash" >&2; exit 1; }
14+
15+
function test_tgz_package_json() {
16+
TGZ=$(rlocation build_bazel_rules_nodejs/internal/pkg_npm/test/tgz_out/my_tar.tgz)
17+
tar -vxf "${TGZ}"
18+
19+
assert_contains "awesome-package" "./package/package.json"
20+
assert_contains "MAIN" "./package/main.js"
21+
}
22+
23+
run_suite "test_tgz_package_json"

tools/defaults.bzl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ def pkg_npm(**kwargs):
5555
"0.0.0-PLACEHOLDER": "{STABLE_BUILD_SCM_VERSION}",
5656
})
5757

58+
name = kwargs.pop("name")
59+
5860
# Call through to the rule with our defaults set
5961
_pkg_npm(
62+
name = name,
63+
tgz = "%s.tgz" % name,
6064
deps = deps,
6165
substitutions = select({
6266
"@build_bazel_rules_nodejs//internal:stamp": stamped_substitutions,

0 commit comments

Comments
 (0)