Skip to content
Closed
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
13 changes: 12 additions & 1 deletion packages/core/src/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,17 @@ function info(file: string): Item {
}
}

// Windows PowerShell 5.1 encodes its own output with the legacy OEM code page
// (e.g. CP936 on zh-CN systems) when stdout is redirected, while opencode
// decodes process output as UTF-8. Force UTF-8 before the user command runs.
const PS_UTF8_PREAMBLE =
"[Console]::OutputEncoding = [System.Text.UTF8Encoding]::new(); $OutputEncoding = [System.Text.UTF8Encoding]::new(); "

export function psCommand(command: string) {
if (process.platform !== "win32") return command
return PS_UTF8_PREAMBLE + command
}

export function args(file: string, command: string, cwd: string) {
const n = name(file)
if (n === "nu" || n === "fish") return ["-c", command]
Expand Down Expand Up @@ -195,7 +206,7 @@ export function args(file: string, command: string, cwd: string) {
]
}
if (n === "cmd") return ["/c", command]
if (ps(file)) return ["-NoProfile", "-Command", command]
if (ps(file)) return ["-NoProfile", "-Command", psCommand(command)]
return ["-c", command]
}

Expand Down
17 changes: 17 additions & 0 deletions packages/core/test/shell.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,23 @@ describe("shell", () => {
expect(zsh.at(-1)).toBe("/tmp")
})

test("psCommand wraps command with UTF-8 preamble on Windows", () => {
const result = Shell.psCommand("echo hi")
if (process.platform === "win32") {
expect(result.startsWith("[Console]::OutputEncoding")).toBe(true)
expect(result.endsWith("echo hi")).toBe(true)
} else {
expect(result).toBe("echo hi")
}
})

test("applies psCommand to PowerShell args", () => {
const psArgs = Shell.args("powershell", "echo hi", "C:\\tmp")
expect(psArgs[0]).toBe("-NoProfile")
expect(psArgs[1]).toBe("-Command")
expect(String(psArgs.at(-1))).toContain("echo hi")
})

if (process.platform === "win32") {
test("rejects blacklisted shells case-insensitively", async () => {
await withShell("NU.EXE", async () => {
Expand Down
16 changes: 10 additions & 6 deletions packages/opencode/src/tool/shell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,16 @@ const ask = Effect.fn("ShellTool.ask")(function* (ctx: Tool.Context, scan: Scan,

function cmd(shell: string, command: string, cwd: string, env: NodeJS.ProcessEnv) {
if (process.platform === "win32" && Shell.ps(shell)) {
return ChildProcess.make(shell, ["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", command], {
cwd,
env,
stdin: "ignore",
detached: false,
})
return ChildProcess.make(
shell,
["-NoLogo", "-NoProfile", "-NonInteractive", "-Command", Shell.psCommand(command)],
{
cwd,
env,
stdin: "ignore",
detached: false,
},
)
}

return ChildProcess.make(command, [], {
Expand Down
Loading