bazel-contrib / bazel-contrib/rules_python

PYTHONHOME gets incorrectly set by pip extension on windows

未关闭
#3,294 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
type: bug type: pip
主要语言
Starlark
星标
688
派生
721
平均合并
15 小时 7 分钟
30 天内合并 PR
76

描述

# 🐞 bug report

### Affected Rule

This affects the pip module extension

### Is this a regression?

Not that I'm aware of, but I assume it's worked on windows at some point

### Description

When using the pip extension on windows, I get crashes like:
```
Fatal Python error: init_fs_encoding: failed to get the Python codec of the filesystem encoding
Python runtime state: core initialized
ModuleNotFoundError: No module named 'encodings'

Current thread 0x00000c40 (most recent call first):

```

I traced this back to the `PYTHONHOME` environment variable, which is being set from the stdout of a repository_ctx.execute call. Instead of just containing the result of the command, it also appears to contain the prompt and the command itself, corrupting this load-bearing environment variable. An example of what I see:
```
Python path configuration:
PYTHONHOME = '\x0d\x0aC:\tmp\3hjz7nbt\external\rules_python~~pip~protobuf_pip_deps_311_setuptools>C:/hostedtoolcache/windows/Python/3.9.13/x64/python3.exe -B -I -c "import sys; print(f\'***sys.prefix***\', end=\'\')" \x0d\x0aC:\hostedtoolcache\windows\Python\3.9.13\x64'
```

Commenting out the line in rules_python that sets PYTHONHOME does work around this problem and I'm able to successfully build. But it's not clear to me what other problems that might cause, and it's probably not the right solution

## 🔬 Minimal Reproduction

Any bazel repository using the pip extension should reproduce this on windows. For example, in https://github.com/protocolbuffers/protobuf, you can run:
`bazel test //python/google/protobuf/internal/numpy:numpy_test`

Note: you'll have to edit `python/requirements.txt` to not have any `<` symbols due to a separate bug I'll be filing another issue about.

## 🔥 Exception or Error

See description above. The error is from a corrupted python environment and not particularly helpful on its own

## 🌍 Your Environment

**Operating System:** Windows
**Output of `bazel version`:** Reproduces with Bazel 7 and 8
**Rules_python version:** 1.6.0

贡献指南

打开贡献指南

调研方向

Start with the pip extension's repository_ctx.execute call that derives PYTHONHOME, then reproduce on Windows using the protobuf target //python/google/protobuf/internal/numpy:numpy_test. Compare the command output with the intended Python path and verify that PYTHONHOME contains only the path, without the prompt or command text; confirm the reproduction succeeds with the affected rules_python versions.

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
build-system
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。