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.
diff --git a/docs/defs.md b/docs/defs.md
index 2f49a50..a1b55f4 100644
--- a/docs/defs.md
+++ b/docs/defs.md
@@ -498,9 +498,9 @@
<pre>
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>,
- <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>,
- <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>,
- <a href="#rust_test-version">version</a>)
+ <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>,
+ <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>,
+ <a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
</pre>
Builds a Rust test crate.
@@ -638,6 +638,7 @@
| <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` |
| <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 | `""` |
| <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 | `{}` |
+| <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 | `[]` |
| <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` |
| <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"` |
| <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` |
diff --git a/docs/flatten.md b/docs/flatten.md
index 46ef53c..ab5969b 100644
--- a/docs/flatten.md
+++ b/docs/flatten.md
@@ -981,9 +981,9 @@
<pre>
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>,
- <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>,
- <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>,
- <a href="#rust_test-version">version</a>)
+ <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>,
+ <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>,
+ <a href="#rust_test-use_libtest_harness">use_libtest_harness</a>, <a href="#rust_test-version">version</a>)
</pre>
Builds a Rust test crate.
@@ -1121,6 +1121,7 @@
| <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` |
| <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 | `""` |
| <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 | `{}` |
+| <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 | `[]` |
| <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` |
| <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"` |
| <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` |
diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl
index 1701880..78ee58f 100644
--- a/rust/private/rust.bzl
+++ b/rust/private/rust.bzl
@@ -430,7 +430,10 @@
env["RUST_LLVM_PROFDATA"] = llvm_profdata_path
components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/")
env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c])
- providers.append(testing.TestEnvironment(env))
+ providers.append(RunEnvironmentInfo(
+ environment = env,
+ inherited_environment = ctx.attr.env_inherit,
+ ))
return providers
@@ -770,6 +773,9 @@
["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution.
"""),
),
+ "env_inherit": attr.string_list(
+ doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.",
+ ),
"use_libtest_harness": attr.bool(
mandatory = False,
default = True,
diff --git a/rust/private/rustfmt.bzl b/rust/private/rustfmt.bzl
index 4b747aa..a47b10f 100644
--- a/rust/private/rustfmt.bzl
+++ b/rust/private/rustfmt.bzl
@@ -235,13 +235,15 @@
runfiles = runfiles,
executable = runner,
),
- testing.TestEnvironment({
- "RUSTFMT_MANIFESTS": ctx.configuration.host_path_separator.join([
- workspace + "/" + manifest.short_path
- for manifest in sorted(manifests.to_list())
- ]),
- "RUST_BACKTRACE": "1",
- }),
+ RunEnvironmentInfo(
+ environment = {
+ "RUSTFMT_MANIFESTS": ctx.configuration.host_path_separator.join([
+ workspace + "/" + manifest.short_path
+ for manifest in sorted(manifests.to_list())
+ ]),
+ "RUST_BACKTRACE": "1",
+ },
+ ),
]
rustfmt_test = rule(
diff --git a/test/current_toolchain_files/current_toolchain_files_test.bzl b/test/current_toolchain_files/current_toolchain_files_test.bzl
index 57965a1..c8db58d 100644
--- a/test/current_toolchain_files/current_toolchain_files_test.bzl
+++ b/test/current_toolchain_files/current_toolchain_files_test.bzl
@@ -41,11 +41,13 @@
runfiles = runfiles,
executable = test_runner,
),
- testing.TestEnvironment({
- "CURRENT_TOOLCHAIN_FILES_TEST_INPUT": input.short_path,
- "CURRENT_TOOLCHAIN_FILES_TEST_KIND": ctx.attr.kind,
- "CURRENT_TOOLCHAIN_FILES_TEST_PATTERN": ctx.attr.pattern,
- }),
+ RunEnvironmentInfo(
+ environment = {
+ "CURRENT_TOOLCHAIN_FILES_TEST_INPUT": input.short_path,
+ "CURRENT_TOOLCHAIN_FILES_TEST_KIND": ctx.attr.kind,
+ "CURRENT_TOOLCHAIN_FILES_TEST_PATTERN": ctx.attr.pattern,
+ },
+ ),
]
current_toolchain_files_test = rule(