Skip to content
Myr

Fix `execaSync()` writing decoded text instead of the output's bytes to files

Before adoption: this commit came in with the history the project adopted, so no landing records it.

With a `{file}`, `'inherit'` or file descriptor target, synchronous methods wrote the output decoded with the `encoding` option, which was then encoded again as UTF-8. That changed the bytes: invalid UTF-8 became U+FFFD, `utf16le` output became UTF-8, and `hex` or `base64` wrote that text instead of the bytes. Like asynchronous methods, the bytes are now written as is.

authored by Sindre Sorhus · committed by Sindre Sorhus · parent ecda56b · Browse files · 2 files+29 added−5 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−5 removed

  • tests:+17 added−0 removed
  • other:+12 added−5 removed

2 files+29 added−5 removed

modified:lib/io/output-sync.js+12 added-5 removed
modified:test/stdio/file-path-main.js+17 added
190 unmodified lines
191test('Can append to stdio[*]', () => testAppendFile(3, execa));192test('Can append to stdout - sync', () => testAppendFile(1, execaSync));193test('Can append to stderr - sync', () => testAppendFile(2, execaSync));194test('Can append to stdio[*] - sync', () => testAppendFile(3, execaSync));195196// The file receives the subprocess' bytes as is, whatever the `encoding` option, which only applies to `result.stdout`197const nonUtf8Bytes = new Uint8Array([0x61, 0x00, 0xE9, 0x00, 0xFF, 0x0A]);198199const testOutputFileEncoding = async (encoding, execaMethod) => {200 const filePath = tempfile();201 await execaMethod('stdin.js', {input: nonUtf8Bytes, stdout: {file: filePath}, encoding});202 assert.deepEqual(new Uint8Array(await readFile(filePath)), nonUtf8Bytes);203 await rm(filePath);204};205206test('Output files are not modified by encoding "utf8"', () => testOutputFileEncoding('utf8', execa));207test('Output files are not modified by encoding "utf16le"', () => testOutputFileEncoding('utf16le', execa));208test('Output files are not modified by encoding "hex"', () => testOutputFileEncoding('hex', execa));209test('Output files are not modified by encoding "utf8" - sync', () => testOutputFileEncoding('utf8', execaSync));210test('Output files are not modified by encoding "utf16le" - sync', () => testOutputFileEncoding('utf16le', execaSync));211test('Output files are not modified by encoding "hex" - sync', () => testOutputFileEncoding('hex', execaSync));