Skip to content

Don't try to run scripts that aren't defined #6841

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

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,16 @@
Please add one entry in this file for each change in Yarn's behavior. Use the same format for all entries, including the third-person verb. Make sure you don't add more than one line of text to keep it clean. Thanks!

## Master
- Stop `yarn workspaces run` error if command not defined in workspace

- Improves PnP compatibility with Node 6

[#6871](https://github.com/yarnpkg/yarn/pull/6871) - [**Robert Jackson**](https://github.com/rwjblue)

- Fixes PnP detection with workspaces (`installConfig` is now read at the top-level)

[#6878](https://github.com/yarnpkg/yarn/pull/6878) - [**Maël Nison**](https://twitter.com/arcanis)

- Fixes an interaction between `yarn pack` and bundled dependencies

[#6908](https://github.com/yarnpkg/yarn/pull/6908) - [**Travis Hoover**](https://twitter.com/thoov)
Expand Down
12 changes: 11 additions & 1 deletion __tests__/commands/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('workspaces info should list the workspaces', (): Promise<void> => {
});
});

test('workspaces run should spawn command for each workspace', (): Promise<void> => {
test('workspaces run should spawn command for each workspace with that script defined', (): Promise<void> => {
const originalArgs = ['run', 'script', 'arg1', '--flag1'];
return runWorkspaces({originalArgs}, ['run', 'script', 'arg1', '--flag1'], 'run-basic', config => {
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'script', 'arg1', '--flag1'], {
Expand All @@ -61,3 +61,13 @@ test('workspaces run should spawn command for each workspace', (): Promise<void>
});
});
});

test('workspaces run should not report error if command is not defined for a workspace', (): Promise<void> => {
const originalArgs = ['run', 'workspace-1-only-script', 'arg1', '--flag1'];
return runWorkspaces({originalArgs}, ['run', 'workspace-1-only-script', 'arg1', '--flag1'], 'run-basic', config => {
expect(spawn).toHaveBeenCalledWith(NODE_BIN_PATH, [YARN_BIN_PATH, 'workspace-1-only-script', 'arg1', '--flag1'], {
stdio: 'inherit',
cwd: path.join(fixturesLoc, 'run-basic', 'packages', 'workspace-child-1'),
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"scripts": {
"prescript": "echo workspace-1 prescript",
"script": "echo workspace-1 script",
"postscript": "echo workspace-1 postscript"
"postscript": "echo workspace-1 postscript",
"workspace-1-only-script": "echo workspace-1-only script"
}
}
3 changes: 3 additions & 0 deletions packages/pkg-tests/pkg-tests-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@
"super-resolve": "^1.0.0",
"tar-fs": "^1.16.0",
"tmp": "^0.0.33"
},
"scripts": {
"script": "echo pkg-tests-core script"
}
}
24 changes: 23 additions & 1 deletion src/cli/commands/workspaces.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,30 @@ export async function runScript(config: Config, reporter: Reporter, flags: Objec
try {
const [_, ...rest] = flags.originalArgs || [];

// first arg not prefixed by `-` is the script that will be run
let scriptName = '';
for (const arg of rest) {
if (arg.startsWith('-')) {
continue;
}
scriptName = arg;
break;
}

for (const workspaceName of Object.keys(workspaces)) {
const {loc} = workspaces[workspaceName];
const {loc, manifest} = workspaces[workspaceName];

// allow for `yarn workspaces run -s exec pwd`
if (scriptName !== 'exec') {
// Let the user know if the script doesn't exist
if (!manifest.scripts) {
reporter.warn(`No scripts defined in workspace ${workspaceName}.`);
continue;
} else if (!manifest.scripts[scriptName]) {
reporter.warn(`${scriptName} not defined in workspace ${workspaceName}.`);
continue;
}
}

await child.spawn(NODE_BIN_PATH, [YARN_BIN_PATH, ...rest], {
stdio: 'inherit',
Expand Down