From 8628c6dec4d9c8c1836a3734fbe662e6dcc46dd0 Mon Sep 17 00:00:00 2001 From: b84330808 Date: Sun, 22 Nov 2026 03:56:25 +0900 Subject: [PATCH] fix(cli): read @file arguments saved as UTF-16LE or with a byte-order mark Windows PowerShell 5.1 writes `>` redirects as UTF-16LE with a byte-order mark, and `Out-File -Encoding utf8` writes UTF-8 with one. The documented `sim workflows export ... > workflow.json` then `workflows import-preview --workflow @workflow.json` failed there with "--workflow must be valid JSON". @file and @- input is now decoded as UTF-16LE when it starts with that mark, and a leading mark is dropped either way, as the embedded reader's TextDecoder already does. --- packages/sim-cli/src/runtime/request.test.ts | 16 ++++++++++++++++ packages/sim-cli/src/runtime/request.ts | 12 +++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) 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) } /**