Skip to content

Commit

Permalink
fix(lockfile): Fix directory resolution variant (#5551)
Browse files Browse the repository at this point in the history
### 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 <Chris Olszewski>
  • Loading branch information
chris-olszewski committed Jul 18, 2023
1 parent ca7e3e4 commit 5ab8ac0
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions crates/turborepo-lockfiles/src/pnpm/data.rs
Expand Up @@ -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<String>,
Expand Down Expand Up @@ -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<String>,
// Tarball fields
#[serde(skip_serializing_if = "Option::is_none")]
integrity: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
tarball: Option<String>,
// Directory fields
#[serde(skip_serializing_if = "Option::is_none")]
dir: Option<String>,
directory: Option<String>,
// Git repository fields
#[serde(skip_serializing_if = "Option::is_none")]
repo: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -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<String, PackageSnapshot> =
serde_yaml::from_str(original_contents).unwrap();
let contents = serde_yaml::to_string(&original_parsed).unwrap();
assert_eq!(original_contents, &contents);
}
}

0 comments on commit 5ab8ac0

Please sign in to comment.