Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sort npm packages in snapshot area for more determinism #196

Merged
merged 2 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading