Skip to content
Merged
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
20 changes: 18 additions & 2 deletions misc/bazel/3rdparty/patch_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,31 @@
import re
import pathlib

label_re = re.compile(r'"@(vendor.*)//:(.+)-([\d.]+)"')
# Problem number 1:
# https://github.com/bazelbuild/rules_rust/issues/3255
# `crates_vendor` generates broken labels in `defs.bzl`: instead of
# "anyhow": Label("@vendor__anyhow-1.0.44//:anyhow")
# it produces
# "anyhow": Label("@vendor//:anyhow-1.0.44")
# which results in: ERROR: no such package '@@[unknown repo 'vendor' requested from @@]//'
#
# Problem number 2:
# Semver versions can contain `+` for build metadata (e.g., `0.9.11+spec-1.1.0`).
# Bazel repo names use `-` instead of `+`, so `vendor_ts__toml-0.9.11+spec-1.1.0`
# becomes `vendor_ts__toml-0.9.11-spec-1.1.0`. The generated labels reference the
# `+` version which doesn't exist, causing:
# ERROR: no such package '@@[unknown repo 'vendor_ts__toml-0.9.11+spec-1.1.0'
# requested from @@ (did you mean 'vendor_ts__toml-0.9.11-spec-1.1.0'?)]//

label_re = re.compile(r'"@(vendor.*)//:([^+]+)-([\d.]+(?:\+.*)?)"')
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The regex pattern ([^+]+) will incorrectly capture package names for versions with + metadata. For input "@vendor//:toml-0.9.11+spec-1.1.0", the pattern will match with group 2 = toml-0.9 instead of the correct package name toml. This happens because [^+]+ greedily matches up to the +, then backtracks to find a - that's followed by [\d.]+, resulting in capturing part of the version number as part of the package name. The correct pattern should be (.+) instead of ([^+]+), as .+ with the anchoring pattern at the end will correctly match just the package name.

Suggested change
label_re = re.compile(r'"@(vendor.*)//:([^+]+)-([\d.]+(?:\+.*)?)"')
label_re = re.compile(r'"@(vendor.*)//:(.+)-([\d.]+(?:\+.*)?)"')

Copilot uses AI. Check for mistakes.

file = pathlib.Path(sys.argv[1])
temp = file.with_suffix(f'{file.suffix}.tmp')


with open(file) as input, open(temp, "w") as output:
for line in input:
line = label_re.sub(lambda m: f'"@{m[1]}__{m[2]}-{m[3]}//:{m[2].replace("-", "_")}"', line)
line = label_re.sub(lambda m: f'"@{m[1]}__{m[2]}-{m[3].replace("+", "-")}//:{m[2].replace("-", "_")}"', line)
Comment on lines +21 to +29
Copy link

Copilot AI Feb 6, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The patch_defs.py script lacks test coverage. Given the complexity of the regex pattern matching and the critical nature of this script (it patches generated Bazel dependency files), tests should be added to verify correct behavior for various input scenarios including: 1) basic package-version format, 2) packages with hyphens in names (e.g., tree-sitter), 3) versions with build metadata (e.g., 0.9.11+spec-1.1.0), and 4) combinations thereof. The repository has comprehensive Python testing infrastructure in place (see misc/codegen/test/ and python/extractor/tests/), so tests should follow those patterns.

Copilot uses AI. Check for mistakes.
output.write(line)

temp.rename(file)
Loading