From 5ab8ac047a23ce7c066c7c054fcaffc7a914cada Mon Sep 17 00:00:00 2001 From: Chris Olszewski Date: Tue, 18 Jul 2023 15:06:38 -0700 Subject: [PATCH] fix(lockfile): Fix directory resolution variant (#5551) ### Description Fixes #5529 During the Rust migration I must've messed up the directory field name. Double checked against [`@pnpm/lockfile-types`](https://github.com/pnpm/pnpm/blob/main/lockfile/lockfile-types/src/index.ts#L86) to make sure all of the fields are correct now. `PackageResolution` should be an enum, but the fact that tarballs are an untagged variant makes that tricky to communicate to `serde`. A struct does enough for us. ### Testing Instructions Added new unit test to make sure we don't lose any fields for the various variants of the `resolution` field --------- Co-authored-by: Chris Olszewski --- crates/turborepo-lockfiles/src/pnpm/data.rs | 40 +++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/crates/turborepo-lockfiles/src/pnpm/data.rs b/crates/turborepo-lockfiles/src/pnpm/data.rs index 5b50e3f7ab110..3364d76aa7db7 100644 --- a/crates/turborepo-lockfiles/src/pnpm/data.rs +++ b/crates/turborepo-lockfiles/src/pnpm/data.rs @@ -80,7 +80,6 @@ pub struct Dependency { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] #[serde(rename_all = "camelCase")] pub struct PackageSnapshot { - // can we make this flow?/is it necessary? resolution: PackageResolution, #[serde(skip_serializing_if = "Option::is_none")] id: Option, @@ -113,14 +112,21 @@ pub struct DependenciesMeta { #[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)] pub struct PackageResolution { + // Type field, cannot use serde(tag) due to tarball having an empty type field + // tarball -> none + // directory -> 'directory' + // git repository -> 'git' #[serde(rename = "type", skip_serializing_if = "Option::is_none")] type_field: Option, + // Tarball fields #[serde(skip_serializing_if = "Option::is_none")] integrity: Option, #[serde(skip_serializing_if = "Option::is_none")] tarball: Option, + // Directory fields #[serde(skip_serializing_if = "Option::is_none")] - dir: Option, + directory: Option, + // Git repository fields #[serde(skip_serializing_if = "Option::is_none")] repo: Option, #[serde(skip_serializing_if = "Option::is_none")] @@ -792,4 +798,34 @@ mod tests { ], ); } + + #[test] + fn test_injected_package_round_trip() { + let original_contents = "a: + resolution: + type: directory, + directory: packages/ui, + name: ui + version: 0.0.0 + dev: false +b: + resolution: + integrity: deadbeef, + tarball: path/to/tarball.tar.gz, + name: tar + version: 0.0.0 + dev: false +c: + resolution: + repo: great-repo.git, + commit: greatcommit, + name: git + version: 0.0.0 + dev: false +"; + let original_parsed: Map = + serde_yaml::from_str(original_contents).unwrap(); + let contents = serde_yaml::to_string(&original_parsed).unwrap(); + assert_eq!(original_contents, &contents); + } }