Skip to content

Commit

Permalink
Pass --host_action_env to host options hostActionEnvironment attribute
Browse files Browse the repository at this point in the history
This PR passes the value of `host_action_env` to `hostActionEnvironment` attribute of the host options in `CoreOptions.getHost`.

Fixes: #12403

Closes #12694.

PiperOrigin-RevId: 348011332
  • Loading branch information
mai93 authored and copybara-github committed Dec 17, 2020
1 parent f5b6abc commit e667082
Show file tree
Hide file tree
Showing 2 changed files with 225 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,7 @@ public FragmentOptions getHost() {

// Pass host action environment variables
host.actionEnvironment = hostActionEnvironment;
host.hostActionEnvironment = hostActionEnvironment;

return host;
}
Expand Down
224 changes: 224 additions & 0 deletions src/test/shell/integration/action_env_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,27 @@ sh_test(
name = "test_env_foo",
srcs = ["test_env_foo.sh"],
)
genrule(
name = "show_host_env_using_tools",
tools = ["env.txt"],
outs = ["tools_env.txt"],
cmd = "cp \$(location env.txt) \"\$@\""
)
genrule(
name = "show_host_env_using_exec_tools",
exec_tools = ["env.txt"],
outs = ["exec_tools_env.txt"],
cmd = "cp \$(location env.txt) \"\$@\""
)
genrule(
name = "show_host_env_using_successive_exec_tools",
exec_tools = ["exec_tools_env.txt"],
outs = ["successive_exec_tools_env.txt"],
cmd = "cp \$(location exec_tools_env.txt) \"\$@\""
)
EOF
cat > pkg/build.bzl <<EOF
def _impl(ctx):
Expand Down Expand Up @@ -178,6 +199,9 @@ function test_env_freezing() {
expect_log "build --action_env=FREEZE_TEST_BUILD=client_build"

rm -f .${PRODUCT_NAME}rc
# Recreate .bazelrc as removing it affects other tests that run in the
# same shard with this test.
write_default_bazelrc
}

function test_use_default_shell_env {
Expand Down Expand Up @@ -218,4 +242,204 @@ function test_action_env_changes_honored {

}

function test_host_env_using_tools_simple() {
export FOO=baz

# If FOO is passed using --host_action_env, it should be listed in host env vars
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=bar"

# But if FOO is passed using --action_env, it should not be listed in host env vars
bazel build --action_env=FOO=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_not_log "FOO=bar"
}

function test_host_env_using_tools_latest_wins() {
export FOO=environmentfoo
export BAR=environmentbar
bazel build --host_action_env=FOO=foo \
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=bar"
}

function test_client_env_using_tools() {
export FOO=startup_foo
bazel clean --expunge
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
export FOO=client_foo
bazel build --host_action_env=FOO pkg:show_host_env_using_tools || \
fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=client_foo"
}

function test_redo_host_env_using_tools() {
export FOO=initial_foo
export UNRELATED=some_value
bazel build --host_action_env=FOO pkg:show_host_env_using_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=initial_foo"

# If an unrelated value changes, we expect the action not to be executed again
export UNRELATED=some_other_value
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'

# However, if a used variable changes, we expect the change to be propagated
export FOO=changed_foo
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=changed_foo"

# But repeating the build with no further changes, no action should happen
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
}

function test_latest_wins_arg_using_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=baz"
expect_not_log "FOO=bar"
}

function test_latest_wins_env_using_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
expect_log "FOO=bar"
expect_log "BAR=baz"
expect_not_log "FOO=foo"
}

function test_host_env_using_exec_tools_simple() {
export FOO=baz

# If FOO is passed using --host_action_env, it should be listed in host env vars
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"

# But if FOO is passed using --action_env, it should not be listed in host env vars
bazel build --action_env=FOO=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_not_log "FOO=bar"
}

function test_host_env_using_exec_tools_latest_wins() {
export FOO=environmentfoo
export BAR=environmentbar
bazel build --host_action_env=FOO=foo \
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=bar"
}

function test_client_env_using_exec_tools() {
export FOO=startup_foo
bazel clean --expunge
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
export FOO=client_foo
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools || \
fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=client_foo"
}

function test_redo_host_env_using_exec_tools() {
export FOO=initial_foo
export UNRELATED=some_value
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=initial_foo"

# If an unrelated value changes, we expect the action not to be executed again
export UNRELATED=some_other_value
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'

# However, if a used variable changes, we expect the change to be propagated
export FOO=changed_foo
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=changed_foo"

# But repeating the build with no further changes, no action should happen
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
}

function test_latest_wins_arg_using_exec_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=foo"
expect_log "BAR=baz"
expect_not_log "FOO=bar"
}

function test_latest_wins_env_using_exec_tools() {
export FOO=bar
export BAR=baz
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"
expect_log "BAR=baz"
expect_not_log "FOO=foo"
}

function test_host_env_using_successive_exec_tools_simple() {
export FOO=baz

bazel build --host_action_env=FOO=bar pkg:show_host_env_using_successive_exec_tools \
|| fail "${PRODUCT_NAME} build show_host_env_using_successive_exec_tool failed"

cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/successive_exec_tools_env.txt > $TEST_log
expect_log "FOO=bar"
}

run_suite "Tests for bazel's handling of environment variables in actions"

0 comments on commit e667082

Please sign in to comment.