Skip to content

fix: Normalize main script path in Python bootstrap #2925

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

Merged
merged 8 commits into from
May 23, 2025

Conversation

mering
Copy link
Contributor

@mering mering commented May 22, 2025

Use os.path.normpath() to resolve _main/../repo/ to repo/ and convert forward
slashes to backward slashes on Windows.

This fixes an issue where _main doesn't exist within runfiles and in turn the
later assertion that the path to main exists fails (~L542). This happens,
for example, when packaging a py_binary from a foreign repo into a tar/container.

@mering mering requested review from rickeylev and aignas as code owners May 22, 2025 11:58
@rickeylev
Copy link
Collaborator

Mostly LGTM. Mostly just needs a test. IIUC, the basic target def is this?

py_binary(
  srcs = ["@repo//:foo.py"],
  main = ["@repo//:foo.py"]
)

?

For the test:

  • Add a py_reconfig_test to tests/bootstrap_impls.
  • Add the external files to tests/modules/other (iirc, this is available as @other in our dev setup).

Does this problem exist in the bootstrap=script impl, too?

Use os.path.normpath() to resolve "_main/../repo" to "repo"
and convert forward slashes to backward slashes on Windows.

This fixes an issue where "_main" doesn't exist and in turn
"assert os.path.exists("_main/../repo")" fails. This happens
for example when packaging a py_binary from a foreign repo
into a tar/container.
@mering mering force-pushed the fix-python-bootstrap branch from 6cf8121 to edba3a2 Compare May 23, 2025 17:16
@mering mering force-pushed the fix-python-bootstrap branch 3 times, most recently from 95a147b to 2ec6eab Compare May 23, 2025 18:02
@mering mering force-pushed the fix-python-bootstrap branch from 2ec6eab to 88e2e36 Compare May 23, 2025 18:17
This is required to correctly populate runfiles in pkg_tar with include_runfiles=True.
@mering mering force-pushed the fix-python-bootstrap branch 2 times, most recently from 3cbb2f2 to 312ec4d Compare May 23, 2025 18:57
@mering mering force-pushed the fix-python-bootstrap branch from 312ec4d to ac1c7db Compare May 23, 2025 18:58
@rickeylev
Copy link
Collaborator

LGTM. Thanks for slogging through that test @mering !

Recapping our chat, for posterity:

We'll skip testing this on windows. The error is about the "launcher" being unable to find the python.exe file, which is probably just an artifact of the sh_test/tar/extract layout of files and/or env variable state. Something to look into another time.

I'm fairly certain this bug doesn't exist in the script implementation because it uses runfiles-root relative paths instead of main-repo-relative paths. It'd be nice to have a test for that regardless, but that doesn't need to go into this PR.

@rickeylev rickeylev enabled auto-merge May 23, 2025 20:07
@rickeylev rickeylev added this pull request to the merge queue May 23, 2025
Merged via the queue into bazel-contrib:main with commit 2036571 May 23, 2025
2 checks passed
@mering mering deleted the fix-python-bootstrap branch May 23, 2025 20:28
@mering mering force-pushed the fix-python-bootstrap branch from 88e2e36 to 2abee57 Compare May 23, 2025 20:33
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.

2 participants