Skip to content

Commit 38830dd

Browse files
Bazel: fix Rust deps patching for semver build metadata
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 parent 2c05624 commit 38830dd

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

misc/bazel/3rdparty/patch_defs.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,31 @@
22
import re
33
import pathlib
44

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

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

1026

1127
with open(file) as input, open(temp, "w") as output:
1228
for line in input:
13-
line = label_re.sub(lambda m: f'"@{m[1]}__{m[2]}-{m[3]}//:{m[2].replace("-", "_")}"', line)
29+
line = label_re.sub(lambda m: f'"@{m[1]}__{m[2]}-{m[3].replace("+", "-")}//:{m[2].replace("-", "_")}"', line)
1430
output.write(line)
1531

1632
temp.rename(file)

0 commit comments

Comments
 (0)