diff --git a/docs/BUILD.bazel b/docs/BUILD.bazel index 50c578bc58..765c9778ff 100644 --- a/docs/BUILD.bazel +++ b/docs/BUILD.bazel @@ -24,6 +24,7 @@ stardoc( deps = [ "//:bzl", "//internal/node:bzl", + "@bazel_skylib//lib:types", "@rules_nodejs//nodejs:bzl", ], ) @@ -35,6 +36,7 @@ stardoc( tags = ["fix-windows"], deps = [ "//:bzl", + "@bazel_skylib//lib:types", "@rules_nodejs//nodejs:bzl", ], ) diff --git a/docs/Built-ins.md b/docs/Built-ins.md index f9e027cdee..82c2254c6c 100755 --- a/docs/Built-ins.md +++ b/docs/Built-ins.md @@ -952,8 +952,8 @@ Defaults to `3600` **USAGE**
-pkg_npm(name, deps, nested_packages, node_context_data, package_name, package_path, srcs,
-        substitutions, tgz, validate, vendor_external)
+pkg_npm(name, deps, nested_packages, package_name, package_path, srcs, stamp, substitutions, tgz,
+        validate, vendor_external)
 
The pkg_npm rule creates a directory containing a publishable npm artifact. @@ -1060,18 +1060,6 @@ Defaults to `[]` Defaults to `[]` -

node_context_data

- -(*Label*): 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` -

package_name

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

stamp

+ +(*Label*): 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` +

substitutions

(*Dictionary: String -> String*): Key-value pairs which are replaced in all the files while building the package. @@ -1134,7 +1139,7 @@ Defaults to `[]` **USAGE**
-pkg_web(name, additional_root_paths, node_context_data, srcs, substitutions)
+pkg_web(name, additional_root_paths, srcs, stamp, substitutions)
 
Assembles a web application from source files. @@ -1153,23 +1158,28 @@ Assembles a web application from source files. Defaults to `[]` -

node_context_data

+

srcs

-(*Label*): 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 +(*List of labels*): Files which should be copied into the package +Defaults to `[]` -Defaults to `@build_bazel_rules_nodejs//internal:node_context_data` +

stamp

-

srcs

+(*Label*): 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 -(*List of labels*): Files which should be copied into the package -Defaults to `[]` +Defaults to `@rules_nodejs//nodejs/stamp:use_stamp_flag`

substitutions

diff --git a/docs/Providers.md b/docs/Providers.md index 25915c7c91..2cb301bac3 100755 --- a/docs/Providers.md +++ b/docs/Providers.md @@ -92,23 +92,6 @@ Historical note: this was the typescript.es5_sources output. Depset of direct and transitive JavaScript files and sourcemaps -## NodeContextInfo - -**USAGE** - -
-NodeContextInfo(stamp)
-
- -Provides data about the build context, like config_setting's - -**FIELDS** - -

stamp

- - If stamping is enabled for this build - - ## NodeRuntimeDepsInfo **USAGE** diff --git a/docs/Rollup.md b/docs/Rollup.md index 8c92aafa67..6279a1800c 100755 --- a/docs/Rollup.md +++ b/docs/Rollup.md @@ -198,7 +198,7 @@ rollup_bundle(
 rollup_bundle(name, args, config_file, deps, entry_point, entry_points, format, link_workspace_root,
-              node_context_data, output_dir, rollup_bin, rollup_worker_bin, silent, sourcemap, srcs,
+              output_dir, rollup_bin, rollup_worker_bin, silent, sourcemap, srcs, stamp,
               supports_workers)
 
@@ -319,18 +319,6 @@ If source files need to be required then they can be copied to the bin_dir with Defaults to `False` -

node_context_data

- -(*Label*): 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` -

output_dir

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

stamp

+ +(*Label*): 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` +

supports_workers

