Skip to content

Commit

Permalink
Fixed crate_universe when using Rust >= 1.85.0
Browse files Browse the repository at this point in the history
  • Loading branch information
UebelAndre committed Feb 9, 2025
1 parent 5226882 commit e7d37ac
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 26 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 83 additions & 0 deletions crate_universe/3rdparty/crates/BUILD.rustc-stable-hash-0.1.1.bazel

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 15 additions & 5 deletions crate_universe/3rdparty/crates/defs.bzl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 8 additions & 2 deletions crate_universe/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion crate_universe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ cargo-lock = "10.0.1"
cargo-platform = "0.1.9"
cfg-expr = "0.17.2"
clap = { version = "4.5.26", features = ["derive", "env"] }
crates-index = { version = "3.5.0", default-features = false, features = [
# crates-index = { version = "3.5.0", default-features = false, features = [
# "git",
# ] }
crates-index = { git = "https://github.com/UebelAndre/rust-crates-index.git", rev = "a77d2d0981aaef368e10406912febbbdad4b83d7", default-features = false, features = [
"git",
] }
hex = "0.4.3"
Expand Down
10 changes: 10 additions & 0 deletions crate_universe/src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ impl LockGenerator {

// Ensure the Cargo cache is up to date to simulate the behavior
// of having just generated a new one
tracing::debug!("Fetching crates for {}", manifest_path);
let output = self
.cargo_bin
.command()?
Expand All @@ -230,6 +231,7 @@ impl LockGenerator {
.arg("fetch")
.arg("--manifest-path")
.arg(manifest_path.as_std_path())
.arg("--verbose")
.output()
.context(format!(
"Error running cargo to fetch crates '{}'",
Expand All @@ -244,6 +246,14 @@ impl LockGenerator {
output.status
))
}
tracing::trace!(
"Cargo fetch stderr:\n{}",
String::from_utf8_lossy(&output.stderr)
);
tracing::trace!(
"Cargo fetch stdout:\n{}",
String::from_utf8_lossy(&output.stdout)
);
} else {
debug!("Generating new lockfile");
// Simply invoke `cargo generate-lockfile`
Expand Down
12 changes: 12 additions & 0 deletions crate_universe/src/metadata/cargo_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,18 @@ impl Cargo {
bail!("Couldn't parse cargo version");
}

/// Determine if Cargo is on a version which uses new hashing behavior
/// introduced in Rust 1.86.0. For details see <https://github.com/frewsxcv/rust-crates-index/issues/182>
pub(crate) fn uses_stable_registry_hash(&self) -> Result<bool> {
let full_version = self.full_version()?;
let version_str = full_version.split(' ').nth(1);
if let Some(version_str) = version_str {
let version = Version::parse(version_str).context("Failed to parse cargo version")?;
return Ok(version.major >= 1 && version.minor >= 85);
}
bail!("Couldn't parse cargo version");
}

fn env(&self) -> Result<BTreeMap<String, OsString>> {
let mut map = BTreeMap::new();

Expand Down
34 changes: 24 additions & 10 deletions crate_universe/src/splicing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,12 @@ impl WorkspaceMetadata {
}
};

let crate_index_hash_kind = if cargo.uses_stable_registry_hash()? {
crates_index::HashKind::Stable
} else {
crates_index::HashKind::Legacy
};

// Load each index for easy access
let crate_indexes = index_urls
.into_iter()
Expand All @@ -320,20 +326,27 @@ impl WorkspaceMetadata {
let index = if cargo.use_sparse_registries_for_crates_io()?
&& index_url == utils::CRATES_IO_INDEX_URL
{
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(
CrateIndexLookup::Http(crates_index::SparseIndex::from_url_with_hash_kind(
"sparse+https://index.crates.io/",
&crate_index_hash_kind,
)?)
} else if index_url.starts_with("sparse+") {
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(index_url)?)
CrateIndexLookup::Http(crates_index::SparseIndex::from_url_with_hash_kind(
index_url,
&crate_index_hash_kind,
)?)
} else {
match source_kind {
SourceKind::Registry => {
let index = {
// Load the index for the current url
let index = crates_index::GitIndex::from_url(index_url)
.with_context(|| {
format!("Failed to load index for url: {index_url}")
})?;
let index = crates_index::GitIndex::from_url_with_hash_kind(
index_url,
&crate_index_hash_kind,
)
.with_context(|| {
format!("Failed to load index for url: {index_url}")
})?;

// Ensure each index has a valid index config
index.index_config().with_context(|| {
Expand All @@ -344,11 +357,12 @@ impl WorkspaceMetadata {
};
CrateIndexLookup::Git(index)
}
SourceKind::SparseRegistry => {
CrateIndexLookup::Http(crates_index::SparseIndex::from_url(
SourceKind::SparseRegistry => CrateIndexLookup::Http(
crates_index::SparseIndex::from_url_with_hash_kind(
format!("sparse+{}", index_url).as_str(),
)?)
}
&crate_index_hash_kind,
)?,
),
unknown => {
return Err(anyhow!(
"'{:?}' crate index type is not supported (caused by '{}')",
Expand Down
12 changes: 6 additions & 6 deletions crate_universe/src/splicing/crate_index_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ impl CrateIndexLookup {
let crate_ = match self {
// The crates we care about should all be in the cache already,
// because `cargo metadata` ran which should have fetched them.
Self::Http(index) => Some(
index
.crate_from_cache(pkg.name.as_str())
.with_context(|| format!("Failed to get crate from cache for {pkg:?}"))?,
),
Self::Http(index) => {
Some(index.crate_from_cache(pkg.name.as_str()).with_context(|| {
format!("Failed to get crate from cache: {:?}\n{:?}", index, pkg)
})?)
}
Self::Git(index) => index.crate_(pkg.name.as_str()),
};
let source_info = crate_.and_then(|crate_idx| {
Expand Down Expand Up @@ -97,7 +97,7 @@ mod test {
);
}
{
let _e = EnvVarResetter::set("CARGO_HOME",
let _e = EnvVarResetter::set("CARGO_HOME",
runfiles::rlocation!(runfiles, "rules_rust/crate_universe/test_data/crate_indexes/rewritten_lazy_static/cargo_home").unwrap());

let index = CrateIndexLookup::Http(
Expand Down
2 changes: 1 addition & 1 deletion examples/crate_universe/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ alias_rule_global_alias_annotation_none.spec(
repositories = ["alias_rule_global_alias_annotation_none"],
version = "0.1.0",
)
alias_rule_global_alias_annotation_none.from_cargo(
alias_rule_global_alias_annotation_none.from_specs(
name = "alias_rule_global_alias_annotation_none",
cargo_lockfile = "//alias_rule:Cargo.lock",
lockfile = "//alias_rule:cargo-bazel-lock_global_alias_annotation_none.json",
Expand Down
2 changes: 1 addition & 1 deletion rust/private/common.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ load(":providers.bzl", "CrateGroupInfo", "CrateInfo", "DepInfo", "DepVariantInfo
# you remove it or change its format, you will also need to update that code.
DEFAULT_RUST_VERSION = "1.84.1"

DEFAULT_NIGHTLY_ISO_DATE = "2024-11-28"
DEFAULT_NIGHTLY_ISO_DATE = "2025-01-30"

def _create_crate_info(**kwargs):
"""A constructor for a `CrateInfo` provider
Expand Down

0 comments on commit e7d37ac

Please sign in to comment.