Skip to content

Commit 0bef753

Browse files
bazel-iokeith
andauthored
[8.0.0] Add --run_env to bazel run (#24517)
This allows passing through env in a similar way to doing `FOO=bar bazel run` but also allows you to put this in a `.bazelrc`, and allows you to bypass any environment filtering done when launching bazel. Closes #24068. PiperOrigin-RevId: 692853922 Change-Id: I802c31f2e0b30a738a41f24395dedbaf0b7caef1 Commit e60d886 Co-authored-by: Keith Smiley <[email protected]>
1 parent 0404e84 commit 0bef753

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@
9292
import com.google.devtools.build.lib.vfs.FileSystemUtils;
9393
import com.google.devtools.build.lib.vfs.Path;
9494
import com.google.devtools.build.lib.vfs.PathFragment;
95+
import com.google.devtools.common.options.Converters;
9596
import com.google.devtools.common.options.Option;
9697
import com.google.devtools.common.options.OptionDocumentationCategory;
9798
import com.google.devtools.common.options.OptionEffectTag;
@@ -164,6 +165,22 @@ public static class RunOptions extends OptionsBase {
164165
"If true, includes paths to replace in ExecRequest to make the resulting paths"
165166
+ " portable.")
166167
public boolean portablePaths;
168+
169+
@Option(
170+
name = "run_env",
171+
converter = Converters.OptionalAssignmentConverter.class,
172+
allowMultiple = true,
173+
defaultValue = "null",
174+
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
175+
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
176+
help =
177+
"Specifies the set of environment variables available to actions with target"
178+
+ " configuration. Variables can be either specified by name, in which case the"
179+
+ " value will be taken from the invocation environment, or by the name=value pair"
180+
+ " which sets the value independent of the invocation environment. This option can"
181+
+ " be used multiple times; for options given for the same variable, the latest"
182+
+ " wins, options for different variables accumulate.")
183+
public List<Map.Entry<String, String>> runEnvironment;
167184
}
168185

169186
private static final String NO_TARGET_MESSAGE = "No targets found to run";
@@ -267,6 +284,11 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
267284
// Only necessary in --batch since the command runs as a subprocess of the java server.
268285
finalRunEnv.putAll(env.getClientEnv());
269286
}
287+
288+
for (Map.Entry<String, String> entry : runOptions.runEnvironment) {
289+
finalRunEnv.put(entry.getKey(), entry.getValue());
290+
}
291+
270292
ExecRequest.Builder execRequest;
271293
try {
272294
boolean shouldRunTarget = runOptions.scriptPath == null && runOptions.runBuiltTarget;

src/test/shell/integration/run_test.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,38 @@ EOF
678678
fi
679679
}
680680

681+
function test_run_env() {
682+
local -r pkg="pkg${LINENO}"
683+
mkdir -p "${pkg}"
684+
cat > "$pkg/BUILD" <<'EOF'
685+
sh_binary(
686+
name = "foo",
687+
srcs = ["foo.sh"],
688+
env = {
689+
"FROMBUILD": "1",
690+
"OVERRIDDEN_RUN_ENV": "2",
691+
}
692+
)
693+
EOF
694+
cat > "$pkg/foo.sh" <<'EOF'
695+
#!/bin/bash
696+
697+
set -euo pipefail
698+
699+
echo "FROMBUILD: '$FROMBUILD'"
700+
echo "OVERRIDDEN_RUN_ENV: '$OVERRIDDEN_RUN_ENV'"
701+
echo "RUN_ENV_ONLY: '$RUN_ENV_ONLY'"
702+
EOF
703+
704+
chmod +x "$pkg/foo.sh"
705+
706+
bazel run --run_env=OVERRIDDEN_RUN_ENV=FOO --run_env=RUN_ENV_ONLY=BAR "//$pkg:foo" >"$TEST_log" || fail "expected run to succeed"
707+
708+
expect_log "FROMBUILD: '1'"
709+
expect_log "OVERRIDDEN_RUN_ENV: 'FOO'"
710+
expect_log "RUN_ENV_ONLY: 'BAR'"
711+
}
712+
681713
# Usage: assert_starts_with PREFIX STRING_TO_CHECK.
682714
# Asserts that `$1` is a prefix of `$2`.
683715
function assert_starts_with() {

0 commit comments

Comments
 (0)