Skip to content

Commit e7d37ac

Browse files
committed
Fixed crate_universe when using Rust >= 1.85.0
1 parent 5226882 commit e7d37ac

File tree

11 files changed

+165
-26
lines changed

11 files changed

+165
-26
lines changed

crate_universe/3rdparty/crates/BUILD.crates-index-3.5.0.bazel

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_universe/3rdparty/crates/BUILD.rustc-stable-hash-0.1.1.bazel

Lines changed: 83 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_universe/3rdparty/crates/defs.bzl

Lines changed: 15 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_universe/Cargo.lock

Lines changed: 8 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crate_universe/Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ cargo-lock = "10.0.1"
6969
cargo-platform = "0.1.9"
7070
cfg-expr = "0.17.2"
7171
clap = { version = "4.5.26", features = ["derive", "env"] }
72-
crates-index = { version = "3.5.0", default-features = false, features = [
72+
# crates-index = { version = "3.5.0", default-features = false, features = [
73+
# "git",
74+
# ] }
75+
crates-index = { git = "https://github.com/UebelAndre/rust-crates-index.git", rev = "a77d2d0981aaef368e10406912febbbdad4b83d7", default-features = false, features = [
7376
"git",
7477
] }
7578
hex = "0.4.3"

crate_universe/src/metadata.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ impl LockGenerator {
220220

221221
// Ensure the Cargo cache is up to date to simulate the behavior
222222
// of having just generated a new one
223+
tracing::debug!("Fetching crates for {}", manifest_path);
223224
let output = self
224225
.cargo_bin
225226
.command()?
@@ -230,6 +231,7 @@ impl LockGenerator {
230231
.arg("fetch")
231232
.arg("--manifest-path")
232233
.arg(manifest_path.as_std_path())
234+
.arg("--verbose")
233235
.output()
234236
.context(format!(
235237
"Error running cargo to fetch crates '{}'",
@@ -244,6 +246,14 @@ impl LockGenerator {
244246
output.status
245247
))
246248
}
249+
tracing::trace!(
250+
"Cargo fetch stderr:\n{}",
251+
String::from_utf8_lossy(&output.stderr)
252+
);
253+
tracing::trace!(
254+
"Cargo fetch stdout:\n{}",
255+
String::from_utf8_lossy(&output.stdout)
256+
);
247257
} else {
248258
debug!("Generating new lockfile");
249259
// Simply invoke `cargo generate-lockfile`

crate_universe/src/metadata/cargo_bin.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,18 @@ impl Cargo {
117117
bail!("Couldn't parse cargo version");
118118
}
119119

120+
/// Determine if Cargo is on a version which uses new hashing behavior
121+
/// introduced in Rust 1.86.0. For details see <https://github.com/frewsxcv/rust-crates-index/issues/182>
122+
pub(crate) fn uses_stable_registry_hash(&self) -> Result<bool> {
123+
let full_version = self.full_version()?;
124+
let version_str = full_version.split(' ').nth(1);
125+
if let Some(version_str) = version_str {
126+
let version = Version::parse(version_str).context("Failed to parse cargo version")?;
127+
return Ok(version.major >= 1 && version.minor >= 85);
128+
}
129+
bail!("Couldn't parse cargo version");
130+
}
131+
120132
fn env(&self) -> Result<BTreeMap<String, OsString>> {
121133
let mut map = BTreeMap::new();
122134

crate_universe/src/splicing.rs

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,12 @@ impl WorkspaceMetadata {
307307
}
308308
};
309309

310+
let crate_index_hash_kind = if cargo.uses_stable_registry_hash()? {
311+
crates_index::HashKind::Stable
312+
} else {
313+
crates_index::HashKind::Legacy
314+
};
315+
310316
// Load each index for easy access
311317
let crate_indexes = index_urls
312318
.into_iter()
@@ -320,20 +326,27 @@ impl WorkspaceMetadata {
320326
let index = if cargo.use_sparse_registries_for_crates_io()?
321327
&& index_url == utils::CRATES_IO_INDEX_URL
322328
{
323-
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(
329+
CrateIndexLookup::Http(crates_index::SparseIndex::from_url_with_hash_kind(
324330
"sparse+https://index.crates.io/",
331+
&crate_index_hash_kind,
325332
)?)
326333
} else if index_url.starts_with("sparse+") {
327-
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(index_url)?)
334+
CrateIndexLookup::Http(crates_index::SparseIndex::from_url_with_hash_kind(
335+
index_url,
336+
&crate_index_hash_kind,
337+
)?)
328338
} else {
329339
match source_kind {
330340
SourceKind::Registry => {
331341
let index = {
332342
// Load the index for the current url
333-
let index = crates_index::GitIndex::from_url(index_url)
334-
.with_context(|| {
335-
format!("Failed to load index for url: {index_url}")
336-
})?;
343+
let index = crates_index::GitIndex::from_url_with_hash_kind(
344+
index_url,
345+
&crate_index_hash_kind,
346+
)
347+
.with_context(|| {
348+
format!("Failed to load index for url: {index_url}")
349+
})?;
337350

338351
// Ensure each index has a valid index config
339352
index.index_config().with_context(|| {
@@ -344,11 +357,12 @@ impl WorkspaceMetadata {
344357
};
345358
CrateIndexLookup::Git(index)
346359
}
347-
SourceKind::SparseRegistry => {
348-
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(
360+
SourceKind::SparseRegistry => CrateIndexLookup::Http(
361+
crates_index::SparseIndex::from_url_with_hash_kind(
349362
format!("sparse+{}", index_url).as_str(),
350-
)?)
351-
}
363+
&crate_index_hash_kind,
364+
)?,
365+
),
352366
unknown => {
353367
return Err(anyhow!(
354368
"'{:?}' crate index type is not supported (caused by '{}')",

crate_universe/src/splicing/crate_index_lookup.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ impl CrateIndexLookup {
1616
let crate_ = match self {
1717
// The crates we care about should all be in the cache already,
1818
// because `cargo metadata` ran which should have fetched them.
19-
Self::Http(index) => Some(
20-
index
21-
.crate_from_cache(pkg.name.as_str())
22-
.with_context(|| format!("Failed to get crate from cache for {pkg:?}"))?,
23-
),
19+
Self::Http(index) => {
20+
Some(index.crate_from_cache(pkg.name.as_str()).with_context(|| {
21+
format!("Failed to get crate from cache: {:?}\n{:?}", index, pkg)
22+
})?)
23+
}
2424
Self::Git(index) => index.crate_(pkg.name.as_str()),
2525
};
2626
let source_info = crate_.and_then(|crate_idx| {
@@ -97,7 +97,7 @@ mod test {
9797
);
9898
}
9999
{
100-
let _e = EnvVarResetter::set("CARGO_HOME",
100+
let _e = EnvVarResetter::set("CARGO_HOME",
101101
runfiles::rlocation!(runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home").unwrap());
102102

103103
let index = CrateIndexLookup::Http(

examples/crate_universe/MODULE.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ alias_rule_global_alias_annotation_none.spec(
7272
repositories = ["alias_rule_global_alias_annotation_none"],
7373
version = "0.1.0",
7474
)
75-
alias_rule_global_alias_annotation_none.from_cargo(
75+
alias_rule_global_alias_annotation_none.from_specs(
7676
name = "alias_rule_global_alias_annotation_none",
7777
cargo_lockfile = "//alias_rule:Cargo.lock",
7878
lockfile = "//alias_rule:cargo-bazel-lock_global_alias_annotation_none.json",

rust/private/common.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ load(":providers.bzl", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo
3333
# you remove it or change its format, you will also need to update that code.
3434
DEFAULT_RUST_VERSION = "1.84.1"
3535

36-
DEFAULT_NIGHTLY_ISO_DATE = "2024-11-28"
36+
DEFAULT_NIGHTLY_ISO_DATE = "2025-01-30"
3737

3838
def _create_crate_info(**kwargs):
3939
"""A constructor for a `CrateInfo` provider

0 commit comments

Comments
 (0)