fix(desktop): run WSL commands with --exec so inline scripts are not pre-expanded (#49902)

This commit is contained in:
Luke Parker
2026-09-19 14:36:04 +10:00
committed by GitHub
parent 56816621f8
commit e50d845451
2 changed files with 38 additions and 1 deletions
@@ -0,0 +1,35 @@
import { expect, test } from "bun:test"
import { listInstalledWslDistros, probeWslRuntime, runWslSh, wslArgs } from "./runtime"
test("wslArgs bypasses the distro default shell", () => {
expect(wslArgs(["sh", "-lc", 'printf "%s\\n" "$HOME"'], "Debian")).toEqual([
"-d",
"Debian",
"--exec",
"sh",
"-lc",
'printf "%s\\n" "$HOME"',
])
expect(wslArgs(["bash", "-se"], "Debian", "root")).toEqual([
"-d",
"Debian",
"--user",
"root",
"--exec",
"bash",
"-se",
])
})
// Exercise the real wsl.exe argv path: with `--`, WSL routes the command line through the distro's
// default shell, which expands `$cli` to "" before `sh` runs the script (#48640).
const distro =
process.platform === "win32" && (await probeWslRuntime()).available
? (await listInstalledWslDistros().catch(() => [])).find((item) => item.isDefault)?.name
: undefined
test.skipIf(!distro)("inline scripts keep their own variable expansion inside WSL", async () => {
const result = await runWslSh(['cli="from-script"', 'printf "%s\\n" "$cli"'].join("\n"), distro)
expect(result.code).toBe(0)
expect(result.stdout.trim()).toBe("from-script")
})
+3 -1
View File
@@ -38,7 +38,9 @@ const DEFAULT_WSL_TIMEOUT_MS = 20_000
const DEFAULT_WSL_INSTALL_TIMEOUT_MS = 15 * 60_000
export function wslArgs(args: string[], distro?: string | null, user?: string | null) {
return [...(distro ? ["-d", distro] : []), ...(user ? ["--user", user] : []), "--", ...args]
// `--` hands the command line to the distro's default shell, which expands `$VAR` and `$(...)`
// inside our inline scripts before `sh -lc` ever runs them. `--exec` runs the command directly.
return [...(distro ? ["-d", distro] : []), ...(user ? ["--user", user] : []), "--exec", ...args]
}
export function runWsl(args: string[], opts: RunWslOptions = {}) {