From c4a1ab8b6577c4376aaaa5c3c2d4ef07d524175c Mon Sep 17 00:00:00 2001 From: Googler Date: Fri, 2 Sep 2022 08:50:21 -0700 Subject: [PATCH] Hookup env attribute to RunEnvironmentInfo for cc_binary. Fixes #16167 PiperOrigin-RevId: 471811734 Change-Id: I7c904cc9056b59587e95704ef98b7d727f27f568 --- .../builtins_bzl/common/cc/cc_binary.bzl | 13 ++++++-- src/test/shell/bazel/cc_integration_test.sh | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl index e04f048cd09dab..e132b5e58f94c5 100644 --- a/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_binary.bzl @@ -957,12 +957,19 @@ def _impl(ctx): # We construct DefaultInfo here, as other cc_binary-like rules (cc_test) need # a different DefaultInfo. - default_info = DefaultInfo( + other_providers.append(DefaultInfo( files = binary_info.files, runfiles = binary_info.runfiles, executable = binary_info.executable, - ) - other_providers.append(default_info) + )) + + # We construct RunEnvironmentInfo here as well. + other_providers.append(RunEnvironmentInfo( + environment = cc_helper.get_expanded_env(ctx, {}), + # cc_binary does not have env_inherit attr. + inherited_environment = [], + )) + if ctx.fragments.cpp.enable_legacy_cc_provider(): # buildifier: disable=rule-impl-return return struct( diff --git a/src/test/shell/bazel/cc_integration_test.sh b/src/test/shell/bazel/cc_integration_test.sh index f681290fde745a..24d94b5cd33c93 100755 --- a/src/test/shell/bazel/cc_integration_test.sh +++ b/src/test/shell/bazel/cc_integration_test.sh @@ -1426,6 +1426,37 @@ EOF FOO=1 bazel test //pkg:foo_test &> "$TEST_log" || fail "Should have inherited FOO env." } +function test_env_attr_cc_binary() { + mkdir pkg + cat > pkg/BUILD < pkg/foo_test.cc < + +int main() { + auto foo = getenv("FOO"); + if (foo == nullptr) { + return 1; + } + return 0; +} +EOF + + bazel run //pkg:foo_bin &> "$TEST_log" && fail "Did not fail as expected. ENV leak?" || true + bazel run //pkg:foo_bin_with_env &> "$TEST_log" || fail "Should have used env attr." +} + function test_getting_compile_action_env_with_cctoolchain_config_features() { [ "$PLATFORM" != "darwin" ] || return 0