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.
  • Loading branch information
forivall committed Oct 12, 2022
1 parent 8ab7f85 commit 0ff93dd
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();

1 comment on commit 0ff93dd

@vercel
Copy link

@vercel vercel bot commented on 0ff93dd Oct 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-git-master-nrwl.vercel.app
nx-dev-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.