diff --git a/packages/sim-cli/src/runtime/request.test.ts b/packages/sim-cli/src/runtime/request.test.ts index 1bf955a1e16..f9a8355153e 100644 --- a/packages/sim-cli/src/runtime/request.test.ts +++ b/packages/sim-cli/src/runtime/request.test.ts @@ -205,6 +205,22 @@ describe('JSON flags that name a file', () => { }) rmSync(path) }) + + it.each([ + ['UTF-16LE, as a Windows PowerShell 5.1 redirect writes it', 'utf16le'], + ['UTF-8 with a byte-order mark', 'utf8'], + ] as const)('reads @path saved as %s', async (_, encoding) => { + const path = join(tmpdir(), `sim-cli-arg-${encoding}.json`) + writeFileSync(path, Buffer.from('{"name":"Café","state":{"blocks":{}}}\r\n', encoding)) + try { + expect(await coerce(`@${path}`, field, {}, 'workflow')).toEqual({ + name: 'Café', + state: { blocks: {} }, + }) + } finally { + rmSync(path) + } + }) }) describe('folder paths are typed by the name the app shows', () => { diff --git a/packages/sim-cli/src/runtime/request.ts b/packages/sim-cli/src/runtime/request.ts index 33cdb05d06a..e9cc0da29e9 100644 --- a/packages/sim-cli/src/runtime/request.ts +++ b/packages/sim-cli/src/runtime/request.ts @@ -223,7 +223,17 @@ function readArgumentDescriptor(descriptor: number): string { chunks.push(Buffer.from(buffer.subarray(0, read))) } - return Buffer.concat(chunks).toString('utf8') + return decodeArgumentBytes(Buffer.concat(chunks)) +} + +/** + * Decodes UTF-8, or UTF-16LE when the bytes start with its byte-order mark, and drops the mark. + * Windows PowerShell 5.1 writes `>` redirects as UTF-16LE and `Out-File -Encoding utf8` with a + * UTF-8 mark, so a file saved from `sim … > workflow.json` would otherwise not parse as JSON. + */ +function decodeArgumentBytes(bytes: Buffer): string { + const utf16le = bytes[0] === 0xff && bytes[1] === 0xfe + return new TextDecoder(utf16le ? 'utf-16le' : 'utf-8').decode(bytes) } /**