Skip to content

Commit 4169f9d

Browse files
keithcopybara-github
authored andcommitted
Make target's env higher priority than --run_env
Similar to env + --test_env, --run_env should be a lower priority than the env directly in the BUILD file. RELNOTES[inc]: fix --run_env order to prioritize BUILD env over command line Closes bazelbuild#26887. PiperOrigin-RevId: 805748782 Change-Id: I327c3ce7d159b8c82ebb6785a7490d30ca805fa7
1 parent b5a6d41 commit 4169f9d

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/main/java/com/google/devtools/build/lib/runtime/commands/RunCommand.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -656,7 +656,7 @@ private static RunCommandLine getCommandLineInfo(
656656
TreeMap<String, String> runEnvironment = makeMutableRunEnvironment(env);
657657
HashSet<String> envVariablesToClear = new HashSet<>();
658658
ImmutableMap<String, String> clientEnv = env.getClientEnv();
659-
actionEnvironment.resolve(runEnvironment, clientEnv);
659+
// Process --run_env flags first
660660
for (var envVar : extraRunEnvironment) {
661661
switch (envVar) {
662662
case Converters.EnvVar.Set(String name, String value) -> {
@@ -680,6 +680,8 @@ private static RunCommandLine getCommandLineInfo(
680680
}
681681
}
682682
}
683+
// Then let the target's environment override --run_env flags
684+
actionEnvironment.resolve(runEnvironment, clientEnv);
683685

684686
return constructCommandLine(
685687
env,

src/test/shell/integration/run_test.sh

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,9 @@ EOF
813813

814814
function test_run_env() {
815815
add_rules_shell "MODULE.bazel"
816+
add_to_bazelrc "run --run_env=OVERRIDDEN_RUN_ENV=OVERRIDDEN_VALUE_FROM_BAZELRC"
817+
add_to_bazelrc "run --run_env=FROM_BAZELRC=VALUE_FROM_BAZELRC"
818+
add_to_bazelrc "run --run_env=FROM_BAZELRC_THEN_UNUSET=VALUE_FROM_BAZELRC"
816819
local -r pkg="pkg${LINENO}"
817820
mkdir -p "${pkg}"
818821
cat > "$pkg/BUILD" <<'EOF'
@@ -834,6 +837,8 @@ set -euo pipefail
834837
835838
echo "FROMBUILD: '$FROMBUILD'"
836839
echo "OVERRIDDEN_RUN_ENV: '$OVERRIDDEN_RUN_ENV'"
840+
echo "FROM_BAZELRC: '$FROM_BAZELRC'"
841+
echo "FROM_BAZELRC_THEN_UNUSET: '${FROM_BAZELRC_THEN_UNUSET:=<unset>}'"
837842
echo "RUN_ENV_ONLY: '$RUN_ENV_ONLY'"
838843
echo "EMPTY_RUN_ENV: '$EMPTY_RUN_ENV'"
839844
echo "INHERITED_RUN_ENV: '$INHERITED_RUN_ENV'"
@@ -849,13 +854,16 @@ EOF
849854
--run_env=EMPTY_RUN_ENV= \
850855
--run_env=INHERITED_RUN_ENV \
851856
--run_env==REMOVED_RUN_ENV \
857+
--run_env==FROM_BAZELRC_THEN_UNUSET \
852858
--run_env=SET_UNSET_SET=set1 \
853859
--run_env==SET_UNSET_SET \
854860
--run_env=SET_UNSET_SET=set2 \
855861
"//$pkg:foo" >"$TEST_log" || fail "expected run to succeed"
856862

857863
expect_log "FROMBUILD: '1'"
858-
expect_log "OVERRIDDEN_RUN_ENV: 'FOO'"
864+
expect_log "OVERRIDDEN_RUN_ENV: '2'"
865+
expect_log "FROM_BAZELRC: 'VALUE_FROM_BAZELRC'"
866+
expect_log "FROM_BAZELRC_THEN_UNUSET: '<unset>'"
859867
expect_log "RUN_ENV_ONLY: 'BAR'"
860868
expect_log "EMPTY_RUN_ENV: ''"
861869
expect_log "INHERITED_RUN_ENV: 'BAZ'"
@@ -999,7 +1007,7 @@ EOF
9991007
./script.bat >"$TEST_log" || fail "expected script to succeed"
10001008

10011009
expect_log "FROMBUILD: '1'"
1002-
expect_log "OVERRIDDEN_RUN_ENV: 'FOO'"
1010+
expect_log "OVERRIDDEN_RUN_ENV: '2'"
10031011
expect_log "RUN_ENV_ONLY: 'BAR'"
10041012
}
10051013

0 commit comments

Comments
 (0)