Skip to content

Commit

Permalink
fix(core): drain stdout before exit in print-affected
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.
  • Loading branch information
forivall authored and AgentEnder committed Oct 12, 2022
1 parent 8ab7f85 commit 31ecffa
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
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
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 31ecffa

Please sign in to comment.