Skip to content

Commit

Permalink
fix(run): Use bin scripts defined in a workspace package before the w…
Browse files Browse the repository at this point in the history
…orkspace 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 yarnpkg#8590
  • Loading branch information
rally25rs committed Jul 6, 2024
1 parent 7cafa51 commit 49d1841
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 1 deletion.
15 changes: 15 additions & 0 deletions __tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -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",
"└─ [email protected]",
"info All dependencies",
"└─ [email protected]",
]
`;

exports[`should add package with no-lockfile option in front 1`] = `
Array [
"info No lockfile found.",
Expand Down
56 changes: 56 additions & 0 deletions __tests__/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> =>
runRun(
['eslint'],
{},
'issue-8590',
(config): ?Promise<void> => {
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<void> =>
runRunInWorkspacePackage(
'packages/workspace-a',
['eslint'],
{},
'issue-8590',
(config): ?Promise<void> => {
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<void> =>
runRunInWorkspacePackage(
'packages/workspace-b',
['eslint'],
{},
'issue-8590',
(config): ?Promise<void> => {
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<void> =>
runRunWithCustomShell('/usr/bin/dummy', ['start'], {}, 'script-shell', async (config): ?Promise<void> => {
const pkg = await fs.readJson(path.join(config.cwd, 'package.json'));
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions __tests__/fixtures/run/issue-8590/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "yarn-issue-8590",
"version": "1.0.0",
"main": "index.js",
"author": "yarn",
"license": "MIT",
"private": true,
"workspaces": {
"packages": [
"packages/*"
]
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-a",
"version": "1.0.0",
"dependencies": {
"eslint": "8.57.0"
}
}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "workspace-b",
"version": "1.0.0",
"dependencies": {
"eslint": "1.0.0"
}
}
6 changes: 5 additions & 1 deletion src/cli/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ export async function getBinEntries(config: Config): Promise<Map<string, string>
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));
if (!binEntries.has(name)) {
binEntries.set(name, path.join(binFolder, name));
}
}
}
}
Expand All @@ -81,6 +83,7 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg

for (const [name, loc] of await getBinEntries(config)) {
scripts.set(name, quoteForShell(loc));
console.log('name:', name, 'loc:', loc);
binCommands.add(name);
}

Expand All @@ -89,6 +92,7 @@ export async function run(config: Config, reporter: Reporter, flags: Object, arg
if (pkgScripts) {
for (const name of Object.keys(pkgScripts).sort()) {
scripts.set(name, pkgScripts[name] || '');
console.log('pkg name:', name, 'loc:', pkgScripts[name]);
pkgCommands.add(name);
}
}
Expand Down

0 comments on commit 49d1841

Please sign in to comment.