py_binary system_python bootstrap can import target outputs instead of stdlib modules
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 74/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 静か
- 技術スタック
- python
- 領域
- build-system
調査の方向性
python/private/python_bootstrap_template.txt と、.bazelrc 設定によって選択される system_python bootstrap から始めます。生成された shutil.py と types.py の出力を使って bazel run //shadow_pkg:hello で再現し、その後 PYTHONSAFEPATH=1 と比較します。bootstrap が target-generated modules をインポートしなくなり、コマンドが "hello from main" を出力すれば完了です。
索引モデルが issue の本文から書いたものです。
説明
🐞 bug report
Affected Rule
The issue is caused by the rule:py_binary when using the Python stage1 bootstrap selected by
--@rules_python//python/config_settings:bootstrap_impl=system_python.
The problem is in python/private/python_bootstrap_template.txt: stage1 imports
stdlib modules before removing the launcher's directory from sys.path[0].
Is this a regression?
Unknown. Reproduced with rules_python `main` at2.1.0-rc0-11-g1a15c3b4.
Description
A clear and concise description of the problem...The generated Python stage1 bootstrap imports stdlib modules while Python still
has the launcher directory at sys.path[0]. If the target package has generated
outputs next to the launcher with stdlib module names, those target files can be
imported instead of the Python standard library before rules_python finishes
bootstrap setup.
In the minimal reproduction below, the target generates shutil.py and
types.py next to the launcher in bazel-bin/shadow_pkg/. When stage1 executes,
its import shutil resolves to the generated target output instead of stdlib
shutil.
This is similar in root cause to
https://github.com/bazel-contrib/rules_python/issues/382, but this report is
specifically about stdlib shadowing inside the generated stage1 bootstrap before
the user's program runs.
🔬 Minimal Reproduction
Minimal local reproduction:
/av/develop/tmp/rules_python_reproductions/stdlib_shadowing
The repro uses local_path_override to point at:
/av/develop/tmp/rules_python
Relevant files:
# .bazelrc
build --@rules_python//python/config_settings:bootstrap_impl=system_python
# shadow_pkg/BUILD.bazel
load("@rules_python//python:py_binary.bzl", "py_binary")
genrule(
name = "generated_stdlib_modules",
outs = [
"shutil.py",
"types.py",
],
cmd = """
cat > $(@D)/shutil.py <<'PY'
raise RuntimeError('target output shutil.py shadowed the stdlib shutil module')
PY
cat > $(@D)/types.py <<'PY'
raise RuntimeError('target output types.py shadowed the stdlib types module')
PY
""",
)
py_binary(
name = "hello",
srcs = [
"main.py",
":generated_stdlib_modules",
],
main = "main.py",
)
# shadow_pkg/main.py
print("hello from main")
Run:
bazel run //shadow_pkg:hello
Expected result:
hello from main
Actual result:
The bootstrap fails before main.py runs because it imports generated
bazel-bin/shadow_pkg/shutil.py instead of stdlib shutil.
🔥 Exception or Error
Traceback (most recent call last):
File ".../execroot/_main/bazel-out/k8-fastbuild/bin/shadow_pkg/hello", line 12, in <module>
import shutil
File ".../execroot/_main/bazel-out/k8-fastbuild/bin/shadow_pkg/shutil.py", line 1, in <module>
raise RuntimeError('target output shutil.py shadowed the stdlib shutil module')
RuntimeError: target output shutil.py shadowed the stdlib shutil module
🌍 Your Environment
Operating System:
Linux gkolobkov 6.8.0-124-generic #124-Ubuntu SMP PREEMPT_DYNAMIC Tue May 26 13:00:45 UTC 2026 x86_64 x86_64 x86_64 GNU/Linux
Output of bazel version:
Bazelisk version: v1.27.0
Build label: 9.1.1
Build target: @@//src/main/java/com/google/devtools/build/lib/bazel:BazelServer
Build time: Wed Jun 03 15:41:13 2026 (1780501273)
Build timestamp: 1780501273
Build timestamp as int: 1780501273
Rules_python version:
rules_python main at 2.1.0-rc0-11-g1a15c3b4
Anything else relevant?
The issue is avoided if Python safe-path mode is enabled:
PYTHONSAFEPATH=1 bazel run //shadow_pkg:hello
That succeeds and prints:
hello from main
This suggests the stage1 bootstrap should remove the unsafe sys.path[0] entry
before importing any stdlib modules, or otherwise ensure safe-path behavior for
the stage1 process.
- 主要言語
- Starlark
- スター
- 690
- フォーク
- 722
- 平均マージ
- 1日 55分
- マージ済み PR(30日)
- 38
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
bazel-contrib/rules_python のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
bazel-contrib/rules_python#4179 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
bazel-contrib/rules_python#4164 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
bazel-contrib/rules_python#3821 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
bazel-contrib/rules_python#4181 ·
-
Release 2.4.0 オープンtype: release
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
bazel-contrib/rules_python#4175 · コメント 3 件 ·
bazel-contrib/rules_python の issue をすべて見る
似ている issue
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
typelevel/sbt-typelevel#929 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
openSUSE/python-rpm-macros#219 ·
-
HMR stops working オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 75/100
-
難易度 1/5 1時間未満 初心者へのやさしさ 90/100
Qiskit/mcp-servers#221 ·