Skip to content

Commit 4ec389d

Browse files
committed
Lookup PackageId in resolved metadata
instead of trying to create ourselves using information available: internal representation is not stable. Sadly tests had to go
1 parent 218d0f7 commit 4ec389d

File tree

3 files changed

+31
-93
lines changed

3 files changed

+31
-93
lines changed

crate2nix/src/lib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ use anyhow::Context;
2020
use anyhow::Error;
2121
use cargo_metadata::Metadata;
2222
use cargo_metadata::PackageId;
23+
use metadata::MergedMetadata;
2324
use serde::Deserialize;
2425
use serde::Serialize;
2526

@@ -84,7 +85,7 @@ impl BuildInfo {
8485

8586
default_nix.prune_unneeded_crates();
8687

87-
prefetch_and_fill_crates_sha256(config, &mut default_nix)?;
88+
prefetch_and_fill_crates_sha256(config, &merged, &mut default_nix)?;
8889

8990
Ok(default_nix)
9091
}
@@ -176,10 +177,11 @@ fn cargo_metadata(config: &GenerateConfig, cargo_toml: &Path) -> Result<Metadata
176177
/// Prefetch hashes when necessary.
177178
fn prefetch_and_fill_crates_sha256(
178179
config: &GenerateConfig,
180+
merged: &MergedMetadata,
179181
default_nix: &mut BuildInfo,
180182
) -> Result<(), Error> {
181183
let mut from_lock_file: HashMap<PackageId, String> =
182-
extract_hashes_from_lockfile(config, default_nix)?;
184+
extract_hashes_from_lockfile(config, merged, default_nix)?;
183185
for (_package_id, hash) in from_lock_file.iter_mut() {
184186
let bytes =
185187
hex::decode(&hash).map_err(|e| format_err!("while decoding '{}': {}", hash, e))?;
@@ -215,6 +217,7 @@ fn prefetch_and_fill_crates_sha256(
215217

216218
fn extract_hashes_from_lockfile(
217219
config: &GenerateConfig,
220+
merged: &MergedMetadata,
218221
default_nix: &mut BuildInfo,
219222
) -> Result<HashMap<PackageId, String>, Error> {
220223
if !config.use_cargo_lock_checksums {
@@ -227,7 +230,7 @@ fn extract_hashes_from_lockfile(
227230
let lock_file_path = cargo_toml.parent().unwrap().join("Cargo.lock");
228231
let lock_file = crate::lock::EncodableResolve::load_lock_file(&lock_file_path)?;
229232
lock_file
230-
.get_hashes_by_package_id(&mut hashes)
233+
.get_hashes_by_package_id(merged, &mut hashes)
231234
.context(format!(
232235
"while parsing checksums from Lockfile {}",
233236
&lock_file_path.to_string_lossy()

crate2nix/src/lock.rs

Lines changed: 24 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ use std::fmt;
88
use std::path::Path;
99
use std::str::FromStr;
1010

11+
use crate::metadata::MergedMetadata;
12+
1113
impl EncodableResolve {
1214
pub fn load_lock_file(path: &Path) -> Result<EncodableResolve, Error> {
1315
let config = &std::fs::read_to_string(path)
@@ -27,8 +29,19 @@ impl EncodableResolve {
2729

2830
pub fn get_hashes_by_package_id(
2931
&self,
32+
metadata: &MergedMetadata,
3033
hashes: &mut HashMap<PackageId, String>,
3134
) -> Result<(), Error> {
35+
let mut package_id_by_source = HashMap::new();
36+
for p in &metadata.packages {
37+
let Some(ref source) = p.source else {
38+
// local crate
39+
continue;
40+
};
41+
let key = (p.name.as_str(), source.repr.as_str(), p.version.to_string());
42+
package_id_by_source.insert(key, &p.id);
43+
}
44+
3245
for EncodableDependency {
3346
name,
3447
version,
@@ -37,13 +50,17 @@ impl EncodableResolve {
3750
..
3851
} in self.package.iter()
3952
{
40-
if let (Some(source), Some(checksum)) = (source, checksum) {
41-
let package_id = PackageId {
42-
repr: format!("{} {} ({})", name, version, source),
43-
};
44-
if checksum != "<none>" {
45-
hashes.insert(package_id, checksum.clone());
46-
}
53+
let Some((source, checksum)) = Option::zip(source.as_ref(), checksum.as_ref()) else {
54+
continue;
55+
};
56+
if checksum == "<none>" {
57+
continue;
58+
}
59+
60+
if let Some(package_id) =
61+
package_id_by_source.get(&(name.as_str(), source.as_str(), version.clone()))
62+
{
63+
hashes.insert((*package_id).clone(), checksum.clone());
4764
}
4865
}
4966

@@ -66,88 +83,6 @@ impl EncodableResolve {
6683
}
6784
}
6885

69-
#[test]
70-
fn test_no_legacy_checksums() {
71-
let config = r#"
72-
[[package]]
73-
name = "aho-corasick"
74-
version = "0.7.6"
75-
source = "registry+https://github.com/rust-lang/crates.io-index"
76-
dependencies = [
77-
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
78-
]
79-
"#;
80-
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap();
81-
let mut hashes = HashMap::new();
82-
resolve.get_hashes_by_package_id(&mut hashes).unwrap();
83-
assert_eq!(hashes, HashMap::new());
84-
}
85-
86-
#[test]
87-
fn test_some_legacy_checksums() {
88-
let config = r#"
89-
[[package]]
90-
name = "aho-corasick"
91-
version = "0.7.6"
92-
source = "registry+https://github.com/rust-lang/crates.io-index"
93-
dependencies = [
94-
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
95-
]
96-
97-
[metadata]
98-
"checksum structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
99-
"checksum structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)" = "53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107"
100-
101-
"#;
102-
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap();
103-
let mut hashes = HashMap::new();
104-
resolve.get_hashes_by_package_id(&mut hashes).unwrap();
105-
assert_eq!(
106-
hashes,
107-
[(
108-
PackageId { repr: "structopt 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)".to_string() },
109-
"16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
110-
),
111-
(
112-
PackageId { repr: "structopt-derive 0.2.18 (registry+https://github.com/rust-lang/crates.io-index)".to_string()},
113-
"53010261a84b37689f9ed7d395165029f9cc7abb9f56bbfe86bee2597ed25107"
114-
)]
115-
.iter()
116-
.map(|(package_id, hash)| (package_id.clone(), hash.to_string()))
117-
.collect::<HashMap<_, _>>()
118-
);
119-
}
120-
121-
#[test]
122-
fn test_some_inline_checksums() {
123-
let config = r#"
124-
[[package]]
125-
name = "aho-corasick"
126-
version = "0.7.6"
127-
source = "registry+https://github.com/rust-lang/crates.io-index"
128-
dependencies = [
129-
"memchr 2.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
130-
]
131-
checksum = "16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
132-
"#;
133-
let resolve = EncodableResolve::load_lock_string(Path::new("dummy"), config).unwrap();
134-
let mut hashes = HashMap::new();
135-
resolve.get_hashes_by_package_id(&mut hashes).unwrap();
136-
assert_eq!(
137-
hashes,
138-
[(
139-
PackageId {
140-
repr: "aho-corasick 0.7.6 (registry+https://github.com/rust-lang/crates.io-index)"
141-
.to_string()
142-
},
143-
"16c2cdbf9cc375f15d1b4141bc48aeef444806655cd0e904207edc8d68d86ed7"
144-
)]
145-
.iter()
146-
.map(|(package_id, hash)| (package_id.clone(), hash.to_string()))
147-
.collect::<HashMap<_, _>>()
148-
);
149-
}
150-
15186
//
15287
// The code below was copied/adjusted from Cargo.
15388
//

crate2nix/src/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use serde::Serialize;
2222
#[derive(Debug)]
2323
pub struct MergedMetadata {
2424
workspace_members: Vec<PackageId>,
25-
packages: Vec<Package>,
25+
pub(crate) packages: Vec<Package>,
2626
root: Option<PackageId>,
2727
nodes: Vec<Node>,
2828
}

0 commit comments

Comments
 (0)