Skip to content

go/tools/builders: don't format subcommand output as []byte#2766

Merged
jayconrod merged 1 commit intobazel-contrib:masterfrom
jayconrod:fix-verbose-stderr
Dec 22, 2020
Merged

go/tools/builders: don't format subcommand output as []byte#2766
jayconrod merged 1 commit intobazel-contrib:masterfrom
jayconrod:fix-verbose-stderr

Conversation

@jayconrod
Copy link
Copy Markdown
Collaborator

Use os.Stderr.Write instead of fmt.Fprint. The latter formats the
value as a []byte, not a string.

(This was due to my own bad review comment on #2736. Sorry.)

Use os.Stderr.Write instead of fmt.Fprint. The latter formats the
value as a []byte, not a string.

(This was due to my own bad review comment on bazel-contrib#2736. Sorry.)
@google-cla google-cla bot added the cla: yes label Dec 21, 2020
@jayconrod jayconrod merged commit d6c4376 into bazel-contrib:master Dec 22, 2020
@jayconrod jayconrod deleted the fix-verbose-stderr branch December 22, 2020 18:08
jayconrod pushed a commit that referenced this pull request Dec 23, 2020
Use os.Stderr.Write instead of fmt.Fprint. The latter formats the
value as a []byte, not a string.

(This was due to my own bad review comment on #2736. Sorry.)
jayconrod pushed a commit that referenced this pull request Dec 23, 2020
Use os.Stderr.Write instead of fmt.Fprint. The latter formats the
value as a []byte, not a string.

(This was due to my own bad review comment on #2736. Sorry.)
yushan26 pushed a commit to yushan26/rules_go that referenced this pull request Jun 16, 2025
These are just bugfixes to already merged code:
* Fix nested bracket parsing in PEP508 marker parser.
* Fix the sys_platform constants, which I noticed in bazel-contrib#2629 but they got
  also pointed out in bazel-contrib#2766.
* Port some of python tests for requirement parsing and improve the
  implementation. Those tests will be removed in bazel-contrib#2629.
* Move the platform related code to a separate file.
* Rename `pep508_req.bzl` to `pep508_requirement.bzl` to follow the
  convention.


All of the bug fixes have added tests.

Work towards bazel-contrib#2423.
yushan26 pushed a commit to yushan26/rules_go that referenced this pull request Jun 16, 2025
This change addresses a bug where `pip.parse` selects the wrong
requirement entry when multiple extras are listed with platform-specific
markers.

#### 🔍 Problem:
In a `requirements.txt` generated by tools like `uv` or `poetry`, it's
valid to have multiple entries for the same package, each with different
extras and `sys_platform` markers, for example:

```ini
optimum[onnxruntime]==1.17.1 ; sys_platform == 'darwin'
optimum[onnxruntime-gpu]==1.17.1 ; sys_platform == 'linux'
```

The current implementation in
[`[parse_requirements.bzl](https://github.com/bazel-contrib/rules_python/blob/032f6aa738a673b13b605dabf55465c6fc1a56eb/python/private/pypi/parse_requirements.bzl#L114-L126)`](https://github.com/bazel-contrib/rules_python/blob/032f6aa738a673b13b605dabf55465c6fc1a56eb/python/private/pypi/parse_requirements.bzl#L114-L126)
uses a sort-by-length heuristic to select the “best” requirement when
there are multiple entries with the same base name. This works well in
legacy `requirements.txt` files where:
```
my_dep
my_dep[foo]
my_dep[foo,bar]
```
...would indicate an intent to select the **most specific subset of
extras** (i.e. the longest name).

However, this heuristic **breaks** in the presence of **platform
markers**, where extras are **not subsets**, but distinct variants. In
the example above, Bazel mistakenly selects `optimum[onnxruntime-gpu]`
on macOS because it's a longer match, even though it is guarded by a
Linux-only marker.

#### ✅ Fix:
This PR modifies the behavior to:
1. **Add the requirement marker** as part of the sorting key.
2. **Then apply the longest-match logic** to drop duplicate requirements
with different extras but the same markers.

This ensures that only applicable requirements are considered during
resolution, preserving correctness in multi-platform environments.

#### 🧪 Before:
On macOS, the following entry is incorrectly selected:
```
optimum[onnxruntime-gpu]==1.17.1 ; sys_platform == 'linux'
```

#### ✅ After:
Correct entry is selected:
```
optimum[onnxruntime]==1.17.1 ; sys_platform == 'darwin'
```

close bazel-contrib/rules_python#2690

---------

Co-authored-by: Ignas Anikevicius <240938+aignas@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant