Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.0.0] Add --run_env to bazel run #24517

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading