Skip to content

Commit

Permalink
rules_r: stamp attribute for r_binary and r_test
Browse files Browse the repository at this point in the history
This stamp attribute will determine whether we depend on the stable
status file in the runfiles of the binary/test. The volatile status file
is always available. This will help people get better cache hits across
builds when their workspace status command is outputting STABLE_ vars
but an individual test does not really need these vars.

NOTE: This is a breaking change for people who were depending on the
stable status file in their r_test or r_markdown target. This file will
not be available by default because the attribute value is False by
default. Volatile status files will continue to be available.

See #37 for more context.

We expect the usage of this attribute to be limited, and we expect
cache hits in stamping to be governed mostly through the management of
the content of stable status file.

The logic extends to r_markdown.

Co-authored-by: forestfang-stripe <[email protected]>
  • Loading branch information
Siddhartha Bagaria and forestfang-stripe committed Feb 20, 2020
1 parent aceb48c commit 8a4ab37
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 3 deletions.
9 changes: 8 additions & 1 deletion R/internal/binary.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,9 @@ def _r_binary_impl(ctx):
)

layered_lib_files = _layer_library_deps(ctx, library_deps)
stamp_files = [ctx.info_file, ctx.version_file]
stamp_files = [ctx.version_file]
if ctx.attr.stamp:
stamp_files.append(ctx.info_file)
runfiles = ctx.runfiles(
files = library_deps.lib_dirs + stamp_files,
transitive_files = depset(transitive = [srcs, exe, tools]),
Expand Down Expand Up @@ -159,6 +161,11 @@ _R_BINARY_ATTRS = {
"script_args": attr.string_list(
doc = "A list of arguments to pass to the src script",
),
"stamp": attr.bool(
doc = ("Include the stable status file in the runfiles of the binary. " +
"The volatile status file is always included."),
default = False,
),
"_binary_sh_tpl": attr.label(
allow_single_file = True,
default = "@com_grail_rules_r//R/scripts:binary.sh.tpl",
Expand Down
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -608,14 +608,22 @@ the runfiles of the root executable.
<p>A list of arguments to pass to the src script.</p>
</td>
</tr>
<tr>
<td><code>stamp</code></td>
<td>
<p><code>Bool; default False</code></p>
<p>Include the stable status file in the runfiles of the binary.
The volatile status file is always included.</p>
</td>
</tr>
</tbody>
</table>

<a name="r_test"></a>
## r_test

```python
r_test(name, src, deps, data, env_vars, tools, rscript_args, script_args)
r_test(name, src, deps, data, env_vars, tools, rscript_args, script_args, stamp)
```

This is identical to [r_binary](#r_binary) but is run as a test.
Expand All @@ -624,7 +632,7 @@ This is identical to [r_binary](#r_binary) but is run as a test.
## r_markdown

```python
r_markdown(name, src, deps, data, env_vars, tools, rscript_args, script_args,
r_markdown(name, src, deps, data, env_vars, tools, rscript_args, script_args, stamp,
render_function="rmarkdown::render", input_argument="input", output_dir_argument="output_dir",
render_args)
```
Expand Down
1 change: 1 addition & 0 deletions tests/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --workspace_status_command=stamping/workspace_status.sh
26 changes: 26 additions & 0 deletions tests/stamping/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Copyright 2020 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

load("@com_grail_rules_r//R:defs.bzl", "r_test")

r_test(
name = "stamp_test",
src = "stamp_test.sh",
stamp = True,
)

r_test(
name = "nostamp_test",
src = "nostamp_test.sh",
)
29 changes: 29 additions & 0 deletions tests/stamping/nostamp_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash
# Copyright 2020 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

set -euo pipefail

fail() {
echo "$@"
exit 1
}

if ! grep "^VAR" "volatile-status.txt"; then
fail "volatile status file expected"
fi

if [[ -e stable-info.txt ]]; then
fail "stable status file not expected"
fi
30 changes: 30 additions & 0 deletions tests/stamping/stamp_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash
# Copyright 2020 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


set -euo pipefail

fail() {
echo "$@"
exit 1
}

if ! grep "^VAR" "volatile-status.txt"; then
fail "volatile status file expected"
fi

if ! grep "^STABLE_VAR" "stable-status.txt"; then
fail "stable status file expected and not empty"
fi
17 changes: 17 additions & 0 deletions tests/stamping/workspace_status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/bash
# Copyright 2020 The Bazel Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

echo "VAR foo"
echo "STABLE_VAR bar"

0 comments on commit 8a4ab37

Please sign in to comment.