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(yarn) detect yarn berry when running bin #1388

Merged
merged 3 commits into from
Jan 10, 2023
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
1 change: 1 addition & 0 deletions crates/volta-core/fixtures/yarn/yarnrc-yml/.yarnrc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
yarnPath: .yarn/releases/yarn-3.3.0.cjs
11 changes: 11 additions & 0 deletions crates/volta-core/fixtures/yarn/yarnrc-yml/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "yarnrc-yml",
"version": "2.0.0",
"description": "Testing that Yarn berry things work",
"license": "To Ill",
"volta": {
"node": "6.11.1",
"npm": "3.10.10",
"yarn": "3.3.0"
}
}
10 changes: 6 additions & 4 deletions crates/volta-core/src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,15 @@ impl Project {
})
}

/// Does this project use Yarn Plug'n'Play?
// (project uses Yarn, and either of the files '.pnp.js' or '.pnp.cjs' exist)
pub fn is_yarn_pnp(&self) -> bool {
/// Yarn projects that are using PnP or pnpm linker need to use yarn run.
// (project uses Yarn berry if 'yarnrc.yml' exists, uses PnP if '.pnp.js' or '.pnp.cjs' exist)
pub fn needs_yarn_run(&self) -> bool {
self.platform()
.map_or(false, |platform| platform.yarn.is_some())
&& self.manifest_file.parent().map_or(false, |base_dir| {
base_dir.join(".pnp.js").exists() || base_dir.join(".pnp.cjs").exists()
base_dir.join(".yarnrc.yml").exists()
|| base_dir.join(".pnp.js").exists()
|| base_dir.join(".pnp.cjs").exists()
})
}

Expand Down
21 changes: 14 additions & 7 deletions crates/volta-core/src/project/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,27 +218,34 @@ mod project {
}
}

mod plug_n_play {
mod needs_yarn_run {
use super::*;

#[test]
fn project_is_not_pnp() {
fn project_does_not_need_yarn_run() {
let project_path = fixture_path(&["basic"]);
let test_project = Project::for_dir(project_path).unwrap().unwrap();
assert!(!test_project.is_yarn_pnp());
assert!(!test_project.needs_yarn_run());
}

#[test]
fn project_has_yarnrc_yml() {
let project_path = fixture_path(&["yarn", "yarnrc-yml"]);
let test_project = Project::for_dir(project_path).unwrap().unwrap();
assert!(test_project.needs_yarn_run());
}

#[test]
fn project_has_pnp_js() {
let project_path = fixture_path(&["plug-n-play", "pnp-js"]);
let project_path = fixture_path(&["yarn", "pnp-js"]);
let test_project = Project::for_dir(project_path).unwrap().unwrap();
assert!(test_project.is_yarn_pnp());
assert!(test_project.needs_yarn_run());
}

#[test]
fn project_has_pnp_cjs() {
let project_path = fixture_path(&["plug-n-play", "pnp-cjs"]);
let project_path = fixture_path(&["yarn", "pnp-cjs"]);
let test_project = Project::for_dir(project_path).unwrap().unwrap();
assert!(test_project.is_yarn_pnp());
assert!(test_project.needs_yarn_run());
}
}
7 changes: 5 additions & 2 deletions crates/volta-core/src/run/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,11 @@ pub(super) fn command(exe: &OsStr, args: &[OsString], session: &mut Session) ->
.into());
}
None => {
if project.is_yarn_pnp() {
debug!("Project uses Yarn PnP, calling {} with 'yarn'", bin);
if project.needs_yarn_run() {
debug!(
"Project needs to use yarn to run command, calling {} with 'yarn'",
bin
);
let platform = Platform::current(session)?;
let mut exe_and_args = vec![exe.to_os_string()];
exe_and_args.extend_from_slice(args);
Expand Down