From 95720cbf6ac7de4b93902e1a722f9608e1b8cf69 Mon Sep 17 00:00:00 2001 From: Joseph Gette Date: Fri, 24 Apr 2026 11:59:35 +0200 Subject: [PATCH] Don't leak default_shell_env into published CrateInfo.rustc_env At the tail of rustc_compile_action, CrateInfo is re-created with rustc_env = env. But env was built as `dict(default_shell_env) + env_from_args`, so it carries the host's default_shell_env (PATH, LIB, INCLUDE, ...) on top of the rustc-specific env. Every downstream consumer that inherits crate_info.rustc_env (notably rust_test(crate = ...)) then picks up the host env, and at its own compile time the inherited values clobber cc_toolchain's link_env -- construct_arguments applies link_env first (~L1183) and crate_info.rustc_env after. Persisting only env_from_args keeps CrateInfo honest: Bazel re-injects default_shell_env at action execution anyway, so there is no reason to bake it into provider state. Fixes #3989. --- rust/private/rustc.bzl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index f43ddaba67..c8f6ecc9e6 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1797,8 +1797,12 @@ def rustc_compile_action( ) if crate_info_dict != None: + # Persist only rustc-specific env; the merged `env` above also carries + # ctx.configuration.default_shell_env, which must not leak through + # CrateInfo -- it would otherwise clobber cc_toolchain link_env in + # downstream rust_test(crate = ...) (see bazelbuild/rules_rust#3989). crate_info_dict.update({ - "rustc_env": env, + "rustc_env": env_from_args, }) crate_info = rust_common.create_crate_info( deps = depset(deps),