Skip to content

Commit

Permalink
fix: Add fallback for unsupported node.dependencyVersionFormat. (#1173
Browse files Browse the repository at this point in the history
)

* Add fallback.

* Add tests.

* Fix tests.

* Fix version.
  • Loading branch information
milesj authored Nov 13, 2023
1 parent 490f0a1 commit 1828e0b
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 6 deletions.
9 changes: 9 additions & 0 deletions .yarn/versions/bb5e5642.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: 15 additions & 1 deletion crates/cli/tests/run_node_test.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use moon_config::{
NodeVersionFormat, NodeVersionManager, PartialNodeConfig, PartialWorkspaceProjects,
NodePackageManager, NodeVersionFormat, NodeVersionManager, PartialNodeConfig,
PartialWorkspaceProjects, PartialYarnConfig,
};
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 proto_core::UnresolvedVersionSpec;
use rustc_hash::FxHashMap;
use std::fs::read_to_string;

Expand Down Expand Up @@ -569,6 +571,12 @@ mod sync_depends_on {

fn test_depends_on_format(format: NodeVersionFormat) {
let sandbox = node_sandbox_with_config(|cfg| {
// Other pm's don't support all formats
cfg.package_manager = Some(NodePackageManager::Yarn);
cfg.yarn = Some(PartialYarnConfig {
version: Some(UnresolvedVersionSpec::parse("1.22.0").unwrap()),
..PartialYarnConfig::default()
});
cfg.sync_project_workspace_dependencies = Some(true);
cfg.dependency_version_format = Some(format);
});
Expand Down Expand Up @@ -632,6 +640,12 @@ mod sync_depends_on {
#[test]
fn syncs_depends_on_with_scopes() {
let sandbox = node_sandbox_with_config(|cfg| {
// Other pm's don't support all formats
cfg.package_manager = Some(NodePackageManager::Yarn);
cfg.yarn = Some(PartialYarnConfig {
version: Some(UnresolvedVersionSpec::parse("1.22.0").unwrap()),
..PartialYarnConfig::default()
});
cfg.sync_project_workspace_dependencies = Some(true);
});

Expand Down
2 changes: 1 addition & 1 deletion crates/core/test-utils/src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl Sandbox {
pub fn debug_configs(&self) -> &Self {
for cfg in glob::walk_files(self.path(), [".moon/**/*.yml"]).unwrap() {
if cfg.exists() {
println!("{:?} = {}", &cfg, fs::read_to_string(&cfg).unwrap());
println!("{:?}:\n{}", &cfg, fs::read_to_string(&cfg).unwrap());
}
}

Expand Down
40 changes: 40 additions & 0 deletions nextgen/config/src/toolchain/node_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use crate::{inherit_tool, inherit_tool_required};
use moon_common::color;
use proto_core::{PluginLocator, ToolsConfig, UnresolvedVersionSpec};
use schematic::{derive_enum, Config, ConfigEnum};
use tracing::debug;

derive_enum!(
#[derive(ConfigEnum, Copy, Default)]
Expand Down Expand Up @@ -28,6 +30,13 @@ derive_enum!(
);

impl NodeVersionFormat {
pub fn get_default_for(&self, pm: &NodePackageManager) -> Self {
match pm {
NodePackageManager::Npm => Self::File,
_ => Self::Workspace,
}
}

pub fn get_prefix(&self) -> String {
match self {
NodeVersionFormat::File => "file:".into(),
Expand All @@ -41,6 +50,18 @@ impl NodeVersionFormat {
NodeVersionFormat::WorkspaceTilde => "workspace:~".into(),
}
}

pub fn is_supported_by(&self, pm: &NodePackageManager) -> bool {
match pm {
NodePackageManager::Bun => !matches!(self, Self::WorkspaceCaret | Self::WorkspaceTilde),
NodePackageManager::Npm => !matches!(
self,
Self::Link | Self::Workspace | Self::WorkspaceCaret | Self::WorkspaceTilde
),
NodePackageManager::Pnpm => true,
NodePackageManager::Yarn => true,
}
}
}

derive_enum!(
Expand Down Expand Up @@ -201,6 +222,25 @@ impl NodeConfig {
}
};

if !self
.dependency_version_format
.is_supported_by(&self.package_manager)
{
let new_format = self
.dependency_version_format
.get_default_for(&self.package_manager);

debug!(
"{} for {} is not supported by {}, changing to {}",
color::symbol(self.dependency_version_format.to_string()),
color::property("node.dependencyVersionFormat"),
self.package_manager.to_string(),
color::symbol(new_format.to_string()),
);

self.dependency_version_format = new_format;
}

if self.plugin.is_none() {
self.plugin = Some(PluginLocator::SourceUrl {
url: "https://github.com/moonrepo/node-plugin/releases/download/v0.4.3/node_plugin.wasm".into()
Expand Down
36 changes: 35 additions & 1 deletion nextgen/config/tests/toolchain_config_test.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
mod utils;

use moon_config::{BinConfig, BinEntry, NodePackageManager, ToolchainConfig};
use moon_config::{BinConfig, BinEntry, NodePackageManager, NodeVersionFormat, ToolchainConfig};
use proto_core::{Id, PluginLocator, ToolsConfig, UnresolvedVersionSpec};
use starbase_sandbox::create_sandbox;
use std::env;
Expand Down Expand Up @@ -356,6 +356,23 @@ node:
UnresolvedVersionSpec::parse("10.0.0").unwrap()
);
}

#[test]
fn fallsback_version_format() {
let config = test_load_config(
FILENAME,
r"
node:
packageManager: npm
dependencyVersionFormat: workspace
",
|path| ToolchainConfig::load_from(path, &ToolsConfig::default()),
);

let cfg = config.node.unwrap();

assert_eq!(cfg.dependency_version_format, NodeVersionFormat::File);
}
}

mod pnpm {
Expand Down Expand Up @@ -728,6 +745,23 @@ node:
UnresolvedVersionSpec::parse("1.0.0").unwrap()
);
}

#[test]
fn fallsback_version_format() {
let config = test_load_config(
FILENAME,
r"
node:
packageManager: bun
dependencyVersionFormat: workspace-tilde
",
|path| ToolchainConfig::load_from(path, &ToolsConfig::default()),
);

let cfg = config.node.unwrap();

assert_eq!(cfg.dependency_version_format, NodeVersionFormat::Workspace);
}
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
- More accurately monitors signals (ctrl+c) and shutdowns.
- Tasks can now be configured with a timeout.

## Unreleased

#### 🐞 Fixes

- Updated `node.dependencyVersionFormat` to fallback to a supported format when the chosen
`node.packageManager` does not support the configured (or default) version format.

## 1.16.1

#### 🐞 Fixes
Expand Down
7 changes: 4 additions & 3 deletions website/docs/config/toolchain.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -233,15 +233,15 @@ When [syncing project dependencies](#syncprojectworkspacedependencies), customiz
will be used for the dependency version range. The following formats are supported (but use the one
most applicable to your chosen package manager):

- `file` - Uses `file:../relative/path` and copies package contents.
- `file` (npm default) - Uses `file:../relative/path` and copies package contents.
- `link` - Uses `link:../relative/path` and symlinks package contents.
- `star` - Uses an explicit `*`.
- `version` - Uses the explicit version from the dependent project's `package.json`, e.g., "1.2.3".
- `version-caret` - Uses the version from the dependent project's `package.json` as a caret range,
e.g., "^1.2.3".
- `version-tilde` - Uses the version from the dependent project's `package.json` as a tilde range,
e.g., "~1.2.3".
- `workspace` (default) - Uses `workspace:*`, which resolves to "1.2.3". Requires package
- `workspace` (pnpm/yarn default) - Uses `workspace:*`, which resolves to "1.2.3". Requires package
workspaces.
- `workspace-caret` - Uses `workspace:^`, which resolves to "^1.2.3". Requires package workspaces.
- `workspace-tilde` - Uses `workspace:~`, which resolves to "~1.2.3". Requires package workspaces.
Expand All @@ -252,7 +252,8 @@ node:
```

> This setting does not apply to peer dependencies, as they will always use a format of
> `^<major>.0.0`.
> `^<major>.0.0`. Furthermore, if a package manager does not support a chosen format, it will
> fallback to another format!

### `inferTasksFromScripts`

Expand Down

0 comments on commit 1828e0b

Please sign in to comment.