Skip to content

Commit

Permalink
Include --run_env in --script_path
Browse files Browse the repository at this point in the history
Previously this only applied if you did `bazel run` without
`--script_path`.
  • Loading branch information
keith committed Jan 24, 2025
1 parent 746bc57 commit 2f3f5b3
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
ImmutableList.copyOf(targetAndArgs.subList(1, targetAndArgs.size()));
RunCommandLine runCommandLine;
try {
runCommandLine = getCommandLineInfo(env, builtTargets, options, argsFromResidue, testPolicy);
runCommandLine = getCommandLineInfo(env, builtTargets, options, argsFromResidue, runOptions.runEnvironment, testPolicy);
} catch (RunCommandException e) {
return e.result;
}
Expand All @@ -286,10 +286,6 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
finalRunEnv.putAll(env.getClientEnv());
}

for (Map.Entry<String, String> entry : runOptions.runEnvironment) {
finalRunEnv.put(entry.getKey(), entry.getValue());
}

ExecRequest.Builder execRequest;
try {
boolean shouldRunTarget = runOptions.scriptPath == null && runOptions.runBuiltTarget;
Expand Down Expand Up @@ -640,6 +636,7 @@ private static RunCommandLine getCommandLineInfo(
BuiltTargets builtTargets,
OptionsParsingResult options,
ImmutableList<String> argsFromResidue,
List<Map.Entry<String, String>> extraRunEnvironment,
TestPolicy testPolicy)
throws RunCommandException {
if (builtTargets.targetToRun.getProvider(TestProvider.class) != null) {
Expand All @@ -660,6 +657,9 @@ private static RunCommandLine getCommandLineInfo(
}
TreeMap<String, String> runEnvironment = makeMutableRunEnvironment(env);
actionEnvironment.resolve(runEnvironment, env.getClientEnv());
for (var entry : extraRunEnvironment) {
runEnvironment.put(entry.getKey(), entry.getValue());
}

ImmutableList<String> argsFromBinary;
try {
Expand Down
39 changes: 39 additions & 0 deletions src/test/shell/integration/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,45 @@ EOF
expect_log "RUN_ENV_ONLY: 'BAR'"
}

function test_run_env_script_path() {
if "$is_windows"; then
# TODO(laszlocsomor): fix this test on Windows, and enable it.
return
fi

local -r pkg="pkg${LINENO}"
mkdir -p "${pkg}"
cat > "$pkg/BUILD" <<'EOF'
sh_binary(
name = "foo",
srcs = ["foo.sh"],
env = {
"FROMBUILD": "1",
"OVERRIDDEN_RUN_ENV": "2",
}
)
EOF
cat > "$pkg/foo.sh" <<'EOF'
#!/bin/bash
set -euo pipefail
echo "FROMBUILD: '$FROMBUILD'"
echo "OVERRIDDEN_RUN_ENV: '$OVERRIDDEN_RUN_ENV'"
echo "RUN_ENV_ONLY: '$RUN_ENV_ONLY'"
EOF

chmod +x "$pkg/foo.sh"

bazel run --script_path=script.sh --run_env=OVERRIDDEN_RUN_ENV=FOO --run_env=RUN_ENV_ONLY=BAR "//$pkg:foo" || fail "expected run to succeed"

./script.sh >"$TEST_log" || fail "expected script to succeed"

expect_log "FROMBUILD: '1'"
expect_log "OVERRIDDEN_RUN_ENV: 'FOO'"
expect_log "RUN_ENV_ONLY: 'BAR'"
}

# Usage: assert_starts_with PREFIX STRING_TO_CHECK.
# Asserts that `$1` is a prefix of `$2`.
function assert_starts_with() {
Expand Down

0 comments on commit 2f3f5b3

Please sign in to comment.