Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: _which_unchecked: don't watch PATH if binary exists. #2552

Merged
merged 1 commit into from
Jan 9, 2025

Conversation

Ubehebe
Copy link
Contributor

@Ubehebe Ubehebe commented Jan 8, 2025

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.

@aignas
Copy link
Collaborator

aignas commented Jan 8, 2025

This makes sense to me, I am happy to stamp it if you could add a more descriptive commit message about before and after behaviour and a Changelog.md item since having a self contained git history with the motivation for changes is always really god for digging the code later.

@aignas
Copy link
Collaborator

aignas commented Jan 8, 2025

Should we wait for this PR to release 1.1.0? @rickeylev, WDYT?

Fixes bazelbuild#2551.

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.
CHANGELOG.md Show resolved Hide resolved
@Ubehebe
Copy link
Contributor Author

Ubehebe commented Jan 9, 2025

add a more descriptive commit message about before and after behaviour and a Changelog.md item

Done. I added the changelog entry under 1.1.0 (feel free to move).

@rickeylev rickeylev added this pull request to the merge queue Jan 9, 2025
@rickeylev
Copy link
Collaborator

I'll include it in 1.1

Merged via the queue into bazelbuild:main with commit 89d850a Jan 9, 2025
3 checks passed
@aignas aignas mentioned this pull request Jan 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Invoking bazel with a different PATH re-fetches pip_repository
3 participants