Skip to content

Commit

Permalink
Use ctx.version_file in place of a genrule with stamp=True
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Mar 23, 2018
1 parent a3104af commit 21f2274
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 32 deletions.
21 changes: 2 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,26 +206,9 @@ Bazel supports this natively, using the following approach:
For a more full-featured script, take a look at the [bazel_stamp_vars in Angular]
1) A `genrule()` with `stamp=True` can read the result.
Bazel puts the output of the `bazel_stamp_vars.sh` in the magic location `bazel-out/volatile-status.txt`.
(Note, this doesn't require that you actually have a `bazel-out` folder in your project.)
We recommend adding this target to your `tools/BAZEL.build`:
```python
genrule(
name = "stamp_data",
outs = ["stamp_data.txt"],
cmd = "cat bazel-out/volatile-status.txt > $@",
stamp = True,
visibility = ["//:__subpackages__"],
)
```
1) Now you can pass this target to the `stamp_data` attribute of `rollup_bundle` or `npm_package`:
Ideally, `rollup_bundle` and `npm_package` should honor the `--stamp` argument to `bazel build`. However this is not currently possible, see https://github.com/bazelbuild/bazel/issues/1054
```python
stamp_data = "//tools:stamp_data"
```
> WARNING: Bazel doesn't rebuild a target if only the result of the workspace_status_command has changed. That means changes to the version information may not be reflected if you re-build the package or bundle, and nothing in the package or bundle has changed.
See https://www.kchodorow.com/blog/2017/03/27/stamping-your-builds/ for more background.
Expand Down
1 change: 0 additions & 1 deletion internal/e2e/rollup/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ rollup_bundle(
entry_point = "internal/e2e/rollup/foo.js",
license_banner = ":license.txt",
node_modules = "//internal/test:node_modules",
stamp_data = ":versions.txt",
deps = ["//internal/e2e/rollup/fum:fumlib"],
)

Expand Down
4 changes: 0 additions & 4 deletions internal/e2e/rollup/versions.txt

This file was deleted.

7 changes: 3 additions & 4 deletions internal/npm_package/npm_package.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def create_package(ctx, devmode_sources, nested_packages):
args.add([p.path for p in nested_packages], join_with=",")
args.add(ctx.attr.replacements)
args.add([ctx.outputs.pack.path, ctx.outputs.publish.path])
args.add(ctx.file.stamp_data.path if ctx.file.stamp_data else '')
args.add(ctx.version_file.path if ctx.version_file else '')

inputs = ctx.files.srcs + devmode_sources + nested_packages + [ctx.file._run_npm_template]
if ctx.file.stamp_data:
inputs.append(ctx.file.stamp_data)
if ctx.version_file:
inputs.append(ctx.version_file)

ctx.action(
executable = ctx.executable._packager,
Expand All @@ -69,7 +69,6 @@ NPM_PACKAGE_ATTRS = {
"deps": attr.label_list(aspects = [sources_aspect]),
"packages": attr.label_list(allow_files = True),
"replacements": attr.string_dict(),
"stamp_data": attr.label(allow_single_file = FileType([".txt"])),
"_packager": attr.label(
default = Label("//internal/npm_package:packager"),
cfg = "host", executable = True),
Expand Down
7 changes: 3 additions & 4 deletions internal/rollup/rollup_bundle.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ def write_rollup_config(ctx, plugins=[], root_dirs=None, filename="_%s.rollup.co
"TMPL_module_mappings": str(mappings),
"TMPL_additional_plugins": ",\n".join(plugins),
"TMPL_banner_file": "\"%s\"" % ctx.file.license_banner.path if ctx.file.license_banner else "undefined",
"TMPL_stamp_data": "\"%s\"" % ctx.file.stamp_data.path if ctx.file.stamp_data else "undefined",
"TMPL_stamp_data": "\"%s\"" % ctx.version_file.path if ctx.version_file else "undefined",
})

return config
Expand All @@ -93,8 +93,8 @@ def run_rollup(ctx, sources, config, output):
inputs = sources + ctx.files.node_modules + [config]
if ctx.file.license_banner:
inputs += [ctx.file.license_banner]
if ctx.file.stamp_data:
inputs += [ctx.file.stamp_data]
if ctx.version_file:
inputs += [ctx.version_file]

ctx.action(
executable = ctx.executable._rollup,
Expand Down Expand Up @@ -185,7 +185,6 @@ ROLLUP_ATTRS = {
"deps": attr.label_list(aspects = [rollup_module_mappings_aspect]),
"node_modules": attr.label(default = Label("@//:node_modules")),
"license_banner": attr.label(allow_single_file = FileType([".txt"])),
"stamp_data": attr.label(allow_single_file = FileType([".txt"])),
"_rollup": attr.label(
executable = True,
cfg="host",
Expand Down
3 changes: 3 additions & 0 deletions tools/bazel.rc
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ test --test_output=errors
build --symlink_prefix=/

build:ci --noshow_progress

# Mock versoning command to test the --stamp behavior
build --workspace_status_command="echo BUILD_SCM_VERSION 1.2.3"

0 comments on commit 21f2274

Please sign in to comment.