Skip to content

Commit

Permalink
feat: support --stamp env vars in npm_package_bin (#3162)
Browse files Browse the repository at this point in the history
  • Loading branch information
gregmagolan authored Dec 28, 2021
1 parent 2c92f5e commit 38fee32
Show file tree
Hide file tree
Showing 37 changed files with 215 additions and 207 deletions.
2 changes: 2 additions & 0 deletions docs/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ stardoc(
deps = [
"//:bzl",
"//internal/node:bzl",
"@bazel_skylib//lib:types",
"@rules_nodejs//nodejs:bzl",
],
)
Expand All @@ -35,6 +36,7 @@ stardoc(
tags = ["fix-windows"],
deps = [
"//:bzl",
"@bazel_skylib//lib:types",
"@rules_nodejs//nodejs:bzl",
],
)
Expand Down
62 changes: 36 additions & 26 deletions docs/Built-ins.md
Original file line number Diff line number Diff line change
Expand Up @@ -952,8 +952,8 @@ Defaults to `3600`
**USAGE**

<pre>
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>,
<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>)
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>,
<a href="#pkg_npm-validate">validate</a>, <a href="#pkg_npm-vendor_external">vendor_external</a>)
</pre>

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

Defaults to `[]`

<h4 id="pkg_npm-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="pkg_npm-package_name">package_name</h4>

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

Defaults to `[]`

<h4 id="pkg_npm-stamp">stamp</h4>

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


Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`

<h4 id="pkg_npm-substitutions">substitutions</h4>

(*<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.
Expand Down Expand Up @@ -1134,7 +1139,7 @@ Defaults to `[]`
**USAGE**

<pre>
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>)
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>)
</pre>

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

Defaults to `[]`

<h4 id="pkg_web-node_context_data">node_context_data</h4>
<h4 id="pkg_web-srcs">srcs</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

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

Defaults to `[]`

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

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

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

Defaults to `[]`
Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`

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

Expand Down
17 changes: 0 additions & 17 deletions docs/Providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,23 +92,6 @@ Historical note: this was the typescript.es5_sources output.
Depset of direct and transitive JavaScript files and sourcemaps


## NodeContextInfo

**USAGE**

<pre>
NodeContextInfo(<a href="#NodeContextInfo-stamp">stamp</a>)
</pre>

Provides data about the build context, like config_setting's

**FIELDS**

<h4 id="NodeContextInfo-stamp">stamp</h4>

If stamping is enabled for this build


## NodeRuntimeDepsInfo

**USAGE**
Expand Down
31 changes: 18 additions & 13 deletions docs/Rollup.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ rollup_bundle(

<pre>
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>,
<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>,
<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>,
<a href="#rollup_bundle-supports_workers">supports_workers</a>)
</pre>

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

Defaults to `False`

<h4 id="rollup_bundle-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="rollup_bundle-output_dir">output_dir</h4>

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

Defaults to `[]`

<h4 id="rollup_bundle-stamp">stamp</h4>

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


Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`

<h4 id="rollup_bundle-supports_workers">supports_workers</h4>

(*Boolean*): Experimental! Use only with caution.
Expand Down
35 changes: 20 additions & 15 deletions docs/esbuild.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ This will create an output directory containing all the code split chunks, along

<pre>
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>,
<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-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>,
<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>)
<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>,
<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>,
<a href="#esbuild-stamp">stamp</a>, <a href="#esbuild-target">target</a>)
</pre>

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

Defaults to `False`

<h4 id="esbuild-node_context_data">node_context_data</h4>

(*<a href="https://bazel.build/docs/build-ref.html#labels">Label</a>*): Provides info about the build context, such as stamping.

By default it reads from the bazel command line, such as the `--stamp` argument.
Use this to override values for this target, such as enabling or disabling stamping.
You can use the `node_context_data` rule in `@build_bazel_rules_nodejs//internal/node:context.bzl`
to create a NodeContextInfo. The dependencies of this attribute must provide: NodeContextInfo


