From 968a4409f6f79a4bfba23fe6ada865d4267d70d6 Mon Sep 17 00:00:00 2001 From: Jeff Valore Date: Sat, 6 Jul 2024 11:43:14 -0400 Subject: [PATCH] fix(run): Use bin scripts defined in a workspace package before the workspace root. When resolving bin scripts, ones in the workspace root were being added to a Map last, overwriting those from the workspace package where the script was being run. This change will now favor workspace-package specific bins instead. fix #8590 --- __tests__/__snapshots__/index.js.snap | 15 +++++ __tests__/commands/run.js | 56 +++++++++++++++++++ .../run/issue-8590/node_modules/.bin/eslint | 0 .../fixtures/run/issue-8590/package.json | 13 +++++ .../workspace-a/node_modules/.bin/eslint | 0 .../packages/workspace-a/package.json | 7 +++ .../workspace-b/node_modules/.bin/eslint | 0 .../packages/workspace-b/package.json | 7 +++ src/cli/commands/run.js | 6 +- 9 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/run/issue-8590/node_modules/.bin/eslint create mode 100644 __tests__/fixtures/run/issue-8590/package.json create mode 100644 __tests__/fixtures/run/issue-8590/packages/workspace-a/node_modules/.bin/eslint create mode 100644 __tests__/fixtures/run/issue-8590/packages/workspace-a/package.json create mode 100644 __tests__/fixtures/run/issue-8590/packages/workspace-b/node_modules/.bin/eslint create mode 100644 __tests__/fixtures/run/issue-8590/packages/workspace-b/package.json diff --git a/__tests__/__snapshots__/index.js.snap b/__tests__/__snapshots__/index.js.snap index c7154dd81b..17422d3df0 100644 --- a/__tests__/__snapshots__/index.js.snap +++ b/__tests__/__snapshots__/index.js.snap @@ -62,6 +62,21 @@ Array [ ] `; +exports[`should add package with no-lockfile option 2`] = ` +Array [ + "info No lockfile found.", + "[1/4] Resolving packages...", + "[2/4] Fetching packages...", + "[3/4] Linking dependencies...", + "[4/4] Building fresh packages...", + "success Saved 1 new dependency.", + "info Direct dependencies", + "└─ repeating@3.0.0", + "info All dependencies", + "└─ repeating@3.0.0", +] +`; + exports[`should add package with no-lockfile option in front 1`] = ` Array [ "info No lockfile found.", diff --git a/__tests__/commands/run.js b/__tests__/commands/run.js index 35a1221e56..54b0a68e56 100644 --- a/__tests__/commands/run.js +++ b/__tests__/commands/run.js @@ -217,6 +217,62 @@ test('adds cwd node_modules/.bin to path when in a workspace usig nohoist', (): expect(envPaths).toContain(path.join(config.cwd, 'packages', 'pkg1', 'node_modules', '.bin')); })); +// Regression test for https://github.com/yarnpkg/yarn/issues/8590 +test('uses transitive script from workspace root', (): Promise => + runRun( + ['eslint'], + {}, + 'issue-8590', + (config): ?Promise => { + expect(execCommand).toBeCalledWith({ + stage: 'eslint', + config, + cmd: `${config.cwd}/node_modules/.bin/eslint`, + cwd: config.cwd, + isInteractive: true, + customShell: undefined, + }); + }, + )); + +// Regression test for https://github.com/yarnpkg/yarn/issues/8590 +test('uses transitive script from workspace package A', (): Promise => + runRunInWorkspacePackage( + 'packages/workspace-a', + ['eslint'], + {}, + 'issue-8590', + (config): ?Promise => { + expect(execCommand).toBeCalledWith({ + stage: 'eslint', + config, + cmd: `${config.cwd}/packages/workspace-a/node_modules/.bin/eslint`, + cwd: `${config.cwd}/packages/workspace-a`, + isInteractive: true, + customShell: undefined, + }); + }, + )); + +// Regression test for https://github.com/yarnpkg/yarn/issues/8590 +test('uses transitive script from workspace package B', (): Promise => + runRunInWorkspacePackage( + 'packages/workspace-b', + ['eslint'], + {}, + 'issue-8590', + (config): ?Promise => { + expect(execCommand).toBeCalledWith({ + stage: 'eslint', + config, + cmd: `${config.cwd}/packages/workspace-b/node_modules/.bin/eslint`, + cwd: `${config.cwd}/packages/workspace-b`, + isInteractive: true, + customShell: undefined, + }); + }, + )); + test('runs script with custom script-shell', (): Promise => runRunWithCustomShell('/usr/bin/dummy', ['start'], {}, 'script-shell', async (config): ?Promise => { const pkg = await fs.readJson(path.join(config.cwd, 'package.json')); diff --git a/__tests__/fixtures/run/issue-8590/node_modules/.bin/eslint b/__tests__/fixtures/run/issue-8590/node_modules/.bin/eslint new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/issue-8590/package.json b/__tests__/fixtures/run/issue-8590/package.json new file mode 100644 index 0000000000..3f00c261e5 --- /dev/null +++ b/__tests__/fixtures/run/issue-8590/package.json @@ -0,0 +1,13 @@ +{ + "name": "yarn-issue-8590", + "version": "1.0.0", + "main": "index.js", + "author": "yarn", + "license": "MIT", + "private": true, + "workspaces": { + "packages": [ + "packages/*" + ] + } +} diff --git a/__tests__/fixtures/run/issue-8590/packages/workspace-a/node_modules/.bin/eslint b/__tests__/fixtures/run/issue-8590/packages/workspace-a/node_modules/.bin/eslint new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/issue-8590/packages/workspace-a/package.json b/__tests__/fixtures/run/issue-8590/packages/workspace-a/package.json new file mode 100644 index 0000000000..c1b5493a98 --- /dev/null +++ b/__tests__/fixtures/run/issue-8590/packages/workspace-a/package.json @@ -0,0 +1,7 @@ +{ + "name": "workspace-a", + "version": "1.0.0", + "dependencies": { + "eslint": "8.57.0" + } +} diff --git a/__tests__/fixtures/run/issue-8590/packages/workspace-b/node_modules/.bin/eslint b/__tests__/fixtures/run/issue-8590/packages/workspace-b/node_modules/.bin/eslint new file mode 100644 index 0000000000..e69de29bb2 diff --git a/__tests__/fixtures/run/issue-8590/packages/workspace-b/package.json b/__tests__/fixtures/run/issue-8590/packages/workspace-b/package.json new file mode 100644 index 0000000000..fcf1d95436 --- /dev/null +++ b/__tests__/fixtures/run/issue-8590/packages/workspace-b/package.json @@ -0,0 +1,7 @@ +{ + "name": "workspace-b", + "version": "1.0.0", + "dependencies": { + "eslint": "1.0.0" + } +} diff --git a/src/cli/commands/run.js b/src/cli/commands/run.js index f0dfbf455d..d11bb09ef7 100644 --- a/src/cli/commands/run.js +++ b/src/cli/commands/run.js @@ -55,7 +55,11 @@ export async function getBinEntries(config: Config): Promise for (const binFolder of binFolders) { if (await fs.exists(binFolder)) { for (const name of await fs.readdir(binFolder)) { - binEntries.set(name, path.join(binFolder, name)); + const existingDepth = binEntries.get(name) ? binEntries.get(name).split(path.sep).length : 0; + const newDepth = binFolder.split(path.sep).length; + if (newDepth >= existingDepth) { + binEntries.set(name, path.join(binFolder, name)); + } } } }