Skip to content

Commit 718c3fc

Browse files
kumavisarcanis
andauthored
fix(plugin-workspace-tools): properly report errors for all non-zero exit codes (#6535)
## What's the problem this PR addresses? `yarn workspaces foreach [...]` does not clearly indicate that there was a failure, or for what workspace there were non-zero exit codes. Before: (just says "done", but exit code is 1) ``` Done in 57s 886ms endo3 on  yarn-workspaces-for-scripts [!?] via  v18.20.4 took 58s ❯ echo $? 1 ``` After: ``` The command failed in workspace ses@workspace:packages/ses Failed with errors in 45s 837ms ``` ## How did you fix it? I used the `report.reportError` feature to report all the workspaces that had non-zero exit codes. The final exitCode is not changed in this PR. ## Checklist - [x] I have read the [Contributing Guide](https://yarnpkg.com/advanced/contributing). - [x] I have set the packages that need to be released for my changes to be effective. - [x] I will check that all automated PR checks pass before the PR gets reviewed. 🙏 @arcanis thank you for yarn 🙏 --------- Co-authored-by: Maël Nison <[email protected]>
1 parent f3962d5 commit 718c3fc

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

.yarn/versions/30ae755e.yml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
releases:
2+
"@yarnpkg/cli": patch
3+
"@yarnpkg/plugin-workspace-tools": patch
4+
5+
declined:
6+
- "@yarnpkg/plugin-compat"
7+
- "@yarnpkg/plugin-constraints"
8+
- "@yarnpkg/plugin-dlx"
9+
- "@yarnpkg/plugin-essentials"
10+
- "@yarnpkg/plugin-init"
11+
- "@yarnpkg/plugin-interactive-tools"
12+
- "@yarnpkg/plugin-nm"
13+
- "@yarnpkg/plugin-npm-cli"
14+
- "@yarnpkg/plugin-pack"
15+
- "@yarnpkg/plugin-patch"
16+
- "@yarnpkg/plugin-pnp"
17+
- "@yarnpkg/plugin-pnpm"
18+
- "@yarnpkg/plugin-stage"
19+
- "@yarnpkg/plugin-typescript"
20+
- "@yarnpkg/plugin-version"
21+
- "@yarnpkg/builder"
22+
- "@yarnpkg/core"
23+
- "@yarnpkg/doctor"
24+
- "@yarnpkg/pnp"

packages/plugin-workspace-tools/sources/commands/foreach.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ export default class WorkspacesForeachCommand extends BaseCommand {
405405
needsProcessing.delete(identHash);
406406
processing.delete(workspace.anchoredDescriptor.descriptorHash);
407407

408-
return exitCode;
408+
return {workspace, exitCode};
409409
}));
410410

411411
// If we're not executing processes in parallel we can just wait for it
@@ -424,13 +424,15 @@ export default class WorkspacesForeachCommand extends BaseCommand {
424424
return;
425425
}
426426

427-
const exitCodes: Array<number> = await Promise.all(commandPromises);
428-
const errorCode = exitCodes.find(code => code !== 0);
427+
const results: Array<{ workspace: Workspace, exitCode: number }> = await Promise.all(commandPromises);
428+
results.forEach(({workspace, exitCode}) => {
429+
if (exitCode !== 0) {
430+
report.reportError(MessageName.UNNAMED, `The command failed in workspace ${structUtils.prettyLocator(configuration, workspace.anchoredLocator)} with exit code ${exitCode}`);
431+
}
432+
});
429433

430-
// The order in which the exit codes will be processed is fairly
431-
// opaque, so better just return a generic "1" for determinism.
432-
if (finalExitCode === null)
433-
finalExitCode = typeof errorCode !== `undefined` ? 1 : finalExitCode;
434+
const exitCodes = results.map(result => result.exitCode);
435+
const errorCode = exitCodes.find(code => code !== 0);
434436

435437
if ((this.topological || this.topologicalDev) && typeof errorCode !== `undefined`) {
436438
report.reportError(MessageName.UNNAMED, `The command failed for workspaces that are depended upon by other workspaces; can't satisfy the dependency graph`);

0 commit comments

Comments
 (0)