From 700269893fbc20925be817b6971884a08723844f Mon Sep 17 00:00:00 2001 From: mai93 Date: Thu, 17 Dec 2020 06:34:40 -0800 Subject: [PATCH] Pass --host_action_env to host options hostActionEnvironment attribute 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 --- .../lib/analysis/config/CoreOptions.java | 1 + src/test/shell/integration/action_env_test.sh | 224 ++++++++++++++++++ 2 files changed, 225 insertions(+) diff --git a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java index c016d6c267bf91..cb0d2a409f4ff3 100644 --- a/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java +++ b/src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java @@ -964,6 +964,7 @@ public FragmentOptions getHost() { // Pass host action environment variables host.actionEnvironment = hostActionEnvironment; + host.hostActionEnvironment = hostActionEnvironment; return host; } diff --git a/src/test/shell/integration/action_env_test.sh b/src/test/shell/integration/action_env_test.sh index 933bcd344e2020..08af2abd059630 100755 --- a/src/test/shell/integration/action_env_test.sh +++ b/src/test/shell/integration/action_env_test.sh @@ -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 < $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"