Skip to content

Commit

Permalink
fix(core): drain stdout before exit in print-affected (#12559)
Browse files Browse the repository at this point in the history
Piping a large print-affected output to another command, such as cat or jq, resulted in broken json
output as the node process can exit before the output is fully written. The process.stdout stream
needs to wait to drain before process.exit.

(cherry picked from commit 0ff93dd)
  • Loading branch information
forivall authored and FrozenPandaz committed Oct 13, 2022
1 parent 64103af commit 731f839
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/command-line/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ export async function affected(
break;
}
}
await output.drain();
} catch (e) {
printError(e, args.verbose);
process.exit(1);
Expand Down
10 changes: 10 additions & 0 deletions packages/nx/src/utils/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,16 @@ class CLIOutput {

this.addNewline();
}

drain(): Promise<void> {
return new Promise((resolve) => {
if (process.stdout.writableNeedDrain) {
process.stdout.once('drain', resolve);
} else {
resolve();
}
});
}
}

export const output = new CLIOutput();

0 comments on commit 731f839

Please sign in to comment.