Skip to content

Commit 89d850a

Browse files
authored
fix: _which_unchecked: don't watch PATH if binary exists. (#2552)
Currently, the _which_unchecked helper unconditionally watches the `PATH` env var via repository_ctx.getenv. getenv is documented https://bazel.build/rules/lib/builtins/repository_ctx#getenv: > any change to the value of the variable named by name will cause this repository to be re-fetched. Thus, any change to `PATH` will cause any repository rule that transitively calls _which_unchecked to be re-fetched. This includes python_repository and whl_library. There are reasonable development workflows that modify `PATH`. In particular, when git runs a hook, it adds the value of `GIT_EXEC_PATH` to `PATH` before invoking the hook. If the hook invokes bazel (for example, a pre-commit hook running `bazel build ...`), it will cause the Python repository rules to be re-fetched. This commit lowers the repository_ctx.getenv("PATH") call to its only use site in _which_unchecked, which happens to be a failure case (when the binary is not found). This allows the success case to not watch `PATH`, and therefore not to re-fetch the repository rule when it changes. Fixes #2551.
1 parent 4e95a60 commit 89d850a

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ Unreleased changes template.
119119
change. Fixes [#2468](https://github.com/bazelbuild/rules_python/issues/2468).
120120
+ (gazelle) Gazelle no longer ignores `setup.py` files by default. To restore
121121
this behavior, apply the `# gazelle:python_ignore_files setup.py` directive.
122+
* Don't re-fetch whl_library, python_repository, etc. repository rules
123+
whenever `PATH` changes. Fixes
124+
[#2551](https://github.com/bazelbuild/rules_python/issues/2551).
122125

123126
[pep-695]: https://peps.python.org/pep-0695/
124127

python/private/repo_utils.bzl

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ def _which_checked(mrctx, binary_name):
256256
def _which_unchecked(mrctx, binary_name):
257257
"""Tests to see if a binary exists.
258258
259-
This is also watch the `PATH` environment variable.
259+
Watches the `PATH` environment variable if the binary doesn't exist.
260260
261261
Args:
262262
binary_name: name of the binary to find.
@@ -268,12 +268,12 @@ def _which_unchecked(mrctx, binary_name):
268268
* `describe_failure`: `Callable | None`; takes no args. If the
269269
binary couldn't be found, provides a detailed error description.
270270
"""
271-
path = _getenv(mrctx, "PATH", "")
272271
binary = mrctx.which(binary_name)
273272
if binary:
274273
_watch(mrctx, binary)
275274
describe_failure = None
276275
else:
276+
path = _getenv(mrctx, "PATH", "")
277277
describe_failure = lambda: _which_describe_failure(binary_name, path)
278278

279279
return struct(

0 commit comments

Comments
 (0)