Skip to content

fix(toolchains): skip install_name_tool when the libpython dylib id is already @rpath - #4528

Open
TrapsterDK wants to merge 5 commits into
bazel-contrib:mainfrom
TrapsterDK:fix/skip-install-name-tool
Open

TrapsterDK wants to merge 5 commits into
bazel-contrib:mainfrom
TrapsterDK:fix/skip-install-name-tool

Conversation

@TrapsterDK

@TrapsterDK TrapsterDK commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #4527

On macOS, fetching a python-build-standalone runtime always ran
install_name_tool -id @rpath/<dylib> on libpython*.dylib. With a PATH
that has no install_name_tool (for example a hermetic
--repo_env=PATH=...), the repository rule failed in
python_repository.FixUpDyldIdPath.

To fix, the fix-up moves into fix_up_dylib_id. If install_name_tool is not
on PATH, it logs a warning and leaves the dylib id unchanged instead of
failing the fetch. python-build-standalone sets the id to @rpath/<dylib>
itself since astral-sh/python-build-standalone#914, so recent runtimes need no
rewrite.

Tested with new unit tests in //tests/python_repository that use the mock
rctx for both cases (tool present and tool missing). The mock rctx gets a
no-op watch, which which_unchecked calls.

AI disclosure: this change was written with Claude. A human (TrapsterDK) is the author and reviews and posts all replies on this PR; no agent monitors or answers comments automatically.

Assisted by Claude.

Copilot AI balanced review requested due to automatic review settings November 18, 2026 11:53
@TrapsterDK

Copy link
Copy Markdown
Contributor Author

AI disclosure: this change was written with Claude. A human (TrapsterDK) is the author and reviews and posts all replies on this PR; no agent monitors or answers comments automatically.

Assisted by Claude.

Assisted-by: Claude

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are reported for the change reducing unnecessary macOS runtime-fetching tool dependencies.

No open issues 🎉

What changed in this PR

This PR addresses #4527 by avoiding an unnecessary install_name_tool dependency when fetching macOS Python runtimes through python_repository.

Changes:

  • Skip dylib ID repair when the binary contains the expected @rpath/<dylib> string, retaining the existing repair otherwise.
  • Add a release note describing the macOS fetching fix.
File Description
python/​private/​python_repository.bzl Guard the install_name_tool lookup and invocation with a dylib content check.
news/​4527.fixed.md Document the reduced host-tool requirement for macOS runtime fetching.

Review effort: Balanced

…l_name_tool

The skip check searched the whole dylib for `@rpath/<dylib>`, so the same
path in another load command or string would skip the id fix-up and leave
the build-host id in place.

Check that the path is the name of the LC_ID_DYLIB load command of a thin
64-bit Mach-O file instead, without running `otool`, which may be missing
from PATH like `install_name_tool`. Anything else still gets the id
rewritten.

* Adds unit tests for the check.
* Wraps the news entry and links the issue.

Assisted-by: Claude
@TrapsterDK TrapsterDK changed the title fix: skip install_name_tool when the libpython dylib id is already @rpath fix(toolchains): skip install_name_tool when the libpython dylib id is already @rpath Nov 18, 2026
The skip check still searched the whole dylib for `@rpath/<dylib>`, so a
well-formed id command in the data after the load commands could skip the
id fix-up.

Read `ncmds` and `sizeofcmds` from the mach_header_64 and walk the load
commands by `cmdsize`, staying inside `sizeofcmds`; the first LC_ID_DYLIB
decides. Fields are decoded with a byte table since `rctx.read` returns
Latin-1 characters and Starlark has no `ord`. Universal and 32-bit binaries,
and malformed load commands, still get the id rewritten.

* Adds unit tests for an id command outside the load command area, a
  truncated file, a 32-bit binary, and sizes with bytes >= 0x80.
* Adds a dev repo and py_test that check the same file after `rctx.read`.

Assisted-by: Claude
@aignas

aignas commented Nov 20, 2026

Copy link
Copy Markdown
Collaborator

even when the dylib already had that id

What are the situations where this is true? Could we just not run the command if it is not found? This adds complex logic that I don't have confidence in maintaining (sure slopbot can keep it, but I want to be able to understand it).

An easier fix would be to just skip using the install_name_tool if it does not exist?

Per review, drop the Mach-O parser. If `install_name_tool` is not on PATH,
leave the libpython dylib id unchanged and log a warning instead of failing
the fetch. python-build-standalone sets the id to `@rpath/<dylib>` itself
since astral-sh/python-build-standalone#914.

* Moves the fix-up into `fix_up_dylib_id` and tests it with the mock rctx
  for both cases.
* Adds a no-op `watch` to the mock rctx, which `which_unchecked` calls.

Assisted-by: Claude
@TrapsterDK

Copy link
Copy Markdown
Contributor Author

Agreed, that is simpler. python-build-standalone sets the id to @rpath/<dylib> itself since astral-sh/python-build-standalone#914, which is the case where the rewrite is not needed.

933ab85 drops the Mach-O parser and its tests. If install_name_tool is not on PATH, fix_up_dylib_id now logs a warning and skips the rewrite. Unit tests cover both cases with the mock rctx. Ran bazel test //tests/python_repository/... //tests/repo_utils/...: 9/9 pass. I updated the PR description to match.

Assisted by Claude.

This branch has not been deployed

No deployments
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.

python_repository runs install_name_tool even when the libpython dylib id is already @rpath

3 participants