Skip to content

Commit

Permalink
fix: sort npm packages in snapshot area for more determinism (#196)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Aug 19, 2024
1 parent b74b3c6 commit 2e134b4
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 53 deletions.
90 changes: 45 additions & 45 deletions src/snapshots/eszip__v2__tests__npm_packages.snap
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ expression: bytes
0,
0,
0,
4,
3,
0,
0,
0,
Expand All @@ -159,7 +159,7 @@ expression: bytes
0,
0,
0,
0,
5,
0,
0,
0,
Expand All @@ -178,53 +178,14 @@ expression: bytes
0,
0,
0,
0,
5,
0,
0,
0,
158,
0,
0,
0,
13,
112,
97,
99,
107,
97,
103,
101,
64,
49,
46,
50,
46,
50,
0,
0,
0,
2,
0,
0,
0,
1,
97,
0,
0,
0,
1,
0,
0,
0,
1,
98,
0,
0,
0,
2,
0,
0,
0,
7,
97,
64,
Expand Down Expand Up @@ -277,7 +238,7 @@ expression: bytes
0,
0,
0,
3,
2,
0,
0,
0,
Expand Down Expand Up @@ -316,7 +277,7 @@ expression: bytes
0,
0,
0,
5,
4,
0,
0,
0,
Expand All @@ -340,7 +301,46 @@ expression: bytes
0,
0,
0,
4,
3,
0,
0,
0,
13,
112,
97,
99,
107,
97,
103,
101,
64,
49,
46,
50,
46,
50,
0,
0,
0,
2,
0,
0,
0,
1,
97,
0,
0,
0,
0,
0,
0,
0,
1,
98,
0,
0,
0,
1,
0,
0,
0,
Expand Down
18 changes: 10 additions & 8 deletions src/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1156,7 +1156,8 @@ impl EszipV2 {

// add npm snapshot entries to the header and fill the npm bytes
if let Some(npm_snapshot) = self.npm_snapshot {
let npm_snapshot = npm_snapshot.into_serialized();
let mut npm_snapshot = npm_snapshot.into_serialized();
npm_snapshot.packages.sort_by(|a, b| a.id.cmp(&b.id)); // determinism
let ids_to_eszip_ids = npm_snapshot
.packages
.iter()
Expand All @@ -1165,7 +1166,7 @@ impl EszipV2 {
.collect::<HashMap<_, _>>();

let mut root_packages: Vec<_> =
npm_snapshot.root_packages.into_iter().collect();
npm_snapshot.root_packages.iter().collect();
root_packages.sort();
for (req, id) in root_packages {
append_string(&mut modules_header, &req.to_string());
Expand All @@ -1178,11 +1179,7 @@ impl EszipV2 {
append_string(&mut npm_bytes, &pkg.id.as_serialized());
let deps_len = pkg.dependencies.len() as u32;
npm_bytes.extend_from_slice(&deps_len.to_be_bytes());
let mut deps: Vec<_> = pkg
.dependencies
.iter()
.map(|(a, b)| (a.clone(), b.clone()))
.collect();
let mut deps: Vec<_> = pkg.dependencies.iter().collect();
deps.sort();
for (req, id) in deps {
append_string(&mut npm_bytes, &req.to_string());
Expand Down Expand Up @@ -2898,7 +2895,12 @@ mod tests {
.await
.unwrap();
let snapshot = eszip.take_npm_snapshot().unwrap();
assert_eq!(snapshot.as_serialized(), original_snapshot.as_serialized());
assert_eq!(snapshot.into_serialized(), {
let mut original = original_snapshot.into_serialized();
// this will be sorted for determinism
original.packages.sort_by(|a, b| a.id.cmp(&b.id));
original
});

// ensure the eszip still works otherwise
fut.await.unwrap();
Expand Down

0 comments on commit 2e134b4

Please sign in to comment.