From 55de2a914c5b9f2af02a32f90d6dad909d6d0599 Mon Sep 17 00:00:00 2001 From: Brentley Jones Date: Mon, 19 Aug 2024 14:42:56 -0500 Subject: [PATCH] Filter environment variables when building with Bazel In order for Xcode builds to work with custom environment variables you need to use `xcodeproj.bazel_env`. This just takes the extra step of filtering out all other environment variables, ensuring more analysis cache hits. You can already do this in your own `tools/bazel`, but now people can get the benefit regardless. This change is needed for shared outputs bases to be performant. Signed-off-by: Brentley Jones --- xcodeproj/internal/templates/bazel_build.sh | 17 ++++++++++++++++- xcodeproj/internal/templates/runner.sh | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/xcodeproj/internal/templates/bazel_build.sh b/xcodeproj/internal/templates/bazel_build.sh index 6fc791abf0..3b1d062b5e 100755 --- a/xcodeproj/internal/templates/bazel_build.sh +++ b/xcodeproj/internal/templates/bazel_build.sh @@ -68,8 +68,23 @@ if [[ -s "$BAZEL_INTEGRATION_DIR/xcodeproj_extra_flags.bazelrc" ]]; then fi readonly bazelrcs +readonly allowed_vars=( + "BUILD_WORKSPACE_DIRECTORY" + "DEVELOPER_DIR" + "HOME" + "TERM" + "USER" +) +passthrough_env=() +for var in "${allowed_vars[@]}"; do + if [[ -n "${!var:-}" ]]; then + passthrough_env+=("$var=${!var}") + fi +done + readonly bazel_cmd=( - env + env -i + "${passthrough_env[@]}" %bazel_env% "%bazel_path%" diff --git a/xcodeproj/internal/templates/runner.sh b/xcodeproj/internal/templates/runner.sh index bee862b9d2..bc1dc35ada 100644 --- a/xcodeproj/internal/templates/runner.sh +++ b/xcodeproj/internal/templates/runner.sh @@ -178,8 +178,23 @@ common:rules_xcodeproj --repo_env=USE_CLANG_CL=%xcode_version% common:rules_xcodeproj --repo_env=XCODE_VERSION=%xcode_version% EOF +readonly allowed_vars=( + "BUILD_WORKSPACE_DIRECTORY" + "HOME" + "TERM" + "USER" +) +passthrough_env=() +for var in "${allowed_vars[@]}"; do + if [[ -n "${!var:-}" ]]; then + passthrough_env+=("$var=${!var}") + fi +done + bazel_cmd=( - env + env -i + "DEVELOPER_DIR=$developer_dir" + "${passthrough_env[@]}" "${envs[@]}" "$bazel_path"