Skip to content

Commit

Permalink
[8.0.0] Add --run_env to bazel run (#24517)
Browse files Browse the repository at this point in the history
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 <keithbsmiley@gmail.com>
  • Loading branch information
bazel-io and keith authored Nov 28, 2024
1 parent 0404e84 commit 0bef753
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import com.google.devtools.build.lib.vfs.FileSystemUtils;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.build.lib.vfs.PathFragment;
import com.google.devtools.common.options.Converters;
import com.google.devtools.common.options.Option;
import com.google.devtools.common.options.OptionDocumentationCategory;
import com.google.devtools.common.options.OptionEffectTag;
Expand Down Expand Up @@ -164,6 +165,22 @@ public static class RunOptions extends OptionsBase {
"If true, includes paths to replace in ExecRequest to make the resulting paths"
+ " portable.")
public boolean portablePaths;

@Option(
name = "run_env",
converter = Converters.OptionalAssignmentConverter.class,
allowMultiple = true,
defaultValue = "null",
documentationCategory = OptionDocumentationCategory.BAZEL_CLIENT_OPTIONS,
effectTags = {OptionEffectTag.AFFECTS_OUTPUTS},
help =
"Specifies the set of environment variables available to actions with target"
+ " configuration. Variables can be either specified by name, in which case the"
+ " value will be taken from the invocation environment, or by the name=value pair"
+ " which sets the value independent of the invocation environment. This option can"
+ " be used multiple times; for options given for the same variable, the latest"
+ " wins, options for different variables accumulate.")
public List<Map.Entry<String, String>> runEnvironment;
}

private static final String NO_TARGET_MESSAGE = "No targets found to run";
Expand Down Expand Up @@ -267,6 +284,11 @@ public BlazeCommandResult exec(CommandEnvironment env, OptionsParsingResult opti
// Only necessary in --batch since the command runs as a subprocess of the java server.
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
32 changes: 32 additions & 0 deletions src/test/shell/integration/run_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,38 @@ EOF
fi
}

function test_run_env() {
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 --run_env=OVERRIDDEN_RUN_ENV=FOO --run_env=RUN_ENV_ONLY=BAR "//$pkg:foo" >"$TEST_log" || fail "expected run 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 0bef753

Please sign in to comment.