Skip to content

Bazel: fix Rust deps patching for semver build metadata#21287

Merged
redsun82 merged 1 commit intomainfrom
redsun82/fix-rust-deps-patching
Feb 6, 2026
Merged

Bazel: fix Rust deps patching for semver build metadata#21287
redsun82 merged 1 commit intomainfrom
redsun82/fix-rust-deps-patching

Conversation

@redsun82
Copy link
Contributor

@redsun82 redsun82 commented Feb 6, 2026

Handle crate versions containing + build metadata (e.g., 0.9.11+spec-1.1.0). Bazel repo names use - instead of +, so the generated labels need patching to reference the correct repo name.

Also adds documentation for both patching issues handled by patch_defs.py:

  1. Problem 1 (bazelbuild/rules_rust#3255): crates_vendor generates broken labels in defs.bzl - uses @vendor//:pkg-version instead of @vendor__pkg-version//:pkg

  2. Problem 2: Semver versions with + build metadata cause repo name mismatches - Bazel uses - in repo names, but generated labels reference the + version

Handle crate versions containing `+` build metadata (e.g., `0.9.11+spec-1.1.0`).
Bazel repo names use `-` instead of `+`, so the generated labels need patching
to reference the correct repo name.

Also adds documentation for both patching issues handled by patch_defs.py.
@redsun82 redsun82 requested a review from a team as a code owner February 6, 2026 13:59
Copilot AI review requested due to automatic review settings February 6, 2026 13:59
@redsun82 redsun82 requested a review from paldepind February 6, 2026 14:01
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR attempts to fix Rust dependency patching to handle semver build metadata (versions with +). It adds documentation for two issues: 1) the upstream bazelbuild/rules_rust#3255 issue where crates_vendor generates incorrect label formats, and 2) a new issue where semver versions with + build metadata don't match Bazel's repo naming (which uses - instead of +).

Changes:

  • Added comprehensive documentation explaining both patching problems
  • Updated regex pattern to match versions containing + build metadata
  • Modified version replacement logic to convert + to - in repo names

# 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.
Comment on lines +21 to +29
label_re = re.compile(r'"@(vendor.*)//:([^+]+)-([\d.]+(?:\+.*)?)"')

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)
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.
Copy link
Contributor

@paldepind paldepind left a comment

Choose a reason for hiding this comment

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

Thanks! This fixes the problem I was experiencing.

@redsun82 redsun82 merged commit 48db24d into main Feb 6, 2026
27 checks passed
@redsun82 redsun82 deleted the redsun82/fix-rust-deps-patching branch February 6, 2026 16:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants