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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ END_UNRELEASED_TEMPLATE
also retrieved from the URL as opposed to only the `--hash` parameter. Fixes
[#2363](https://github.com/bazel-contrib/rules_python/issues/2363).
* (pypi) `whl_library` now infers file names from its `urls` attribute correctly.
* (py_test, py_binary) Allow external files to be used for main

{#v0-0-0-added}
### Added
Expand Down
6 changes: 3 additions & 3 deletions internal_dev_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ def rules_python_internal_deps():
http_archive(
name = "rules_pkg",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/0.7.0/rules_pkg-0.7.0.tar.gz",
"https://mirror.bazel.build/github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz",
"https://github.com/bazelbuild/rules_pkg/releases/download/1.0.1/rules_pkg-1.0.1.tar.gz",
],
sha256 = "8a298e832762eda1830597d64fe7db58178aa84cd5926d76d5b744d6558941c2",
sha256 = "d20c951960ed77cb7b341c2a59488534e494d5ad1d30c4818c736d57772a9fef",
)

http_archive(
Expand Down
8 changes: 6 additions & 2 deletions python/private/python_bootstrap_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,12 @@ def Main():
# The magic string percent-main-percent is replaced with the runfiles-relative
# filename of the main file of the Python binary in BazelPythonSemantics.java.
main_rel_path = '%main%'
if IsWindows():
main_rel_path = main_rel_path.replace('/', os.sep)
# NOTE: We call normpath for two reasons:
# 1. Transform Bazel `foo/bar` to Windows `foo\bar`
# 2. Transform `_main/../foo/main.py` to simply `foo/main.py`, which
# matters if `_main` doesn't exist (which can occur if a binary
# is packaged and needs no artifacts from the main repo)
main_rel_path = os.path.normpath(main_rel_path)

if IsRunningFromZip():
module_space = CreateModuleSpace()
Expand Down
24 changes: 22 additions & 2 deletions tests/bootstrap_impls/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
load("@rules_shell//shell:sh_test.bzl", "sh_test")

# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,6 +11,8 @@ load("@rules_shell//shell:sh_test.bzl", "sh_test")
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
load("@rules_pkg//pkg:tar.bzl", "pkg_tar")
load("@rules_shell//shell:sh_test.bzl", "sh_test")
load("//tests/support:py_reconfig.bzl", "py_reconfig_binary", "py_reconfig_test")
load("//tests/support:sh_py_run_test.bzl", "sh_py_run_test")
load("//tests/support:support.bzl", "SUPPORTS_BOOTSTRAP_SCRIPT")
Expand Down Expand Up @@ -158,4 +158,24 @@ py_reconfig_test(
target_compatible_with = SUPPORTS_BOOTSTRAP_SCRIPT,
)

pkg_tar(
name = "external_binary",
testonly = True,
srcs = ["@other//:external_main"],
include_runfiles = True,
tags = ["manual"], # Don't build as part of wildcards
)

sh_test(
name = "external_binary_test",
srcs = ["external_binary_test.sh"],
data = [":external_binary"],
# For now, skip this test on Windows because it fails for reasons
# other than the code path being tested.
target_compatible_with = select({
"@platforms//os:windows": ["@platforms//:incompatible"],
"//conditions:default": [],
}),
)

relative_path_test_suite(name = "relative_path_tests")
9 changes: 9 additions & 0 deletions tests/bootstrap_impls/external_binary_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash
set -euxo pipefail

tmpdir="${TEST_TMPDIR}/external_binary"
mkdir -p "${tmpdir}"
tar xf "tests/bootstrap_impls/external_binary.tar" -C "${tmpdir}"
test -x "${tmpdir}/external_main"
output="$("${tmpdir}/external_main")"
test "$output" = "token"
14 changes: 14 additions & 0 deletions tests/modules/other/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@rules_python//tests/support:py_reconfig.bzl", "py_reconfig_binary")

package(
default_visibility = ["//visibility:public"],
)

py_reconfig_binary(
name = "external_main",
srcs = [":external_main.py"],
# We're testing a system_python specific code path,
# so force using that bootstrap
bootstrap_impl = "system_python",
main = "external_main.py",
)
1 change: 1 addition & 0 deletions tests/modules/other/external_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
print("token")