Skip to content
Merged
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
9 changes: 9 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,15 @@ export class ExecProcess implements Result {
if (handle.stdin) {
const {stdin} = options;

// A child may exit without reading all of its input (e.g. `head`),
// which surfaces as EPIPE here (EOF on Windows). That is not a failure
// of ours.
handle.stdin.on('error', (err: NodeJS.ErrnoException) => {
if (err.code !== 'EPIPE' && err.code !== 'EOF') {
this._thrownError = err;
}
});

if (typeof stdin === 'string') {
handle.stdin.end(stdin);
} else {
Expand Down
9 changes: 9 additions & 0 deletions src/test/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,15 @@ describe('exec (async)', () => {
expect(result.exitCode).toBe(0);
});

test('does not crash when the process exits without reading stdin', async () => {
// Larger than the pipe buffer, so the write is still pending on exit
const result = await x('node', ['-e', 'process.exit(0)'], {
stdin: 'x'.repeat(4 * 1024 * 1024)
});

expect(result.exitCode).toBe(0);
});

test('supports stdin passed as another process (Result)', async () => {
const proc = x('node', ['-e', "process.stdout.write('foo\\nbar')"]);
const result = await x(
Expand Down