Skip to content

Commit 38fee32

Browse files
authored
feat: support --stamp env vars in npm_package_bin (#3162)
1 parent 2c92f5e commit 38fee32

37 files changed

+215
-207
lines changed

docs/BUILD.bazel

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ stardoc(
2424
deps = [
2525
"//:bzl",
2626
"//internal/node:bzl",
27+
"@bazel_skylib//lib:types",
2728
"@rules_nodejs//nodejs:bzl",
2829
],
2930
)
@@ -35,6 +36,7 @@ stardoc(
3536
tags = ["fix-windows"],
3637
deps = [
3738
"//:bzl",
39+
"@bazel_skylib//lib:types",
3840
"@rules_nodejs//nodejs:bzl",
3941
],
4042
)

docs/Built-ins.md

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -952,8 +952,8 @@ Defaults to `3600`
952952
**USAGE**
953953

954954
<pre>
955-
pkg_npm(<a href="#pkg_npm-name">name</a>, <a href="#pkg_npm-deps">deps</a>, <a href="#pkg_npm-nested_packages">nested_packages</a>, <a href="#pkg_npm-node_context_data">node_context_data</a>, <a href="#pkg_npm-package_name">package_name</a>, <a href="#pkg_npm-package_path">package_path</a>, <a href="#pkg_npm-srcs">srcs</a>,
956-
<a href="#pkg_npm-substitutions">substitutions</a>, <a href="#pkg_npm-tgz">tgz</a>, <a href="#pkg_npm-validate">validate</a>, <a href="#pkg_npm-vendor_external">vendor_external</a>)
955+
pkg_npm(<a href="#pkg_npm-name">name</a>, <a href="#pkg_npm-deps">deps</a>, <a href="#pkg_npm-nested_packages">nested_packages</a>, <a href="#pkg_npm-package_name">package_name</a>, <a href="#pkg_npm-package_path">package_path</a>, <a href="#pkg_npm-srcs">srcs</a>, <a href="#pkg_npm-stamp">stamp</a>, <a href="#pkg_npm-substitutions">substitutions</a>, <a href="#pkg_npm-tgz">tgz</a>,
956+
<a href="#pkg_npm-validate">validate</a>, <a href="#pkg_npm-vendor_external">vendor_external</a>)
957957
</pre>
958958

959959
The pkg_npm rule creates a directory containing a publishable npm artifact.
@@ -1060,18 +1060,6 @@ Defaults to `[]`
10601060

10611061
Defaults to `[]`
10621062

1063-
<h4 id="pkg_npm-node_context_data">node_context_data</h4>
1064-
1065-
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.
1066-
1067-
By default it reads from the bazel command line, such as the `--stamp` argument.
1068-
Use this to override values for this target, such as enabling or disabling stamping.
1069-
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
1070-
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo
1071-
1072-
1073-
Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`
1074-
10751063
<h4 id="pkg_npm-package_name">package_name</h4>
10761064

10771065
(*String*): The package name that the linker will link this npm package as.
@@ -1096,6 +1084,23 @@ Defaults to `""`
10961084

10971085
Defaults to `[]`
10981086

1087+
<h4 id="pkg_npm-stamp">stamp</h4>
1088+
1089+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
1090+
- `@rules_nodejs//nodejs/stamp:always`:
1091+
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
1092+
This setting should be avoided, since it potentially causes cache misses remote caching for
1093+
any downstream actions that depend on it.
1094+
- `@rules_nodejs//nodejs/stamp:never`:
1095+
Always replace build information by constant values. This gives good build result caching.
1096+
- `@rules_nodejs//nodejs/stamp:use_stamp_flag`:
1097+
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
1098+
Stamped binaries are not rebuilt unless their dependencies change.
1099+
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo
1100+
1101+
1102+
Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`
1103+
10991104
<h4 id="pkg_npm-substitutions">substitutions</h4>
11001105

11011106
(*<a href="https://bazel.build/docs/skylark/lib/dict.html">Dictionary: String -> String</a>*): Key-value pairs which are replaced in all the files while building the package.
@@ -1134,7 +1139,7 @@ Defaults to `[]`
11341139
**USAGE**
11351140

11361141
<pre>
1137-
pkg_web(<a href="#pkg_web-name">name</a>, <a href="#pkg_web-additional_root_paths">additional_root_paths</a>, <a href="#pkg_web-node_context_data">node_context_data</a>, <a href="#pkg_web-srcs">srcs</a>, <a href="#pkg_web-substitutions">substitutions</a>)
1142+
pkg_web(<a href="#pkg_web-name">name</a>, <a href="#pkg_web-additional_root_paths">additional_root_paths</a>, <a href="#pkg_web-srcs">srcs</a>, <a href="#pkg_web-stamp">stamp</a>, <a href="#pkg_web-substitutions">substitutions</a>)
11381143
</pre>
11391144

11401145
Assembles a web application from source files.
@@ -1153,23 +1158,28 @@ Assembles a web application from source files.
11531158

11541159
Defaults to `[]`
11551160

1156-
<h4 id="pkg_web-node_context_data">node_context_data</h4>
1161+
<h4 id="pkg_web-srcs">srcs</h4>
11571162

1158-
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.
1159-
1160-
By default it reads from the bazel command line, such as the `--stamp` argument.
1161-
Use this to override values for this target, such as enabling or disabling stamping.
1162-
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
1163-
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo
1163+
(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files which should be copied into the package
11641164

1165+
Defaults to `[]`
11651166

1166-
Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`
1167+
<h4 id="pkg_web-stamp">stamp</h4>
11671168

1168-
<h4 id="pkg_web-srcs">srcs</h4>
1169+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
1170+
- `@rules_nodejs//nodejs/stamp:always`:
1171+
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
1172+
This setting should be avoided, since it potentially causes cache misses remote caching for
1173+
any downstream actions that depend on it.
1174+
- `@rules_nodejs//nodejs/stamp:never`:
1175+
Always replace build information by constant values. This gives good build result caching.
1176+
- `@rules_nodejs//nodejs/stamp:use_stamp_flag`:
1177+
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
1178+
Stamped binaries are not rebuilt unless their dependencies change.
1179+
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo
11691180

1170-
(*<a href="https://bazel.build/docs/build-ref.html#labels">List of labels</a>*): Files which should be copied into the package
11711181

1172-
Defaults to `[]`
1182+
Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`
11731183

11741184
<h4 id="pkg_web-substitutions">substitutions</h4>
11751185

docs/Providers.md

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -92,23 +92,6 @@ Historical note: this was the typescript.es5_sources output.
9292
Depset of direct and transitive JavaScript files and sourcemaps
9393

9494

95-
## NodeContextInfo
96-
97-
**USAGE**
98-
99-
<pre>
100-
NodeContextInfo(<a href="#NodeContextInfo-stamp">stamp</a>)
101-
</pre>
102-
103-
Provides data about the build context, like config_setting's
104-
105-
**FIELDS**
106-
107-
<h4 id="NodeContextInfo-stamp">stamp</h4>
108-
109-
If stamping is enabled for this build
110-
111-
11295
## NodeRuntimeDepsInfo
11396

11497
**USAGE**

docs/Rollup.md

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ rollup_bundle(
198198

199199
<pre>
200200
rollup_bundle(<a href="#rollup_bundle-name">name</a>, <a href="#rollup_bundle-args">args</a>, <a href="#rollup_bundle-config_file">config_file</a>, <a href="#rollup_bundle-deps">deps</a>, <a href="#rollup_bundle-entry_point">entry_point</a>, <a href="#rollup_bundle-entry_points">entry_points</a>, <a href="#rollup_bundle-format">format</a>, <a href="#rollup_bundle-link_workspace_root">link_workspace_root</a>,
201-
<a href="#rollup_bundle-node_context_data">node_context_data</a>, <a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>,
201+
<a href="#rollup_bundle-output_dir">output_dir</a>, <a href="#rollup_bundle-rollup_bin">rollup_bin</a>, <a href="#rollup_bundle-rollup_worker_bin">rollup_worker_bin</a>, <a href="#rollup_bundle-silent">silent</a>, <a href="#rollup_bundle-sourcemap">sourcemap</a>, <a href="#rollup_bundle-srcs">srcs</a>, <a href="#rollup_bundle-stamp">stamp</a>,
202202
<a href="#rollup_bundle-supports_workers">supports_workers</a>)
203203
</pre>
204204

@@ -319,18 +319,6 @@ If source files need to be required then they can be copied to the bin_dir with
319319

320320
Defaults to `False`
321321

322-
<h4 id="rollup_bundle-node_context_data">node_context_data</h4>
323-
324-
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.
325-
326-
By default it reads from the bazel command line, such as the `--stamp` argument.
327-
Use this to override values for this target, such as enabling or disabling stamping.
328-
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
329-
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo
330-
331-
332-
Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`
333-
334322
<h4 id="rollup_bundle-output_dir">output_dir</h4>
335323

336324
(*Boolean*): Whether to produce a directory output.
@@ -381,6 +369,23 @@ You must not repeat file(s) passed to entry_point/entry_points.
381369

382370
Defaults to `[]`
383371

372+
<h4 id="rollup_bundle-stamp">stamp</h4>
373+
374+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
375+
- `@rules_nodejs//nodejs/stamp:always`:
376+
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
377+
This setting should be avoided, since it potentially causes cache misses remote caching for
378+
any downstream actions that depend on it.
379+
- `@rules_nodejs//nodejs/stamp:never`:
380+
Always replace build information by constant values. This gives good build result caching.
381+
- `@rules_nodejs//nodejs/stamp:use_stamp_flag`:
382+
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
383+
Stamped binaries are not rebuilt unless their dependencies change.
384+
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo
385+
386+
387+
Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`
388+
384389
<h4 id="rollup_bundle-supports_workers">supports_workers</h4>
385390

386391
(*Boolean*): Experimental! Use only with caution.

docs/esbuild.md

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ This will create an output directory containing all the code split chunks, along
103103

104104
<pre>
105105
esbuild(<a href="#esbuild-name">name</a>, <a href="#esbuild-args">args</a>, <a href="#esbuild-args_json">args_json</a>, <a href="#esbuild-config">config</a>, <a href="#esbuild-define">define</a>, <a href="#esbuild-define_settings">define_settings</a>, <a href="#esbuild-deps">deps</a>, <a href="#esbuild-entry_point">entry_point</a>, <a href="#esbuild-entry_points">entry_points</a>,
106-
<a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>, <a href="#esbuild-launcher">launcher</a>, <a href="#esbuild-link_workspace_root">link_workspace_root</a>, <a href="#esbuild-max_threads">max_threads</a>, <a href="#esbuild-metafile">metafile</a>, <a href="#esbuild-minify">minify</a>,
107-
<a href="#esbuild-node_context_data">node_context_data</a>, <a href="#esbuild-output">output</a>, <a href="#esbuild-output_css">output_css</a>, <a href="#esbuild-output_dir">output_dir</a>, <a href="#esbuild-output_map">output_map</a>, <a href="#esbuild-platform">platform</a>, <a href="#esbuild-sourcemap">sourcemap</a>,
108-
<a href="#esbuild-sources_content">sources_content</a>, <a href="#esbuild-splitting">splitting</a>, <a href="#esbuild-srcs">srcs</a>, <a href="#esbuild-target">target</a>)
106+
<a href="#esbuild-external">external</a>, <a href="#esbuild-format">format</a>, <a href="#esbuild-launcher">launcher</a>, <a href="#esbuild-link_workspace_root">link_workspace_root</a>, <a href="#esbuild-max_threads">max_threads</a>, <a href="#esbuild-metafile">metafile</a>, <a href="#esbuild-minify">minify</a>, <a href="#esbuild-output">output</a>,
107+
<a href="#esbuild-output_css">output_css</a>, <a href="#esbuild-output_dir">output_dir</a>, <a href="#esbuild-output_map">output_map</a>, <a href="#esbuild-platform">platform</a>, <a href="#esbuild-sourcemap">sourcemap</a>, <a href="#esbuild-sources_content">sources_content</a>, <a href="#esbuild-splitting">splitting</a>, <a href="#esbuild-srcs">srcs</a>,
108+
<a href="#esbuild-stamp">stamp</a>, <a href="#esbuild-target">target</a>)
109109
</pre>
110110

111111
Runs the esbuild bundler under Bazel
@@ -263,18 +263,6 @@ See https://esbuild.github.io/api/#minify for more details
263263

264264
Defaults to `False`
265265

266-
<h4 id="esbuild-node_context_data">node_context_data</h4>
267-
268-
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.
269-
270-
By default it reads from the bazel command line, such as the `--stamp` argument.
271-
Use this to override values for this target, such as enabling or disabling stamping.
272-
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
273-
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo
274-
275-
276-
Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`
277-
278266
<h4 id="esbuild-output">output</h4>
279267

280268
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Name of the output file when bundling
@@ -338,6 +326,23 @@ Defaults to `False`
338326

339327
Defaults to `[]`
340328

329+
<h4 id="esbuild-stamp">stamp</h4>
330+
331+
(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Whether to encode build information into the output. Possible values:
332+
- `@rules_nodejs//nodejs/stamp:always`:
333+
Always stamp the build information into the output, even in [--nostamp][stamp] builds.
334+
This setting should be avoided, since it potentially causes cache misses remote caching for
335+
any downstream actions that depend on it.
336+
- `@rules_nodejs//nodejs/stamp:never`:
337+
Always replace build information by constant values. This gives good build result caching.
338+
- `@rules_nodejs//nodejs/stamp:use_stamp_flag`:
339+
Embedding of build information is controlled by the [--[no]stamp][stamp] flag.
340+
Stamped binaries are not rebuilt unless their dependencies change.
341+
[stamp]: https://docs.bazel.build/versions/main/user-manual.html#flag--stamp The dependencies of this attribute must provide: StampSettingInfo
342+
343+
344+
Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`
345+
341346
<h4 id="esbuild-target">target</h4>
342347

343348
(*String*): Environment target (e.g. es2017, chrome58, firefox57, safari11,

internal/BUILD.bazel

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
load("@build_bazel_rules_nodejs//:index.bzl", "BAZEL_VERSION", "nodejs_test")
16-
load("//internal/node:context.bzl", "node_context_data")
1716

1817
package(default_visibility = ["//visibility:public"])
1918

@@ -34,19 +33,3 @@ nodejs_test(
3433
entry_point = ":check_bazel_version.js",
3534
templated_args = [BAZEL_VERSION],
3635
)
37-
38-
# Detect if the build is running under --stamp
39-
config_setting(
40-
name = "stamp",
41-
values = {"stamp": "true"},
42-
)
43-
44-
# Modelled after go_context_data from rules_go
45-
# passes config values to starlark via a provider
46-
node_context_data(
47-
name = "node_context_data",
48-
stamp = select({
49-
"@build_bazel_rules_nodejs//internal:stamp": True,
50-
"//conditions:default": False,
51-
}),
52-
)

internal/node/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ bzl_library(
3030
deps = [
3131
"//internal/npm_install:bzl",
3232
"//third_party/github.com/bazelbuild/bazel-skylib:bzl",
33+
"@bazel_skylib//lib:types",
3334
"@rules_nodejs//nodejs:bzl",
3435
],
3536
)

internal/node/context.bzl

Lines changed: 0 additions & 20 deletions
This file was deleted.

internal/node/npm_package_bin.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"A generic rule to run a tool that appears in node_modules/.bin"
22

3-
load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo")
3+
load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo", "STAMP_ATTR")
44
load("//:providers.bzl", "ExternalNpmPackageInfo", "node_modules_aspect", "run_node")
55
load("//internal/common:expand_variables.bzl", "expand_variables")
66
load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect")
@@ -24,6 +24,7 @@ _ATTRS = {
2424
cfg = "host",
2525
mandatory = True,
2626
),
27+
"stamp": STAMP_ATTR,
2728
}
2829

2930
def _expand_locations(ctx, s):

internal/pkg_npm/pkg_npm.bzl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ If all users of your library code use Bazel, they should just add your library
66
to the `deps` of one of their targets.
77
"""
88

9-
load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo", "LinkablePackageInfo")
10-
load("//:providers.bzl", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo")
9+
load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo", "LinkablePackageInfo", "STAMP_ATTR", "StampSettingInfo")
10+
load("//:providers.bzl", "JSEcmaScriptModuleInfo", "JSNamedModuleInfo")
1111

1212
_DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact.
1313
@@ -94,7 +94,7 @@ my_rule(
9494
"""
9595

9696
# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
97-
PKG_NPM_ATTRS = dict(NODE_CONTEXT_ATTRS, **{
97+
PKG_NPM_ATTRS = {
9898
"deps": attr.label_list(
9999
doc = """Other targets which produce files that should be included in the package, such as `rollup_bundle`""",
100100
allow_files = True,
@@ -119,6 +119,7 @@ If package_path is not set the this will be the root node_modules of the workspa
119119
doc = """Files inside this directory which are simply copied into the package.""",
120120
allow_files = True,
121121
),
122+
"stamp": STAMP_ATTR,
122123
"substitutions": attr.string_dict(
123124
doc = """Key-value pairs which are replaced in all the files while building the package.
124125
@@ -152,7 +153,7 @@ See the section on stamping in the [README](stamping)
152153
cfg = "exec",
153154
executable = True,
154155
),
155-
})
156+
}
156157

157158
# Used in angular/angular /packages/bazel/src/ng_package/ng_package.bzl
158159
PKG_NPM_OUTPUTS = {
@@ -208,7 +209,7 @@ def create_package(ctx, deps_files, nested_packages):
208209
The tree artifact which is the publishable directory.
209210
"""
210211

211-
stamp = ctx.attr.node_context_data[NodeContextInfo].stamp
212+
stamp = ctx.attr.stamp[StampSettingInfo].value
212213

213214
all_files = deps_files + ctx.files.srcs
214215

0 commit comments

Comments
 (0)