Skip to content

Commit

Permalink
fix: Fix pnpm lockfile parsing for non-workspace repos. (#595)
Browse files Browse the repository at this point in the history
* Fix fields.

* Take in commit.

* Move tests.

* Test pnpm.

* Test npm.

* Add tests.

* Bump version.

* Fix lockfile.
  • Loading branch information
milesj authored Feb 4, 2023
1 parent 875c3f0 commit e203529
Show file tree
Hide file tree
Showing 50 changed files with 250 additions and 26 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/410551b9.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
releases:
"@moonrepo/cli": patch
"@moonrepo/core-linux-arm64-gnu": patch
"@moonrepo/core-linux-arm64-musl": patch
"@moonrepo/core-linux-x64-gnu": patch
"@moonrepo/core-linux-x64-musl": patch
"@moonrepo/core-macos-arm64": patch
"@moonrepo/core-macos-x64": patch
"@moonrepo/core-windows-x64-msvc": patch
16 changes: 8 additions & 8 deletions crates/cli/tests/docker_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ mod scaffold_workspace {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -114,7 +114,7 @@ mod scaffold_workspace {
get_node_depman_fixture_configs("pnpm");

let sandbox = create_sandbox_with_config(
"node-pnpm",
"node-pnpm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -136,7 +136,7 @@ mod scaffold_workspace {
get_node_depman_fixture_configs("yarn");

let sandbox = create_sandbox_with_config(
"node-yarn",
"node-yarn/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -158,7 +158,7 @@ mod scaffold_workspace {
get_node_depman_fixture_configs("yarn1");

let sandbox = create_sandbox_with_config(
"node-yarn1",
"node-yarn1/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down Expand Up @@ -295,7 +295,7 @@ mod prune_node {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down Expand Up @@ -327,7 +327,7 @@ mod prune_node {
get_node_depman_fixture_configs("pnpm");

let sandbox = create_sandbox_with_config(
"node-pnpm",
"node-pnpm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down Expand Up @@ -357,7 +357,7 @@ mod prune_node {
get_node_depman_fixture_configs("yarn");

let sandbox = create_sandbox_with_config(
"node-yarn",
"node-yarn/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down Expand Up @@ -387,7 +387,7 @@ mod prune_node {
get_node_depman_fixture_configs("yarn1");

let sandbox = create_sandbox_with_config(
"node-yarn1",
"node-yarn1/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down
20 changes: 11 additions & 9 deletions crates/cli/tests/node_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ mod run_script {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -36,7 +36,7 @@ mod run_script {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -58,7 +58,7 @@ mod run_script {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -78,15 +78,17 @@ mod run_script {
get_node_depman_fixture_configs("npm");

let sandbox = create_sandbox_with_config(
"node-npm",
"node-npm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
);

let assert = sandbox.run_moon(|cmd| {
cmd.args(["node", "run-script", "test"])
.env("MOON_PROJECT_ROOT", get_fixtures_path("node-npm/base"));
cmd.args(["node", "run-script", "test"]).env(
"MOON_PROJECT_ROOT",
get_fixtures_path("node-npm/workspaces/base"),
);
});

assert.success().stdout(predicate::str::contains("> test"));
Expand All @@ -99,7 +101,7 @@ mod run_script {
get_node_depman_fixture_configs("pnpm");

let sandbox = create_sandbox_with_config(
"node-pnpm",
"node-pnpm/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -119,7 +121,7 @@ mod run_script {
get_node_depman_fixture_configs("yarn");

let sandbox = create_sandbox_with_config(
"node-yarn",
"node-yarn/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand All @@ -139,7 +141,7 @@ mod run_script {
get_node_depman_fixture_configs("yarn1");

let sandbox = create_sandbox_with_config(
"node-yarn1",
"node-yarn1/workspaces",
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down
67 changes: 65 additions & 2 deletions crates/cli/tests/run_node_test.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use moon_config::{NodeConfig, TypeScriptConfig};
use moon_config::{NodeConfig, TypeScriptConfig, WorkspaceProjects};
use moon_test_utils::{
assert_snapshot, create_sandbox_with_config, get_node_depman_fixture_configs,
get_node_fixture_configs, get_typescript_fixture_configs, predicates::prelude::*, Sandbox,
};
use moon_utils::string_vec;
use rustc_hash::FxHashMap;
use std::fs::read_to_string;

fn node_sandbox() -> Sandbox {
Expand Down Expand Up @@ -46,7 +47,25 @@ fn depman_sandbox(depman: &str) -> Sandbox {
get_node_depman_fixture_configs(depman);

let sandbox = create_sandbox_with_config(
format!("node-{}", depman),
format!("node-{}/workspaces", depman),
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
);

sandbox.enable_git();
sandbox
}

fn depman_non_workspaces_sandbox(depman: &str) -> Sandbox {
let (mut workspace_config, toolchain_config, tasks_config) =
get_node_depman_fixture_configs(depman);

workspace_config.projects =
WorkspaceProjects::Sources(FxHashMap::from_iter([("root".to_owned(), ".".to_owned())]));

let sandbox = create_sandbox_with_config(
format!("node-{}/project", depman),
Some(&workspace_config),
Some(&toolchain_config),
Some(&tasks_config),
Expand Down Expand Up @@ -704,6 +723,17 @@ mod npm {

assert.success();
}

#[test]
fn works_in_non_workspaces_project() {
let sandbox = depman_non_workspaces_sandbox("npm");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("root:version");
});

assert!(predicate::str::contains("8.0.0").eval(&assert.output()));
}
}

mod pnpm {
Expand Down Expand Up @@ -806,6 +836,17 @@ mod pnpm {

// assert.success();
// }

#[test]
fn works_in_non_workspaces_project() {
let sandbox = depman_non_workspaces_sandbox("pnpm");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("root:version");
});

assert!(predicate::str::contains("7.5.0").eval(&assert.output()));
}
}

mod yarn1 {
Expand Down Expand Up @@ -886,6 +927,17 @@ mod yarn1 {

assert.success();
}

#[test]
fn works_in_non_workspaces_project() {
let sandbox = depman_non_workspaces_sandbox("yarn1");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("root:version");
});

assert!(predicate::str::contains("1.22.0").eval(&assert.output()));
}
}

mod yarn {
Expand Down Expand Up @@ -966,6 +1018,17 @@ mod yarn {

assert.success();
}

#[test]
fn works_in_non_workspaces_project() {
let sandbox = depman_non_workspaces_sandbox("yarn");

let assert = sandbox.run_moon(|cmd| {
cmd.arg("run").arg("root:version");
});

assert!(predicate::str::contains("3.3.0").eval(&assert.output()));
}
}

mod profile {
Expand Down
2 changes: 1 addition & 1 deletion crates/node/lang/src/pnpm/dependency_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use thiserror::Error;

#[derive(Error, Debug, PartialEq, Eq)]
pub enum DependencyPathError {
#[error("<symbol>{0}</symbol> is an invalid relative dependency path")]
#[error("<symbol>{0}</symbol> is an invalid pnpm relative dependency path.")]
IsNotAbsolute(String),
}

Expand Down
23 changes: 17 additions & 6 deletions crates/node/lang/src/pnpm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// https://github.com/pnpm/pnpm/blob/main/lockfile/lockfile-types/src/index.ts

pub mod dependency_path;
pub mod workspace;

Expand All @@ -21,13 +23,17 @@ type DependencyMap = FxHashMap<String, Value>;
pub struct PnpmLockPackage {
pub cpu: Option<Vec<String>>,
pub dependencies: Option<DependencyMap>,
pub deprecated: Option<String>,
pub dev: Option<bool>,
pub engines: Option<FxHashMap<String, String>>,
pub has_bin: Option<bool>,
pub libc: Option<Vec<String>>,
pub optional: Option<bool>,
pub optional_dependencies: Option<DependencyMap>,
pub os: Option<Vec<String>>,
pub patched: Option<bool>,
pub peer_dependencies: Option<DependencyMap>,
pub prepare: Option<bool>,
pub requires_build: Option<bool>,
pub transitive_peer_dependencies: Option<Vec<String>>,
pub resolution: PnpmLockResolution,
Expand All @@ -36,6 +42,7 @@ pub struct PnpmLockPackage {
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PnpmLockResolution {
pub commit: Option<String>, // git
pub integrity: Option<String>,
pub tarball: Option<String>,
}
Expand All @@ -44,7 +51,7 @@ pub struct PnpmLockResolution {
#[serde(rename_all = "camelCase")]
pub struct PnpmLock {
pub lockfile_version: Value,
pub importers: FxHashMap<String, Value>,
pub importers: Option<FxHashMap<String, Value>>,
pub packages: FxHashMap<String, PnpmLockPackage>,

#[serde(skip)]
Expand All @@ -69,6 +76,10 @@ pub fn load_lockfile_dependencies(path: PathBuf) -> Result<LockfileDependencyVer
if let Some(ver) = details.resolution.tarball {
entry.push(ver.clone());
}

if let Some(ver) = details.resolution.commit {
entry.push(ver.clone());
}
}
}

Expand Down Expand Up @@ -141,7 +152,7 @@ packages:
lockfile,
PnpmLock {
lockfile_version: Value::Number(Number::from(5.4)),
importers: FxHashMap::from_iter([(".".into(), Value::Mapping(Mapping::new()))]),
importers: Some(FxHashMap::from_iter([(".".into(), Value::Mapping(Mapping::new()))])),
packages: FxHashMap::from_iter([(
"/@ampproject/remapping/2.2.0".into(),
PnpmLockPackage {
Expand All @@ -154,7 +165,7 @@ packages:
("node".to_owned(), ">=6.0.0".to_owned())
])),
resolution:
PnpmLockResolution { integrity: Some("sha512-qRmjj8nj9qmLTQXXmaR1cck3UXSRMPrbsLJAasZpF+t3riI71BXed5ebIOYwQntykeZuhjsdweEc9BxH5Jc26w==".to_owned()), tarball: None },
PnpmLockResolution { commit: None, integrity: Some("sha512-qRmjj8nj9qmLTQXXmaR1cck3UXSRMPrbsLJAasZpF+t3riI71BXed5ebIOYwQntykeZuhjsdweEc9BxH5Jc26w==".to_owned()), tarball: None },
..PnpmLockPackage::default()
}
), (
Expand All @@ -170,7 +181,7 @@ packages:
Value::String("^7.0.0-0".to_owned())
)])),
resolution:
PnpmLockResolution { integrity: Some( "sha512-tycmZxkGfZaxhMRbXlPXuVFpdWlXpir2W4AMhSJgRKzk/eDlIXOhb2LHWoLpDF7TEHylV5zNhykX6KAgHJmTNw==".to_owned()), tarball: None },
PnpmLockResolution { commit: None,integrity: Some( "sha512-tycmZxkGfZaxhMRbXlPXuVFpdWlXpir2W4AMhSJgRKzk/eDlIXOhb2LHWoLpDF7TEHylV5zNhykX6KAgHJmTNw==".to_owned()), tarball: None },
..PnpmLockPackage::default()
}
), (
Expand All @@ -181,7 +192,7 @@ packages:
("node".to_owned(), ">=8".to_owned())
])),
resolution:
PnpmLockResolution { integrity: Some( "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==".to_owned()), tarball: None },
PnpmLockResolution { commit: None,integrity: Some( "sha512-HGyxoOTYUyCM6stUe6EJgnd4EoewAI7zMdfqO+kGjnlZmBDz/cR5pf8r/cR4Wq60sL/p0IkcjUEEPwS3GFrIyw==".to_owned()), tarball: None },
..PnpmLockPackage::default()
}
), (
Expand All @@ -199,7 +210,7 @@ packages:
)])),
transitive_peer_dependencies: Some(string_vec!["@babel/core", "supports-color"]),
resolution:
PnpmLockResolution { integrity: Some( "sha512-1ILtAj+z6bh1vTvaDlcT8501vmkzkVZMk2aiexJy+XWTZ+sb9B7IWedvWadIhOwwL97fiW4eMmN6SrbaHjn12A==".to_owned()), tarball: None },
PnpmLockResolution { commit: None,integrity: Some( "sha512-1ILtAj+z6bh1vTvaDlcT8501vmkzkVZMk2aiexJy+XWTZ+sb9B7IWedvWadIhOwwL97fiW4eMmN6SrbaHjn12A==".to_owned()), tarball: None },
..PnpmLockPackage::default()
}
)]),
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- Fixed an issue where pnpm lockfile parsing would fail when not workspaces enabled.

## 0.23.3

#### 🐞 Fixes
Expand Down
5 changes: 5 additions & 0 deletions tests/fixtures/node-npm/project/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
language: javascript

tasks:
version:
command: npm
Loading

0 comments on commit e203529

Please sign in to comment.