Skip to content

Commit 8628c6d

Browse files
committed
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.
1 parent 293ec1b commit 8628c6d

2 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎packages/sim-cli/src/runtime/request.test.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,22 @@ describe('JSON flags that name a file', () => {
205205
})
206206
rmSync(path)
207207
})
208+
209+
it.each([
210+
['UTF-16LE, as a Windows PowerShell 5.1 redirect writes it', 'utf16le'],
211+
['UTF-8 with a byte-order mark', 'utf8'],
212+
] as const)('reads @path saved as %s', async (_, encoding) => {
213+
const path = join(tmpdir(), `sim-cli-arg-${encoding}.json`)
214+
writeFileSync(path, Buffer.from('{"name":"Café","state":{"blocks":{}}}\r\n', encoding))
215+
try {
216+
expect(await coerce(`@${path}`, field, {}, 'workflow')).toEqual({
217+
name: 'Café',
218+
state: { blocks: {} },
219+
})
220+
} finally {
221+
rmSync(path)
222+
}
223+
})
208224
})
209225

210226
describe('folder paths are typed by the name the app shows', () => {

‎packages/sim-cli/src/runtime/request.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,17 @@ function readArgumentDescriptor(descriptor: number): string {
223223
chunks.push(Buffer.from(buffer.subarray(0, read)))
224224
}
225225

226-
return Buffer.concat(chunks).toString('utf8')
226+
return decodeArgumentBytes(Buffer.concat(chunks))
227+
}
228+
229+
/**
230+
* Decodes UTF-8, or UTF-16LE when the bytes start with its byte-order mark, and drops the mark.
231+
* Windows PowerShell 5.1 writes `>` redirects as UTF-16LE and `Out-File -Encoding utf8` with a
232+
* UTF-8 mark, so a file saved from `sim … > workflow.json` would otherwise not parse as JSON.
233+
*/
234+
function decodeArgumentBytes(bytes: Buffer): string {
235+
const utf16le = bytes[0] === 0xff && bytes[1] === 0xfe
236+
return new TextDecoder(utf16le ? 'utf-16le' : 'utf-8').decode(bytes)
227237
}
228238

229239
/**

0 commit comments

Comments
 (0)