Add --experimental_strict_repo_env option#24404
Add --experimental_strict_repo_env option#24404Silic0nS0ldier wants to merge 1 commit intobazelbuild:masterfrom
--experimental_strict_repo_env option#24404Conversation
|
The CI failure flushed out a potential bug. With this issue, any changes to |
Could you split the fix into a separate PR? That would make it easier to cherry-pick into patch releases and Bazel 8. Since |
|
Stale environment issue split to #24433 (this PR is dependent on the fix for tests to pass, so I'll leave it here as well and rebase once merged). |
My initial thoughts were to keep this as a project preference, since currently many repository rules are heavily influenced by the host environment (sometimes intentionally). But if this change is being viewed as something to move towards, it may make sense to classify this as an incompatible change like with On a related note, there are a few places that use the "old" repo environment in
And for sake of completion, the client environment is directly used in these places.
The only spot of concern for me here is the download manager. Seems like in its current state, this PR may introduce some fragmentation (though such fragmentation would have already been possible with |
|
Thanks for compiling this list, I'll have to think about this more. Since some repo rules do depend on the environment quite heavily, have you considered making this behavior configurable on the |
No, but only because I didn't think to. I'm not sure what such an API would look like. There is Some questions (not necessarily directed at anyone, just to help with ideation). Looking even further beyond this, a case could even be made for sandboxing certain repository rules. The download repos created by |
|
Keenly awaiting this one, thank you for this work 👍 |
|
What I'm currently thinking here is;
That represents a fairly significant expansion vs. the original scope, something that ought to go through the proposal process first. That all said, I do have a need to fully lock-down the repo environment (an extreme that is not suited to all) and would rather not leave this in limbo. So here is my plan;
|
|
This looks great! I did have one query:
With --strict_repo_env enabled, will environ(argument) allow repo rules to pull in additional env vars that have not been 'permitted' via --repo_env? |
This adds a new flag `--incompatible_repo_env_ignores_action_env` which when flipped stops `--action_env=NAME=VALUE` from affecting repository rule and module extension environments. Split from bazelbuild#24404. Closes bazelbuild#25238. PiperOrigin-RevId: 767031987 Change-Id: I1683935ab2728f6bbfcf499ea8a598770002020d
|
We are being impacted by this (we have tools that modify the current shell environment, and they are invalidating our builds), so I wondered if you would update on plans? In the meantime, is there any workaround, other than wrapping all bazel calls in a wrapper that sanitizes the environment? |
This adds a new flag `--incompatible_repo_env_ignores_action_env` which when flipped stops `--action_env=NAME=VALUE` from affecting repository rule and module extension environments. Split from #24404. Closes #25238. PiperOrigin-RevId: 767031987 Change-Id: I1683935ab2728f6bbfcf499ea8a598770002020d Commit 216ae9d Co-authored-by: Jordan Mele <jordan.mele@outlook.com.au>
fe563d9 to
94bebc6
Compare
|
For those interested in this change, #26300 may also be of interest. |
|
I would still love to see this land pre 9.0 |
9d8285c to
06a5aa7
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR introduces the --experimental_strict_repo_env flag to control environment variable inheritance for repository rules and module extensions. When enabled, only PATH, PATHEXT (on Windows), and variables explicitly specified via --repo_env are inherited, improving build hermeticity while maintaining necessary environment access for repository operations.
Key Changes
- Added
--experimental_strict_repo_envcommand-line flag to enable strict environment filtering - Separated environment variable handling into
repoEnvFromOptions(filtered set) andrepoEnv(full set) in CommandEnvironment - Updated repository and module extension code to accept both repository-specific and client environment variables, using the appropriate set based on the flag
Reviewed Changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java | Adds the new --experimental_strict_repo_env option definition and documentation |
| src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java | Implements environment variable filtering logic, tracking default inherited vars (PATH, PATHEXT) in repoEnvFromOptions |
| src/main/java/com/google/devtools/build/lib/bazel/BazelRepositoryModule.java | Wires up the repo environment supplier to use filtered or full environment based on flag state |
| src/main/java/com/google/devtools/build/lib/bazel/repository/RepositoryFetchFunction.java | Adds repoEnvironmentSupplier parameter to support environment filtering |
| src/main/java/com/google/devtools/build/lib/bazel/bzlmod/SingleExtensionEvalFunction.java | Adds repoEnvironmentSupplier parameter for module extension environment handling |
| src/main/java/com/google/devtools/build/lib/bazel/bzlmod/RegularRunnableExtension.java | Updates to pass through repo environment supplier for extension execution |
| src/main/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionContext.java | Accepts separate repo and client environment variables |
| src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkRepositoryContext.java | Updated to use separate repoEnvVariables and clientEnvVariables parameters |
| src/main/java/com/google/devtools/build/lib/bazel/repository/starlark/StarlarkBaseExternalContext.java | Refactors environment variable handling, renaming for clarity and using appropriate environment for different operations |
| src/main/java/com/google/devtools/build/lib/starlarkbuildapi/repository/RepositoryModuleApi.java | Updates documentation to reference the new flag and marks module extension environ parameter as deprecated |
| src/test/shell/bazel/starlark_repository_test.sh | Adds test validating strict environment behavior with default and explicit variables |
| src/test/java/com/google/devtools/build/lib/skyframe/*.java | Updates test infrastructure to pass both environment suppliers to functions |
| src/test/java/com/google/devtools/build/lib/bazel/repository/*.java | Updates test setup to provide both repo and client environment variables |
| src/test/java/com/google/devtools/build/lib/bazel/bzlmod/*.java | Updates test infrastructure for new constructor signatures |
| src/test/java/com/google/devtools/build/lib/analysis/util/AnalysisMock.java | Updates mock setup with dual environment suppliers |
| src/main/java/com/google/devtools/build/lib/skyframe/packages/BazelPackageLoader.java | Updates to provide both environment suppliers to repository fetch function |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
Outdated
Show resolved
Hide resolved
meteorcloudy
left a comment
There was a problem hiding this comment.
Thanks, looks good, just a few minor comments from me and Copilot
|
Would it be possible to get this in 8.x? |
|
We don't want to delay 8.5 release any further so will mark this one for later 8.x minor release. |
0dcd836 to
50b548c
Compare
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 20 out of 20 changed files in this pull request and generated 3 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java
Outdated
Show resolved
Hide resolved
...main/java/com/google/devtools/build/lib/starlarkbuildapi/repository/RepositoryModuleApi.java
Outdated
Show resolved
Hide resolved
src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
Outdated
Show resolved
Hide resolved
50b548c to
4b35347
Compare
This PR introduces a new flag `--experimental_strict_repo_env` which stops repository rules and module extensions from inheriting the client environment (making `--repo_env=NAME` more than just an advisory notice). When enabled up to 2 environment variables will still be forwarded (unless overridden or explicitly removed via `--repo_env==VARNAME`, see bazelbuild#26300 for more details); - `PATH` - All platforms - `PATHEXT` - Windows See `test_execute_environment_strict_vars` in `src/test/shell/bazel/starlark_repository_test.sh` for a demonstration. Note that the behavior is different to the similarly named `--incompatible_strict_action_env`, which stops _all_ environment variables (`--action_env` affects actions with `use_default_shell_env = True`) except those specified within the defining rule. This is by design as repository rules operate in an inherently non-hermetic domain, covering roles such as integrating with the C/C++ toolchain installed on the host. It does not make sense to lock down environment variables _by default_, this is best left up to individual projects and users. This flag is marked experimental to allow for testing and requirement discovery (e.g. env vars other than `PATH` that should be included). Closes bazelbuild#10996 Closes bazelbuild#24404. PiperOrigin-RevId: 836494750 Change-Id: Ic05e5ca47a14badb2cd23f810e775c3341ddfaa8
This PR introduces a new flag `--experimental_strict_repo_env` which stops repository rules and module extensions from inheriting the client environment (making `--repo_env=NAME` more than just an advisory notice). When enabled up to 2 environment variables will still be forwarded (unless overridden or explicitly removed via `--repo_env==VARNAME`, see #26300 for more details); - `PATH` - All platforms - `PATHEXT` - Windows See `test_execute_environment_strict_vars` in `src/test/shell/bazel/starlark_repository_test.sh` for a demonstration. Note that the behavior is different to the similarly named `--incompatible_strict_action_env`, which stops _all_ environment variables (`--action_env` affects actions with `use_default_shell_env = True`) except those specified within the defining rule. This is by design as repository rules operate in an inherently non-hermetic domain, covering roles such as integrating with the C/C++ toolchain installed on the host. It does not make sense to lock down environment variables _by default_, this is best left up to individual projects and users. This flag is marked experimental to allow for testing and requirement discovery (e.g. env vars other than `PATH` that should be included). Closes #10996 Closes #24404. PiperOrigin-RevId: 836494750 Change-Id: Ic05e5ca47a14badb2cd23f810e775c3341ddfaa8 Commit 60bc017 Co-authored-by: Jordan Mele <jordan.mele@outlook.com.au>
This PR introduces a new flag `--experimental_strict_repo_env` which stops repository rules and module extensions from inheriting the client environment (making `--repo_env=NAME` more than just an advisory notice). When enabled up to 2 environment variables will still be forwarded (unless overridden or explicitly removed via `--repo_env==VARNAME`, see bazelbuild#26300 for more details); - `PATH` - All platforms - `PATHEXT` - Windows See `test_execute_environment_strict_vars` in `src/test/shell/bazel/starlark_repository_test.sh` for a demonstration. Note that the behavior is different to the similarly named `--incompatible_strict_action_env`, which stops _all_ environment variables (`--action_env` affects actions with `use_default_shell_env = True`) except those specified within the defining rule. This is by design as repository rules operate in an inherently non-hermetic domain, covering roles such as integrating with the C/C++ toolchain installed on the host. It does not make sense to lock down environment variables _by default_, this is best left up to individual projects and users. This flag is marked experimental to allow for testing and requirement discovery (e.g. env vars other than `PATH` that should be included). Closes bazelbuild#10996 Closes bazelbuild#24404. PiperOrigin-RevId: 836494750 Change-Id: Ic05e5ca47a14badb2cd23f810e775c3341ddfaa8
|
Any chance of backporting this to 8.6.0? Given it didn't make the cut for 8.5.0? |
|
If we backport this, we should also backport #28168. |
|
Yikes. I'm surprised |
|
@bazel-io fork 8.6.0 |
This PR introduces a new flag
--experimental_strict_repo_envwhich stops repository rules and module extensions from inheriting the client environment (making--repo_env=NAMEmore than just an advisory notice).When enabled up to 2 environment variables will still be forwarded (unless overridden or explicitly removed via
--repo_env==VARNAME, see #26300 for more details);PATH- All platformsPATHEXT- WindowsSee
test_execute_environment_strict_varsinsrc/test/shell/bazel/starlark_repository_test.shfor a demonstration.Note that the behavior is different to the similarly named
--incompatible_strict_action_env, which stops all environment variables (--action_envaffects actions withuse_default_shell_env = True) except those specified within the defining rule. This is by design as repository rules operate in an inherently non-hermetic domain, covering roles such as integrating with the C/C++ toolchain installed on the host. It does not make sense to lock down environment variables by default, this is best left up to individual projects and users.This flag is marked experimental to allow for testing and requirement discovery (e.g. env vars other than
PATHthat should be included).Closes #10996