Skip to content

Commit dff064e

Browse files
authored
Added env_inherit attribute to rust_test (#2809)
This brings Rust into parity with other Bazel rules (e.g. C++, Python, Java, etc) by introducing the [env_inherit](https://bazel.build/reference/be/common-definitions#test.env_inherit) attribute.
1 parent a5f7464 commit dff064e

File tree

5 files changed

+31
-19
lines changed

5 files changed

+31
-19
lines changed

docs/defs.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -498,9 +498,9 @@ When building the whole binary in Bazel, use `rust_library` instead.
498498

499499
<pre>
500500
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-deps">deps</a>, <a href="#rust_test-srcs">srcs</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-alwayslink">alwayslink</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>,
501-
<a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_test-malloc">malloc</a>, <a href="#rust_test-platform">platform</a>,
502-
<a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-stamp">stamp</a>, <a href="#rust_test-use_libtest_harness">use_libtest_harness</a>,
503-
<a href="#rust_test-version">version</a>)
501+
<a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-env_inherit">env_inherit</a>, <a href="#rust_test-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_test-malloc">malloc</a>,
502+
<a href="#rust_test-platform">platform</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-stamp">stamp</a>,
503+
<a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
504504
</pre>
505505

506506
Builds a Rust test crate.
@@ -638,6 +638,7 @@ Run the test with `bazel test //hello_lib:greeting_test`.
638638
| <a id="rust_test-crate_root"></a>crate_root | The file that will be passed to `rustc` to be used for building this crate.<br><br>If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
639639
| <a id="rust_test-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` |
640640
| <a id="rust_test-env"></a>env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
641+
| <a id="rust_test-env_inherit"></a>env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | `[]` |
641642
| <a id="rust_test-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | `-1` |
642643
| <a id="rust_test-malloc"></a>malloc | Override the default dependency on `malloc`.<br><br>By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@bazel_tools//tools/cpp:malloc"` |
643644
| <a id="rust_test-platform"></a>platform | Optional platform to transition the test to. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |

docs/flatten.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -981,9 +981,9 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
981981

982982
<pre>
983983
rust_test(<a href="#rust_test-name">name</a>, <a href="#rust_test-deps">deps</a>, <a href="#rust_test-srcs">srcs</a>, <a href="#rust_test-data">data</a>, <a href="#rust_test-aliases">aliases</a>, <a href="#rust_test-alwayslink">alwayslink</a>, <a href="#rust_test-compile_data">compile_data</a>, <a href="#rust_test-crate">crate</a>, <a href="#rust_test-crate_features">crate_features</a>,
984-
<a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_test-malloc">malloc</a>, <a href="#rust_test-platform">platform</a>,
985-
<a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-stamp">stamp</a>, <a href="#rust_test-use_libtest_harness">use_libtest_harness</a>,
986-
<a href="#rust_test-version">version</a>)
984+
<a href="#rust_test-crate_name">crate_name</a>, <a href="#rust_test-crate_root">crate_root</a>, <a href="#rust_test-edition">edition</a>, <a href="#rust_test-env">env</a>, <a href="#rust_test-env_inherit">env_inherit</a>, <a href="#rust_test-experimental_use_cc_common_link">experimental_use_cc_common_link</a>, <a href="#rust_test-malloc">malloc</a>,
985+
<a href="#rust_test-platform">platform</a>, <a href="#rust_test-proc_macro_deps">proc_macro_deps</a>, <a href="#rust_test-rustc_env">rustc_env</a>, <a href="#rust_test-rustc_env_files">rustc_env_files</a>, <a href="#rust_test-rustc_flags">rustc_flags</a>, <a href="#rust_test-stamp">stamp</a>,
986+
<a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
987987
</pre>
988988

989989
Builds a Rust test crate.
@@ -1121,6 +1121,7 @@ Run the test with `bazel test //hello_lib:greeting_test`.
11211121
| <a id="rust_test-crate_root"></a>crate_root | The file that will be passed to `rustc` to be used for building this crate.<br><br>If `crate_root` is not set, then this rule will look for a `lib.rs` file (or `main.rs` for rust_binary) or the single file in `srcs` if `srcs` contains only one file. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |
11221122
| <a id="rust_test-edition"></a>edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` |
11231123
| <a id="rust_test-env"></a>env | Specifies additional environment variables to set when the test is executed by bazel test. Values are subject to `$(rootpath)`, `$(execpath)`, location, and ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | `{}` |
1124+
| <a id="rust_test-env_inherit"></a>env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | `[]` |
11241125
| <a id="rust_test-experimental_use_cc_common_link"></a>experimental_use_cc_common_link | Whether to use cc_common.link to link rust binaries. Possible values: [-1, 0, 1]. -1 means use the value of the toolchain.experimental_use_cc_common_link boolean build setting to determine. 0 means do not use cc_common.link (use rustc instead). 1 means use cc_common.link. | Integer | optional | `-1` |
11251126
| <a id="rust_test-malloc"></a>malloc | Override the default dependency on `malloc`.<br><br>By default, Rust binaries linked with cc_common.link are linked against `@bazel_tools//tools/cpp:malloc"`, which is an empty library and the resulting binary will use libc's `malloc`. This label must refer to a `cc_library` rule. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `"@bazel_tools//tools/cpp:malloc"` |
11261127
| <a id="rust_test-platform"></a>platform | Optional platform to transition the test to. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | `None` |

rust/private/rust.bzl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,10 @@ def _rust_test_impl(ctx):
430430
env["RUST_LLVM_PROFDATA"] = llvm_profdata_path
431431
components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
432432
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
433-
providers.append(testing.TestEnvironment(env))
433+
providers.append(RunEnvironmentInfo(
434+
environment = env,
435+
inherited_environment = ctx.attr.env_inherit,
436+
))
434437

435438
return providers
436439

@@ -770,6 +773,9 @@ _rust_test_attrs = dict({
770773
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
771774
"""),
772775
),
776+
"env_inherit": attr.string_list(
777+
doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.",
778+
),
773779
"use_libtest_harness": attr.bool(
774780
mandatory = False,
775781
default = True,

rust/private/rustfmt.bzl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -235,13 +235,15 @@ def _rustfmt_test_impl(ctx):
235235
runfiles = runfiles,
236236
executable = runner,
237237
),
238-
testing.TestEnvironment({
239-
"RUSTFMT_MANIFESTS": ctx.configuration.host_path_separator.join([
240-
workspace + "/" + manifest.short_path
241-
for manifest in sorted(manifests.to_list())
242-
]),
243-
"RUST_BACKTRACE": "1",
244-
}),
238+
RunEnvironmentInfo(
239+
environment = {
240+
"RUSTFMT_MANIFESTS": ctx.configuration.host_path_separator.join([
241+
workspace + "/" + manifest.short_path
242+
for manifest in sorted(manifests.to_list())
243+
]),
244+
"RUST_BACKTRACE": "1",
245+
},
246+
),
245247
]
246248

247249
rustfmt_test = rule(

test/current_toolchain_files/current_toolchain_files_test.bzl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,13 @@ def _current_toolchain_files_test_impl(ctx):
4141
runfiles = runfiles,
4242
executable = test_runner,
4343
),
44-
testing.TestEnvironment({
45-
"CURRENT_TOOLCHAIN_FILES_TEST_INPUT": input.short_path,
46-
"CURRENT_TOOLCHAIN_FILES_TEST_KIND": ctx.attr.kind,
47-
"CURRENT_TOOLCHAIN_FILES_TEST_PATTERN": ctx.attr.pattern,
48-
}),
44+
RunEnvironmentInfo(
45+
environment = {
46+
"CURRENT_TOOLCHAIN_FILES_TEST_INPUT": input.short_path,
47+
"CURRENT_TOOLCHAIN_FILES_TEST_KIND": ctx.attr.kind,
48+
"CURRENT_TOOLCHAIN_FILES_TEST_PATTERN": ctx.attr.pattern,
49+
},
50+
),
4951
]
5052

5153
current_toolchain_files_test = rule(

0 commit comments

Comments
 (0)