From dff064e21b6c82ba97a287449c6f05f373eba7d6 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Mon, 26 Aug 2024 12:01:14 -0700 Subject: [PATCH] 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. --- docs/defs.md | 7 ++++--- docs/flatten.md | 7 ++++--- rust/private/rust.bzl | 8 +++++++- rust/private/rustfmt.bzl | 16 +++++++++------- .../current_toolchain_files_test.bzl | 12 +++++++----- 5 files changed, 31 insertions(+), 19 deletions(-) diff --git a/docs/defs.md b/docs/defs.md index 2f49a50912..a1b55f4f3d 100644 --- a/docs/defs.md +++ b/docs/defs.md @@ -498,9 +498,9 @@ When building the whole binary in Bazel, use `rust_library` instead.
 rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features,
-          crate_name, crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
-          proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, use_libtest_harness,
-          version)
+          crate_name, crate_root, edition, env, env_inherit, experimental_use_cc_common_link, malloc,
+          platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
+          use_libtest_harness, version)
 
Builds a Rust test crate. @@ -638,6 +638,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | crate_root | The file that will be passed to `rustc` to be used for building this crate.

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. | Label | optional | `None` | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | | 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. | Dictionary: String -> String | optional | `{}` | +| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | `[]` | | 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` | | malloc | Override the default dependency on `malloc`.

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. | Label | optional | `"@bazel_tools//tools/cpp:malloc"` | | platform | Optional platform to transition the test to. | Label | optional | `None` | diff --git a/docs/flatten.md b/docs/flatten.md index 46ef53c76d..ab5969b550 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -981,9 +981,9 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
 rust_test(name, deps, srcs, data, aliases, alwayslink, compile_data, crate, crate_features,
-          crate_name, crate_root, edition, env, experimental_use_cc_common_link, malloc, platform,
-          proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp, use_libtest_harness,
-          version)
+          crate_name, crate_root, edition, env, env_inherit, experimental_use_cc_common_link, malloc,
+          platform, proc_macro_deps, rustc_env, rustc_env_files, rustc_flags, stamp,
+          use_libtest_harness, version)
 
Builds a Rust test crate. @@ -1121,6 +1121,7 @@ Run the test with `bazel test //hello_lib:greeting_test`. | crate_root | The file that will be passed to `rustc` to be used for building this crate.

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. | Label | optional | `None` | | edition | The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. | String | optional | `""` | | 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. | Dictionary: String -> String | optional | `{}` | +| env_inherit | Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. | List of strings | optional | `[]` | | 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` | | malloc | Override the default dependency on `malloc`.

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. | Label | optional | `"@bazel_tools//tools/cpp:malloc"` | | platform | Optional platform to transition the test to. | Label | optional | `None` | diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 1701880c4c..78ee58f196 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -430,7 +430,10 @@ def _rust_test_impl(ctx): 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 @@ _rust_test_attrs = dict({ ["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 4b747aa909..a47b10fee0 100644 --- a/rust/private/rustfmt.bzl +++ b/rust/private/rustfmt.bzl @@ -235,13 +235,15 @@ def _rustfmt_test_impl(ctx): 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 57965a1d6e..c8db58db07 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 @@ def _current_toolchain_files_test_impl(ctx): 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(