Skip to content

Commit e667082

Browse files
mai93copybara-github
authored andcommitted
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: bazelbuild#12403 Closes bazelbuild#12694. PiperOrigin-RevId: 348011332
1 parent f5b6abc commit e667082

File tree

2 files changed

+225
-0
lines changed

2 files changed

+225
-0
lines changed

src/main/java/com/google/devtools/build/lib/analysis/config/CoreOptions.java

+1
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,7 @@ public FragmentOptions getHost() {
963963

964964
// Pass host action environment variables
965965
host.actionEnvironment = hostActionEnvironment;
966+
host.hostActionEnvironment = hostActionEnvironment;
966967

967968
return host;
968969
}

src/test/shell/integration/action_env_test.sh

+224
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,27 @@ sh_test(
4444
name = "test_env_foo",
4545
srcs = ["test_env_foo.sh"],
4646
)
47+
48+
genrule(
49+
name = "show_host_env_using_tools",
50+
tools = ["env.txt"],
51+
outs = ["tools_env.txt"],
52+
cmd = "cp \$(location env.txt) \"\$@\""
53+
)
54+
55+
genrule(
56+
name = "show_host_env_using_exec_tools",
57+
exec_tools = ["env.txt"],
58+
outs = ["exec_tools_env.txt"],
59+
cmd = "cp \$(location env.txt) \"\$@\""
60+
)
61+
62+
genrule(
63+
name = "show_host_env_using_successive_exec_tools",
64+
exec_tools = ["exec_tools_env.txt"],
65+
outs = ["successive_exec_tools_env.txt"],
66+
cmd = "cp \$(location exec_tools_env.txt) \"\$@\""
67+
)
4768
EOF
4869
cat > pkg/build.bzl <<EOF
4970
def _impl(ctx):
@@ -178,6 +199,9 @@ function test_env_freezing() {
178199
expect_log "build --action_env=FREEZE_TEST_BUILD=client_build"
179200

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

183207
function test_use_default_shell_env {
@@ -218,4 +242,204 @@ function test_action_env_changes_honored {
218242

219243
}
220244

245+
function test_host_env_using_tools_simple() {
246+
export FOO=baz
247+
248+
# If FOO is passed using --host_action_env, it should be listed in host env vars
249+
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_tools \
250+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
251+
252+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
253+
expect_log "FOO=bar"
254+
255+
# But if FOO is passed using --action_env, it should not be listed in host env vars
256+
bazel build --action_env=FOO=bar pkg:show_host_env_using_tools \
257+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
258+
259+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
260+
expect_not_log "FOO=bar"
261+
}
262+
263+
function test_host_env_using_tools_latest_wins() {
264+
export FOO=environmentfoo
265+
export BAR=environmentbar
266+
bazel build --host_action_env=FOO=foo \
267+
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_tools \
268+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
269+
270+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
271+
expect_log "FOO=foo"
272+
expect_log "BAR=bar"
273+
}
274+
275+
function test_client_env_using_tools() {
276+
export FOO=startup_foo
277+
bazel clean --expunge
278+
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
279+
export FOO=client_foo
280+
bazel build --host_action_env=FOO pkg:show_host_env_using_tools || \
281+
fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
282+
283+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
284+
expect_log "FOO=client_foo"
285+
}
286+
287+
function test_redo_host_env_using_tools() {
288+
export FOO=initial_foo
289+
export UNRELATED=some_value
290+
bazel build --host_action_env=FOO pkg:show_host_env_using_tools \
291+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
292+
293+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
294+
expect_log "FOO=initial_foo"
295+
296+
# If an unrelated value changes, we expect the action not to be executed again
297+
export UNRELATED=some_other_value
298+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
299+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
300+
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
301+
302+
# However, if a used variable changes, we expect the change to be propagated
303+
export FOO=changed_foo
304+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
305+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
306+
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
307+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
308+
expect_log "FOO=changed_foo"
309+
310+
# But repeating the build with no further changes, no action should happen
311+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_tools 2> $TEST_log \
312+
|| fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
313+
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_tools'
314+
}
315+
316+
function test_latest_wins_arg_using_tools() {
317+
export FOO=bar
318+
export BAR=baz
319+
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
320+
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
321+
322+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
323+
expect_log "FOO=foo"
324+
expect_log "BAR=baz"
325+
expect_not_log "FOO=bar"
326+
}
327+
328+
function test_latest_wins_env_using_tools() {
329+
export FOO=bar
330+
export BAR=baz
331+
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
332+
pkg:show_host_env_using_tools || fail "${PRODUCT_NAME} build show_host_env_using_tools failed"
333+
334+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/tools_env.txt > $TEST_log
335+
expect_log "FOO=bar"
336+
expect_log "BAR=baz"
337+
expect_not_log "FOO=foo"
338+
}
339+
340+
function test_host_env_using_exec_tools_simple() {
341+
export FOO=baz
342+
343+
# If FOO is passed using --host_action_env, it should be listed in host env vars
344+
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_exec_tools \
345+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
346+
347+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
348+
expect_log "FOO=bar"
349+
350+
# But if FOO is passed using --action_env, it should not be listed in host env vars
351+
bazel build --action_env=FOO=bar pkg:show_host_env_using_exec_tools \
352+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
353+
354+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
355+
expect_not_log "FOO=bar"
356+
}
357+
358+
function test_host_env_using_exec_tools_latest_wins() {
359+
export FOO=environmentfoo
360+
export BAR=environmentbar
361+
bazel build --host_action_env=FOO=foo \
362+
--host_action_env=BAR=willbeoverridden --host_action_env=BAR=bar pkg:show_host_env_using_exec_tools \
363+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
364+
365+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
366+
expect_log "FOO=foo"
367+
expect_log "BAR=bar"
368+
}
369+
370+
function test_client_env_using_exec_tools() {
371+
export FOO=startup_foo
372+
bazel clean --expunge
373+
bazel help build > /dev/null || fail "${PRODUCT_NAME} help failed"
374+
export FOO=client_foo
375+
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools || \
376+
fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
377+
378+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
379+
expect_log "FOO=client_foo"
380+
}
381+
382+
function test_redo_host_env_using_exec_tools() {
383+
export FOO=initial_foo
384+
export UNRELATED=some_value
385+
bazel build --host_action_env=FOO pkg:show_host_env_using_exec_tools \
386+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
387+
388+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
389+
expect_log "FOO=initial_foo"
390+
391+
# If an unrelated value changes, we expect the action not to be executed again
392+
export UNRELATED=some_other_value
393+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
394+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
395+
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
396+
397+
# However, if a used variable changes, we expect the change to be propagated
398+
export FOO=changed_foo
399+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
400+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
401+
expect_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
402+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
403+
expect_log "FOO=changed_foo"
404+
405+
# But repeating the build with no further changes, no action should happen
406+
bazel build --host_action_env=FOO -s pkg:show_host_env_using_exec_tools 2> $TEST_log \
407+
|| fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
408+
expect_not_log '^SUBCOMMAND.*pkg:show_host_env_using_exec_tools'
409+
}
410+
411+
function test_latest_wins_arg_using_exec_tools() {
412+
export FOO=bar
413+
export BAR=baz
414+
bazel build --host_action_env=BAR --host_action_env=FOO --host_action_env=FOO=foo \
415+
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
416+
417+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
418+
expect_log "FOO=foo"
419+
expect_log "BAR=baz"
420+
expect_not_log "FOO=bar"
421+
}
422+
423+
function test_latest_wins_env_using_exec_tools() {
424+
export FOO=bar
425+
export BAR=baz
426+
bazel build --host_action_env=BAR --host_action_env=FOO=foo --host_action_env=FOO \
427+
pkg:show_host_env_using_exec_tools || fail "${PRODUCT_NAME} build show_host_env_using_exec_tools failed"
428+
429+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/exec_tools_env.txt > $TEST_log
430+
expect_log "FOO=bar"
431+
expect_log "BAR=baz"
432+
expect_not_log "FOO=foo"
433+
}
434+
435+
function test_host_env_using_successive_exec_tools_simple() {
436+
export FOO=baz
437+
438+
bazel build --host_action_env=FOO=bar pkg:show_host_env_using_successive_exec_tools \
439+
|| fail "${PRODUCT_NAME} build show_host_env_using_successive_exec_tool failed"
440+
441+
cat `bazel info ${PRODUCT_NAME}-genfiles`/pkg/successive_exec_tools_env.txt > $TEST_log
442+
expect_log "FOO=bar"
443+
}
444+
221445
run_suite "Tests for bazel's handling of environment variables in actions"

0 commit comments

Comments
 (0)