Defaults to `@build_bazel_rules_nodejs//internal:node_context_data`

<h4 id="esbuild-output">output</h4>

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

Defaults to `[]`

<h4 id="esbuild-stamp">stamp</h4>

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


Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`

<h4 id="esbuild-target">target</h4>

(*String*): Environment target (e.g. es2017, chrome58, firefox57, safari11,
Expand Down
17 changes: 0 additions & 17 deletions internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
# limitations under the License.

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

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

Expand All @@ -34,19 +33,3 @@ nodejs_test(
entry_point = ":check_bazel_version.js",
templated_args = [BAZEL_VERSION],
)

# Detect if the build is running under --stamp
config_setting(
name = "stamp",
values = {"stamp": "true"},
)

# Modelled after go_context_data from rules_go
# passes config values to starlark via a provider
node_context_data(
name = "node_context_data",
stamp = select({
"@build_bazel_rules_nodejs//internal:stamp": True,
"//conditions:default": False,
}),
)
1 change: 1 addition & 0 deletions internal/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ bzl_library(
deps = [
"//internal/npm_install:bzl",
"//third_party/github.com/bazelbuild/bazel-skylib:bzl",
"@bazel_skylib//lib:types",
"@rules_nodejs//nodejs:bzl",
],
)
Expand Down
20 changes: 0 additions & 20 deletions internal/node/context.bzl

This file was deleted.

3 changes: 2 additions & 1 deletion internal/node/npm_package_bin.bzl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"A generic rule to run a tool that appears in node_modules/.bin"

load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo")
load("@rules_nodejs//nodejs:providers.bzl", "DeclarationInfo", "JSModuleInfo", "STAMP_ATTR")
load("//:providers.bzl", "ExternalNpmPackageInfo", "node_modules_aspect", "run_node")
load("//internal/common:expand_variables.bzl", "expand_variables")
load("//internal/linker:link_node_modules.bzl", "module_mappings_aspect")
Expand All @@ -24,6 +24,7 @@ _ATTRS = {
cfg = "host",
mandatory = True,
),
"stamp": STAMP_ATTR,
}

def _expand_locations(ctx, s):
Expand Down
11 changes: 6 additions & 5 deletions internal/pkg_npm/pkg_npm.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ If all users of your library code use Bazel, they should just add your library
to the `deps` of one of their targets.
"""

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

_DOC = """The pkg_npm rule creates a directory containing a publishable npm artifact.
Expand Down Expand Up @@ -94,7 +94,7 @@ my_rule(
"""

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

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

stamp = ctx.attr.node_context_data[NodeContextInfo].stamp
stamp = ctx.attr.stamp[StampSettingInfo].value

all_files = deps_files + ctx.files.srcs

Expand Down
10 changes: 1 addition & 9 deletions internal/pkg_npm/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
load("@build_bazel_rules_nodejs//:index.bzl", "pkg_npm")
load("//internal/node:context.bzl", "node_context_data")
load("//packages/jasmine:index.bzl", "jasmine_node_test")
load("//packages/rollup:index.bzl", "rollup_bundle")
load("//packages/concatjs:index.bzl", "ts_library")
Expand Down Expand Up @@ -31,13 +30,6 @@ pkg_npm(
srcs = ["dependent_file"],
)

# Force stamping behavior even in builds without --stamp config
# by mocking out the config data
node_context_data(
name = "force_stamp",
stamp = True,
)

pkg_npm(
name = "test_pkg",
package_name = "test-pkg",
Expand All @@ -47,7 +39,7 @@ pkg_npm(
"@internal_npm_package_test_vendored_external//:vendored_external_file",
],
nested_packages = [":dependent_pkg"],
node_context_data = ":force_stamp",
stamp = "@rules_nodejs//nodejs/stamp:always",
substitutions = {
"0.0.0-PLACEHOLDER": "{BUILD_SCM_VERSION}",
"replace_me": "replaced",
Expand Down
Loading

0 comments on commit 38fee32

Please sign in to comment.