(*Boolean*): Experimental! Use only with caution. diff --git a/docs/esbuild.md b/docs/esbuild.md index 0603612849..03305e5727 100755 --- a/docs/esbuild.md +++ b/docs/esbuild.md @@ -103,9 +103,9 @@ This will create an output directory containing all the code split chunks, along
 esbuild(name, args, args_json, config, define, define_settings, deps, entry_point, entry_points,
-        external, format, launcher, link_workspace_root, max_threads, metafile, minify,
-        node_context_data, output, output_css, output_dir, output_map, platform, sourcemap,
-        sources_content, splitting, srcs, target)
+        external, format, launcher, link_workspace_root, max_threads, metafile, minify, output,
+        output_css, output_dir, output_map, platform, sourcemap, sources_content, splitting, srcs,
+        stamp, target)
 
Runs the esbuild bundler under Bazel @@ -263,18 +263,6 @@ See https://esbuild.github.io/api/#minify for more details Defaults to `False` -

node_context_data

- -(*Label*): 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` -

output

(*Label*): Name of the output file when bundling @@ -338,6 +326,23 @@ Defaults to `False` Defaults to `[]` +

stamp

+ +(*Label*): 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` +

target

(*String*): Environment target (e.g. es2017, chrome58, firefox57, safari11, diff --git a/internal/BUILD.bazel b/internal/BUILD.bazel index 3933063d21..16bd182e11 100644 --- a/internal/BUILD.bazel +++ b/internal/BUILD.bazel @@ -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"]) @@ -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, - }), -) diff --git a/internal/node/BUILD.bazel b/internal/node/BUILD.bazel index d2a8bfc2e4..308fa2148a 100644 --- a/internal/node/BUILD.bazel +++ b/internal/node/BUILD.bazel @@ -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", ], ) diff --git a/internal/node/context.bzl b/internal/node/context.bzl deleted file mode 100644 index fba03a19d8..0000000000 --- a/internal/node/context.bzl +++ /dev/null @@ -1,20 +0,0 @@ -"node_context_data rule" - -load("@build_bazel_rules_nodejs//:providers.bzl", "NodeContextInfo") - -_DOC = """node_context_data gathers information about the build configuration. -It is a common dependency of all targets that are sensitive to configuration. -(currently pkg_npm, pkg_web, and rollup_bundle)""" - -def _impl(ctx): - return [NodeContextInfo(stamp = ctx.attr.stamp)] - -# Modelled after go_context_data in rules_go -# Works around github.com/bazelbuild/bazel/issues/1054 -node_context_data = rule( - implementation = _impl, - attrs = { - "stamp": attr.bool(mandatory = True), - }, - doc = _DOC, -) diff --git a/internal/node/npm_package_bin.bzl b/internal/node/npm_package_bin.bzl index 576b1b6202..580ae69d12 100644 --- a/internal/node/npm_package_bin.bzl +++ b/internal/node/npm_package_bin.bzl @@ -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") @@ -24,6 +24,7 @@ _ATTRS = { cfg = "host", mandatory = True, ), + "stamp": STAMP_ATTR, } def _expand_locations(ctx, s): diff --git a/internal/pkg_npm/pkg_npm.bzl b/internal/pkg_npm/pkg_npm.bzl index c1faaff436..147ce92b9a 100644 --- a/internal/pkg_npm/pkg_npm.bzl +++ b/internal/pkg_npm/pkg_npm.bzl @@ -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. @@ -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, @@ -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. @@ -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 = { @@ -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 diff --git a/internal/pkg_npm/test/BUILD.bazel b/internal/pkg_npm/test/BUILD.bazel index 23f7f931c0..bc0fde5351 100644 --- a/internal/pkg_npm/test/BUILD.bazel +++ b/internal/pkg_npm/test/BUILD.bazel @@ -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") @@ -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", @@ -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", diff --git a/internal/pkg_web/pkg_web.bzl b/internal/pkg_web/pkg_web.bzl index 539e3e983d..90db407e37 100644 --- a/internal/pkg_web/pkg_web.bzl +++ b/internal/pkg_web/pkg_web.bzl @@ -15,11 +15,11 @@ """Contains the pkg_web rule. """ -load("//:providers.bzl", "NODE_CONTEXT_ATTRS", "NodeContextInfo") +load("@rules_nodejs//nodejs:providers.bzl", "STAMP_ATTR", "StampSettingInfo") _DOC = """Assembles a web application from source files.""" -_ATTRS = dict(NODE_CONTEXT_ATTRS, **{ +_ATTRS = { "additional_root_paths": attr.string_list( doc = """Path prefixes to strip off all srcs relative to the root of the repo, in addition to the current package. Longest wins.""", ), @@ -27,6 +27,7 @@ _ATTRS = dict(NODE_CONTEXT_ATTRS, **{ allow_files = True, doc = """Files which should be copied into the package""", ), + "stamp": STAMP_ATTR, "substitutions": attr.string_dict( doc = """Key-value pairs which are replaced in all the files while building the package. @@ -39,7 +40,7 @@ See the section on stamping in the README.""", executable = True, cfg = "host", ), -}) +} # Hints for Bazel spawn strategy _execution_requirements = { @@ -62,7 +63,7 @@ def _move_files(ctx, root_paths): args = ctx.actions.args() inputs = ctx.files.srcs[:] args.add(www_dir.path) - if ctx.attr.node_context_data[NodeContextInfo].stamp: + if ctx.attr.stamp[StampSettingInfo].value: args.add(ctx.version_file.path) inputs.append(ctx.version_file) args.add(ctx.info_file.path) diff --git a/internal/pkg_web/test/BUILD.bazel b/internal/pkg_web/test/BUILD.bazel index e1b8abea98..0b4c589e59 100644 --- a/internal/pkg_web/test/BUILD.bazel +++ b/internal/pkg_web/test/BUILD.bazel @@ -1,6 +1,5 @@ load("@build_bazel_rules_nodejs//:index.bzl", "pkg_web") load("@npm//@babel/cli:index.bzl", "babel") -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/terser:index.bzl", "terser_minified") @@ -63,11 +62,6 @@ terser_minified( debug = True, ) -node_context_data( - name = "force_stamp", - stamp = True, -) - pkg_web( name = "pkg", srcs = [ @@ -79,7 +73,7 @@ pkg_web( ":bundle.min_debug", ":bundle.min_debug.es2015", ], - node_context_data = ":force_stamp", + stamp = "@rules_nodejs//nodejs/stamp:always", substitutions = { "${version}": "{BUILD_SCM_VERSION}", }, diff --git a/internal/providers/node_context.bzl b/internal/providers/node_context.bzl deleted file mode 100644 index 15cda3f5d5..0000000000 --- a/internal/providers/node_context.bzl +++ /dev/null @@ -1,25 +0,0 @@ -"""Provide rules access to the config of the current build - -Modelled after _GoContextData in rules_go/go/private/context.bzl -""" - -NodeContextInfo = provider( - doc = "Provides data about the build context, like config_setting's", - fields = { - "stamp": "If stamping is enabled for this build", - }, -) - -NODE_CONTEXT_ATTRS = { - "node_context_data": attr.label( - default = "@build_bazel_rules_nodejs//internal:node_context_data", - providers = [NodeContextInfo], - doc = """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. -""", - ), -} diff --git a/internal/providers/node_runtime_deps_info.bzl b/internal/providers/node_runtime_deps_info.bzl index 151978e9a0..0436e3d91e 100644 --- a/internal/providers/node_runtime_deps_info.bzl +++ b/internal/providers/node_runtime_deps_info.bzl @@ -14,6 +14,8 @@ """Custom provider that mimics the Runfiles, but doesn't incur the expense of creating the runfiles symlink tree""" +load("@bazel_skylib//lib:types.bzl", "types") +load("@rules_nodejs//nodejs:providers.bzl", "StampSettingInfo") load("//internal/common:expand_into_runfiles.bzl", "expand_location_into_runfiles") load("//internal/linker:link_node_modules.bzl", "add_arg", "write_node_modules_manifest") load("//internal/providers:external_npm_package_info.bzl", "ExternalNpmPackageInfo") @@ -141,10 +143,19 @@ def run_node(ctx, inputs, arguments, executable, chdir = None, **kwargs): bazel_node_module_roots = bazel_node_module_roots + "%s:%s" % (path, root) env["BAZEL_NODE_MODULES_ROOTS"] = bazel_node_module_roots + stamp = ctx.attr.stamp[StampSettingInfo].value if hasattr(ctx.attr, "stamp") else False + if stamp: + env["BAZEL_VERSION_FILE"] = ctx.version_file.path + env["BAZEL_INFO_FILE"] = ctx.info_file.path + if types.is_list(inputs): + inputs.extend([ctx.version_file, ctx.info_file]) + else: + inputs = depset([ctx.version_file, ctx.info_file], transitive = [inputs]) + # ctx.actions.run accepts both lists and a depset for inputs. Coerce the original inputs to a # depset if they're a list, so that extra inputs can be combined in a performant manner. inputs_depset = depset(transitive = [ - depset(direct = inputs) if type(inputs) == "list" else inputs, + depset(direct = inputs) if types.is_list(inputs) else inputs, extra_inputs, depset(direct = [modules_manifest]), ]) diff --git a/nodejs/BUILD.bazel b/nodejs/BUILD.bazel index 24d47d6488..3180755215 100644 --- a/nodejs/BUILD.bazel +++ b/nodejs/BUILD.bazel @@ -6,6 +6,7 @@ filegroup( name = "package_contents", srcs = glob(["*"]) + [ "//nodejs/private:package_contents", + "//nodejs/stamp:package_contents", ], visibility = ["//:__pkg__"], ) diff --git a/nodejs/private/providers/stamp_setting_info.bzl b/nodejs/private/providers/stamp_setting_info.bzl new file mode 100644 index 0000000000..aca880e309 --- /dev/null +++ b/nodejs/private/providers/stamp_setting_info.bzl @@ -0,0 +1,24 @@ +"Pass information about stamp between rules" + +#Modelled after _GoContextData in rules_go/go/private/context.bzl +StampSettingInfo = provider( + fields = { + "value": "Whether stamping is enabled", + }, +) + +STAMP_ATTR = attr.label( + default = "@rules_nodejs//nodejs/stamp:use_stamp_flag", + providers = [StampSettingInfo], + doc = """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""", +) diff --git a/nodejs/private/stamp.bzl b/nodejs/private/stamp.bzl new file mode 100644 index 0000000000..9ec539bb9d --- /dev/null +++ b/nodejs/private/stamp.bzl @@ -0,0 +1,18 @@ +"Helper for determining when to stamp build outputs" + +load("//nodejs/private/providers:stamp_setting_info.bzl", "StampSettingInfo") + +def _impl(ctx): + return [StampSettingInfo(value = ctx.attr.stamp)] + +# Modelled after go_context_data in rules_go +# Works around github.com/bazelbuild/bazel/issues/1054 +stamp_setting = rule( + implementation = _impl, + attrs = { + "stamp": attr.bool(mandatory = True), + }, + doc = """Determines whether build outputs should be stamped with version control info. + + Stamping causes outputs to be non-deterministic, resulting in cache misses.""", +) diff --git a/nodejs/providers.bzl b/nodejs/providers.bzl index 84f9473c92..775d64d5f8 100644 --- a/nodejs/providers.bzl +++ b/nodejs/providers.bzl @@ -24,6 +24,11 @@ load( "//nodejs/private/providers:user_build_settings.bzl", _UserBuildSettingInfo = "UserBuildSettingInfo", ) +load( + "//nodejs/private/providers:stamp_setting_info.bzl", + _STAMP_ATTR = "STAMP_ATTR", + _StampSettingInfo = "StampSettingInfo", +) DeclarationInfo = _DeclarationInfo declaration_info = _declaration_info @@ -32,3 +37,5 @@ js_module_info = _js_module_info LinkablePackageInfo = _LinkablePackageInfo DirectoryFilePathInfo = _DirectoryFilePathInfo UserBuildSettingInfo = _UserBuildSettingInfo +StampSettingInfo = _StampSettingInfo +STAMP_ATTR = _STAMP_ATTR diff --git a/nodejs/stamp/BUILD.bazel b/nodejs/stamp/BUILD.bazel new file mode 100644 index 0000000000..c1a7cf021a --- /dev/null +++ b/nodejs/stamp/BUILD.bazel @@ -0,0 +1,36 @@ +load("//nodejs/private:stamp.bzl", "stamp_setting") + +# Detect if the build is running under --stamp +config_setting( + name = "stamp", + values = {"stamp": "true"}, + visibility = ["//visibility:public"], +) + +# Enable stamping based on the --stamp flag +stamp_setting( + name = "use_stamp_flag", + stamp = select({ + ":stamp": True, + "//conditions:default": False, + }), + visibility = ["//visibility:public"], +) + +stamp_setting( + name = "always", + stamp = True, + visibility = ["//visibility:public"], +) + +stamp_setting( + name = "never", + stamp = False, + visibility = ["//visibility:public"], +) + +filegroup( + name = "package_contents", + srcs = glob(["*"]), + visibility = ["//nodejs:__pkg__"], +) diff --git a/packages/concatjs/BUILD.bazel b/packages/concatjs/BUILD.bazel index d3f6846c48..490a9f43b5 100644 --- a/packages/concatjs/BUILD.bazel +++ b/packages/concatjs/BUILD.bazel @@ -52,6 +52,7 @@ bzl_library( "//packages/concatjs/devserver:bzl", "//packages/concatjs/internal:bzl", "//packages/concatjs/web_test:bzl", + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/node:bzl", diff --git a/packages/concatjs/internal/BUILD.bazel b/packages/concatjs/internal/BUILD.bazel index 0bbc74cfc3..d4afc4bb1f 100644 --- a/packages/concatjs/internal/BUILD.bazel +++ b/packages/concatjs/internal/BUILD.bazel @@ -33,6 +33,7 @@ bzl_library( ), visibility = ["//visibility:public"], deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@rules_nodejs//nodejs:bzl", diff --git a/packages/concatjs/web_test/BUILD.bazel b/packages/concatjs/web_test/BUILD.bazel index b3fe7a0e51..17d55593ec 100644 --- a/packages/concatjs/web_test/BUILD.bazel +++ b/packages/concatjs/web_test/BUILD.bazel @@ -41,6 +41,7 @@ bzl_library( testonly = True, srcs = glob(["*.bzl"]), deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/js_library:bzl", "@io_bazel_rules_webtesting//web", diff --git a/packages/cypress/BUILD.bazel b/packages/cypress/BUILD.bazel index d1382f1772..eebb4faa19 100644 --- a/packages/cypress/BUILD.bazel +++ b/packages/cypress/BUILD.bazel @@ -35,6 +35,7 @@ bzl_library( "//internal/common:bzl", "//internal/node:bzl", "//toolchains/cypress:bzl", + "@bazel_skylib//lib:types", "@rules_nodejs//nodejs:bzl", ], ) diff --git a/packages/esbuild/BUILD.bazel b/packages/esbuild/BUILD.bazel index f4b34386f4..14a6d8f35c 100644 --- a/packages/esbuild/BUILD.bazel +++ b/packages/esbuild/BUILD.bazel @@ -31,6 +31,7 @@ bzl_library( "@bazel_tools//tools:bzl_srcs", ], deps = [ + "@bazel_skylib//lib:types", "@bazel_skylib//rules:common_settings", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", diff --git a/packages/esbuild/esbuild.bzl b/packages/esbuild/esbuild.bzl index 1b5052c1f2..d36f96ce7d 100644 --- a/packages/esbuild/esbuild.bzl +++ b/packages/esbuild/esbuild.bzl @@ -2,10 +2,10 @@ esbuild rule """ -load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo") +load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo", "STAMP_ATTR") load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo") load("@build_bazel_rules_nodejs//:index.bzl", "nodejs_binary") -load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo", "node_modules_aspect", "run_node") +load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "node_modules_aspect", "run_node") load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "LinkerPackageMappingInfo", "module_mappings_aspect") load("@build_bazel_rules_nodejs//internal/common:expand_variables.bzl", "expand_variables") load("@build_bazel_rules_nodejs//toolchains/esbuild:toolchain.bzl", "TOOLCHAIN") @@ -172,14 +172,6 @@ def _esbuild_impl(ctx): inputs.append(configs[0]) launcher_args.add("--config_file=%s" % configs[0].path) - stamp = ctx.attr.node_context_data[NodeContextInfo].stamp - if stamp: - inputs.append(ctx.info_file) - env["BAZEL_INFO_FILE"] = ctx.info_file.path - - inputs.append(ctx.version_file) - env["BAZEL_VERSION_FILE"] = ctx.version_file.path - run_node( ctx = ctx, inputs = depset(inputs), @@ -207,7 +199,7 @@ def _esbuild_impl(ctx): ] esbuild = rule( - attrs = dict({ + attrs = { "args": attr.string_dict( default = {}, doc = """A dict of extra arguments that are included in the call to esbuild, where the key is the argument name. @@ -382,6 +374,7 @@ See https://esbuild.github.io/api/#splitting and https://esbuild.github.io/api/# default = [], doc = """Source files to be made available to esbuild""", ), + "stamp": STAMP_ATTR, "target": attr.string( default = "es2015", doc = """Environment target (e.g. es2017, chrome58, firefox57, safari11, @@ -397,7 +390,7 @@ See https://esbuild.github.io/api/#target for more details See https://github.com/bazelbuild/rules_nodejs/tree/stable/packages/esbuild/test/plugins/BUILD.bazel for examples of using esbuild_config and plugins. """, ), - }, **NODE_CONTEXT_ATTRS), + }, implementation = _esbuild_impl, toolchains = [ str(TOOLCHAIN), diff --git a/packages/esbuild/test/define/BUILD.bazel b/packages/esbuild/test/define/BUILD.bazel index 36478bd313..9b7e3e63d2 100644 --- a/packages/esbuild/test/define/BUILD.bazel +++ b/packages/esbuild/test/define/BUILD.bazel @@ -1,7 +1,6 @@ load("//packages/esbuild:index.bzl", "esbuild", "esbuild_config") load("//packages/jasmine:index.bzl", "jasmine_node_test") load("//packages/concatjs:index.bzl", "ts_library") -load("//internal/node:context.bzl", "node_context_data") load("@bazel_skylib//rules:common_settings.bzl", "bool_flag", "string_flag") ts_library( @@ -35,13 +34,6 @@ esbuild( deps = [":main"], ) -# Force stamping behavior even in builds without --stamp config -# by mocking out the config data -node_context_data( - name = "context_data", - stamp = True, -) - esbuild_config( name = "esbuild_config", config_file = "esbuild.config.mjs", @@ -63,7 +55,7 @@ esbuild( ":some_string_flag": "SOME_STRING_FLAG_VALUE", }, entry_point = "main.ts", - node_context_data = ":context_data", + stamp = "@rules_nodejs//nodejs/stamp:always", deps = [":main"], ) diff --git a/packages/jasmine/BUILD.bazel b/packages/jasmine/BUILD.bazel index ecac8af849..83d71c1452 100644 --- a/packages/jasmine/BUILD.bazel +++ b/packages/jasmine/BUILD.bazel @@ -26,6 +26,7 @@ bzl_library( name = "bzl", srcs = glob(["*.bzl"]), deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/node:bzl", diff --git a/packages/protractor/BUILD.bazel b/packages/protractor/BUILD.bazel index b73df7138e..ae317ec5e7 100644 --- a/packages/protractor/BUILD.bazel +++ b/packages/protractor/BUILD.bazel @@ -31,6 +31,7 @@ bzl_library( testonly = True, srcs = glob(["*.bzl"]), deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/node:bzl", diff --git a/packages/rollup/BUILD.bazel b/packages/rollup/BUILD.bazel index 2ca53b1366..72c3f2705a 100644 --- a/packages/rollup/BUILD.bazel +++ b/packages/rollup/BUILD.bazel @@ -32,6 +32,7 @@ bzl_library( name = "bzl", srcs = glob(["*.bzl"]), deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/linker:bzl", diff --git a/packages/rollup/rollup_bundle.bzl b/packages/rollup/rollup_bundle.bzl index 5c364f5a29..fd70a53f26 100644 --- a/packages/rollup/rollup_bundle.bzl +++ b/packages/rollup/rollup_bundle.bzl @@ -1,12 +1,12 @@ "Rules for running Rollup under Bazel" -load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo") -load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "NODE_CONTEXT_ATTRS", "NodeContextInfo", "node_modules_aspect", "run_node") +load("@rules_nodejs//nodejs:providers.bzl", "JSModuleInfo", "STAMP_ATTR", "StampSettingInfo") +load("@build_bazel_rules_nodejs//:providers.bzl", "ExternalNpmPackageInfo", "JSEcmaScriptModuleInfo", "node_modules_aspect", "run_node") load("@build_bazel_rules_nodejs//internal/linker:link_node_modules.bzl", "module_mappings_aspect") _DOC = "Runs the rollup.js CLI under Bazel." -_ROLLUP_ATTRS = dict(NODE_CONTEXT_ATTRS, **{ +_ROLLUP_ATTRS = { "args": attr.string_list( doc = """Command line arguments to pass to Rollup. Can be used to override config file settings. @@ -156,6 +156,7 @@ You must not repeat file(s) passed to entry_point/entry_points. # Don't try to constrain the filenames, could be json, svg, whatever allow_files = True, ), + "stamp": STAMP_ATTR, "supports_workers": attr.bool( doc = """Experimental! Use only with caution. @@ -164,7 +165,7 @@ When enabled, this rule invokes the "rollup_worker_bin" worker aware binary rather than "rollup_bin".""", default = False, ), -}) +} def _desugar_entry_point_names(name, entry_point, entry_points): """Users can specify entry_point (sugar) or entry_points (long form). @@ -302,7 +303,7 @@ def _rollup_bundle(ctx): # Run the rollup binary with the --silent flag args.add("--silent") - stamp = ctx.attr.node_context_data[NodeContextInfo].stamp + stamp = ctx.attr.stamp[StampSettingInfo].value config = ctx.actions.declare_file("_%s.rollup_config.js" % ctx.label.name) ctx.actions.expand_template( @@ -317,10 +318,6 @@ def _rollup_bundle(ctx): args.add_all(["--config", config.path]) inputs.append(config) - if stamp: - inputs.append(ctx.info_file) - inputs.append(ctx.version_file) - # Prevent rollup's module resolver from hopping outside Bazel's sandbox # When set to false, symbolic links are followed when resolving a file. # When set to true, instead of being followed, symbolic links are treated as if the file is diff --git a/packages/rollup/test/version_stamp/BUILD.bazel b/packages/rollup/test/version_stamp/BUILD.bazel index 50189c1ed5..c698449502 100644 --- a/packages/rollup/test/version_stamp/BUILD.bazel +++ b/packages/rollup/test/version_stamp/BUILD.bazel @@ -1,20 +1,12 @@ load("@build_bazel_rules_nodejs//:index.bzl", "generated_file_test") -load("//internal/node:context.bzl", "node_context_data") load("//packages/rollup:index.bzl", "rollup_bundle") -# Force stamping behavior even in builds without --stamp config -# by mocking out the config data -node_context_data( - name = "force_stamp", - stamp = True, -) - rollup_bundle( name = "version_stamp", config_file = "rollup.config.js", entry_point = "input.js", - node_context_data = ":force_stamp", sourcemap = "false", + stamp = "@rules_nodejs//nodejs/stamp:always", supports_workers = True, ) diff --git a/packages/terser/BUILD.bazel b/packages/terser/BUILD.bazel index e9706d507c..51ddbd97fc 100644 --- a/packages/terser/BUILD.bazel +++ b/packages/terser/BUILD.bazel @@ -30,6 +30,7 @@ bzl_library( name = "bzl", srcs = glob(["*.bzl"]), deps = [ + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@rules_nodejs//nodejs:bzl", diff --git a/packages/typescript/BUILD.bazel b/packages/typescript/BUILD.bazel index 5d34535a36..af278ec4e6 100644 --- a/packages/typescript/BUILD.bazel +++ b/packages/typescript/BUILD.bazel @@ -26,6 +26,7 @@ bzl_library( srcs = glob(["*.bzl"]), deps = [ "//packages/typescript/internal:bzl", + "@bazel_skylib//lib:types", "@build_bazel_rules_nodejs//:bzl", "@build_bazel_rules_nodejs//internal/common:bzl", "@build_bazel_rules_nodejs//internal/node:bzl", diff --git a/providers.bzl b/providers.bzl index 55c444b0b6..403b2d5bf2 100644 --- a/providers.bzl +++ b/providers.bzl @@ -35,20 +35,13 @@ load( _NodeRuntimeDepsInfo = "NodeRuntimeDepsInfo", _run_node = "run_node", ) -load( - "//internal/providers:node_context.bzl", - _NODE_CONTEXT_ATTRS = "NODE_CONTEXT_ATTRS", - _NodeContextInfo = "NodeContextInfo", -) ExternalNpmPackageInfo = _ExternalNpmPackageInfo js_ecma_script_module_info = _js_ecma_script_module_info js_named_module_info = _js_named_module_info JSEcmaScriptModuleInfo = _JSEcmaScriptModuleInfo JSNamedModuleInfo = _JSNamedModuleInfo -NODE_CONTEXT_ATTRS = _NODE_CONTEXT_ATTRS node_modules_aspect = _node_modules_aspect -NodeContextInfo = _NodeContextInfo NodeRuntimeDepsInfo = _NodeRuntimeDepsInfo run_node = _run_node diff --git a/toolchains/BUILD.bazel b/toolchains/BUILD.bazel index 539420cbc8..738d31f296 100644 --- a/toolchains/BUILD.bazel +++ b/toolchains/BUILD.bazel @@ -9,6 +9,7 @@ stardoc( deps = [ "//toolchains/cypress:bzl", "//toolchains/esbuild:bzl", + "@bazel_skylib//lib:types", "@rules_nodejs//nodejs:bzl", ], ) diff --git a/tools/defaults.bzl b/tools/defaults.bzl index 39f7507b7b..c6bff6624a 100644 --- a/tools/defaults.bzl +++ b/tools/defaults.bzl @@ -65,7 +65,7 @@ def pkg_npm(**kwargs): tgz = "%s.tgz" % name, deps = deps, substitutions = select({ - "@build_bazel_rules_nodejs//internal:stamp": stamped_substitutions, + "@rules_nodejs//nodejs/stamp": stamped_substitutions, "//conditions:default": substitutions, }), visibility = visibility,