Files
apes-sidebar/src/open-external.ts
T
Menghuan1918 f6f7629b23 fix(open-external): Windows「在应用中打开」改为 cmd start → rundll32 候选链,全失败不再静默成功(#412)
现象(#412):Windows 上右键「在应用中打开」经常没反应。核实:`urlCommand` 的 win32 分支
只有 `rundll32.exe url.dll,FileProtocolHandler <url>` 一条候选,而 rundll32.exe 在任何
Windows 上都存在、spawn 必然成功 → 路由回 `{started:true}`,UI 拿不到任何失败信息。

修法:
① `urlCommand` → `urlCommands(...): ExternalCommand[]`(有序候选链):win32 为
   `cmd.exe /d /c start "" <url>` → `rundll32.exe url.dll,FileProtocolHandler <url>`
   (空首参是 start 的窗口标题,缺了它带引号的 URL 会被当成标题、只弹空控制台);
   darwin / linux / android / WSL 仍是单元素,语义逐字不变。
② URL 含 cmd 元字符(& % ^ ! | < > ")或控制字符时**跳过 cmd 分支**直接走 rundll32:
   Node 的 argv 引用拦不住 `cmd /c` 的第二次解析(%VAR% 展开、&/| 截断、^ 吞字符、"
   打乱引用),而 rundll32 以单个 argv 收到 URL、什么都不解释。
③ `launchExternal` 顺序尝试候选:只有 Node 发出 `spawn` 才算成功,同步抛错与 `error`
   事件都推进到下一候选;**全部候选失败**时抛 `SidebarError('internal', …)`,消息里逐个
   列出命令与底层报错(走既有 `{ok:false,error:{code,message}}` 信封)。
④ 调用方一并改形状:`EditorHost.openWith` 返回「是否被接受」(失败仍 console.error 留痕),
   `FileTree` 据此把失败渲染进既有 `openInAppFailed` 错误条——复用词条,未新增词典 key。

测试:
- tests/open-external.spec.ts:候选链顺序、元字符跳过规则、空格 URL 仍是单个 argv、
  失败回退顺序、全失败错误形状(含 `&` URL 只尝试 rundll32、reveal 分支的 path 措辞)。
- tests/open-external-windows.spec.ts(Windows lane,pnpm test:windows):只注入两个
  「不可能存在」的可执行名(`WindowsOpenerOptions` 测试缝),走**真实 spawn** 断言两条
  候选都失败时错误被回报而不是静默成功;**绝不拉起任何应用**(detached:true 会开真窗口,
  所以生产命令名一个都不在测试里出现)。
- tests/file-tree-open-with.spec.tsx / tests/editor-host.spec.tsx:被拒(返回 false)与
  抛错的打开都必须落进错误条,console-only 即红。

判别性(临时还原源码改动后跑新增用例,随后逐字恢复、md5 校验一致):
- 候选链退回单 rundll32(`if (false && …)`)→ 9 red(含 Windows lane 两条)
- spawn error 仍回 `{started:true}`(旧的「从不失败」契约)→ 6 red
- 去掉元字符跳过 → 11 red
- FileTree 不再上报失败 → 3 red
- EditorHost 恢复吞错 → 1 red

限制(写入设计文档):`start` 与 rundll32 在 handler 忽略 URL 时都退出 0,
「已启动但没打开」仍不可从退出码探测。
2026-10-05 22:58:01 +08:00

333 lines
13 KiB
TypeScript

/**
* External open actions for the file tree's "open with" menu: hand a path to
* the OS file manager (reveal/select) or launch a URL scheme's registered
* handler (vscode://, cursor://, zed://, custom schemes).
*
* The client runs in a browser / DSH Desktop renderer where a raw `vscode://`
* navigation is unreliable, so both actions fan out through this host route
* and spawn the platform opener with an argv array (no shell interpolation).
* WSL is special: Node reports `linux`, while the actual desktop handlers live
* on Windows, so its commands cross the interop boundary explicitly.
*/
import { execFileSync, spawn } from 'node:child_process'
import { readFileSync } from 'node:fs'
import { parentOf, requireAbsolute } from './fs-tree.ts'
import { SidebarError } from './wire.ts'
/** The two external open actions the route accepts. */
export type OpenExternalAction = 'reveal' | 'url'
/** One platform opener invocation (argv array — never a shell string). */
export interface ExternalCommand {
command: string
args: string[]
}
/** Injectable WSL command seams keep the command builders pure in unit tests. */
export interface WslCommandOptions {
wsl?: boolean
toWindowsPath?: (path: string) => string
windowsExecutable?: (path: string) => string
}
/**
* Injectable Windows opener NAMES — a test-only seam, never set in production.
*
* Pointing them at executables that cannot exist lets the REAL `spawn` drive
* the whole fallback chain and its failure reporting on a real Windows runtime
* (see tests/open-external-windows.spec.ts) without launching an application:
* a binary that does not exist cannot start a browser, an editor or a console
* window on the runner.
*/
export interface WindowsOpenerOptions {
/** The `cmd.exe` that runs the `start` branch. */
cmdExecutable?: string
/** The `rundll32.exe` that runs the `url.dll,FileProtocolHandler` branch. */
rundll32Executable?: string
}
/** Every command-builder seam: WSL path/executable projection + Windows opener names. */
export type OpenerCommandOptions = WslCommandOptions & WindowsOpenerOptions
/** Runtime seams used only to make launch/error behavior testable off-host. */
export interface LaunchExternalOptions {
platform?: NodeJS.Platform
wsl?: boolean
distroName?: string
commandOptions?: Omit<OpenerCommandOptions, 'wsl'>
spawn?: typeof spawn
}
/** True when this Linux host is backed by Windows Subsystem for Linux. */
export function isWslRuntime(): boolean {
if (process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP) return true
try {
return /microsoft|wsl/i.test(readFileSync('/proc/version', 'utf8'))
} catch {
return false
}
}
/** Run WSL's built-in path translator by absolute path so
* `appendWindowsPath=false` cannot make the helper disappear from PATH. */
function wslPath(mode: '-u' | '-w', value: string): string {
const translated = execFileSync('/usr/bin/wslpath', [mode, value], { encoding: 'utf8' }).trim()
if (translated === '') throw new Error(`wslpath returned no translation for ${value}`)
return translated
}
/** Linux/WSL absolute path -> Windows path understood by Explorer. */
function windowsPathOf(path: string): string {
return wslPath('-w', path)
}
/** Windows executable path -> its WSL mount path, without relying on PATH. */
function windowsExecutableOf(path: string): string {
return wslPath('-u', path)
}
/** Reveal/select a path in the OS file manager. On plain Linux there is no
* common select protocol — the containing directory is opened instead.
* Under WSL the desktop file manager is Windows Explorer. */
export function revealCommand(
path: string,
platform: NodeJS.Platform = process.platform,
options: WslCommandOptions = {},
): ExternalCommand {
switch (platform) {
case 'darwin':
return { command: 'open', args: ['-R', path] }
// Explorer expects `/select,<path>` as one argument. Keep the spawn
// shell-free: a command shell would reinterpret valid path characters.
case 'win32':
return { command: 'explorer.exe', args: [`/select,${path}`] }
// Termux (the dominant Node runtime reporting platform "android") has no
// desktop file manager and no xdg-open; `termux-open` hands the path to
// Android's system open-with chooser. Fall through to the parent dir like
// Linux: Android has no select protocol either.
case 'android': {
const parent = parentOf(path)
return { command: 'termux-open', args: [parent ?? path] }
}
default: {
if (options.wsl) {
const toWindowsPath = options.toWindowsPath ?? windowsPathOf
const windowsExecutable = options.windowsExecutable ?? windowsExecutableOf
return {
command: windowsExecutable('C:\\Windows\\explorer.exe'),
args: [`/select,${toWindowsPath(path)}`],
}
}
const parent = parentOf(path)
return { command: 'xdg-open', args: [parent ?? path] }
}
}
}
/**
* Characters `cmd.exe` interprets after Node has quoted each argv element for
* CreateProcess: `cmd /c` parses the command line AGAIN, so `%VAR%` expands,
* `&`/`|` split the command, `<`/`>` redirect, `^` escapes the next character,
* `!` triggers delayed expansion and `"` re-balances the quoting. A URL
* carrying any of them never reaches the `start` branch — it goes straight to
* rundll32, which receives the URL as one argv element and interprets nothing.
*/
const CMD_METACHARACTERS = /[&%^!|<>"]/
/** C0/C1 control characters count as metacharacters too: `cmd.exe` treats a
* raw CR/LF as a command separator, and `validateExternalUrl` returns the
* RAW string (`new URL` strips those characters, so validation passes). */
const CMD_CONTROL_CHARACTERS = /\p{Cc}/u
/** Whether `cmd.exe` would reinterpret any part of this URL — i.e. the `start`
* branch must be skipped and rundll32 used directly. */
function cmdUnsafe(url: string): boolean {
return CMD_METACHARACTERS.test(url) || CMD_CONTROL_CHARACTERS.test(url)
}
/**
* The URL openers to try, best first — the Windows chain exists because the
* `start` route and the rundll32 route fail in disjoint situations (#412:
* rundll32 alone reported `{ started: true }` while nothing opened).
*
* Every entry is an argv array (never a shell string). macOS/Linux keep their
* single opener and WSL keeps its single absolute-path Windows dispatcher (the
* registered handlers live on the Windows side of the interop boundary): this
* chain is a Windows fix, so their behavior is unchanged.
*/
export function urlCommands(
url: string,
platform: NodeJS.Platform = process.platform,
options: OpenerCommandOptions = {},
): ExternalCommand[] {
switch (platform) {
case 'darwin':
return [{ command: 'open', args: [url] }]
case 'win32': {
const chain: ExternalCommand[] = []
if (!cmdUnsafe(url)) {
// `start "" <url>`: the empty argument is the window TITLE. Without it
// `start` treats the quoted URL as the title and opens a bare console
// window instead of the registered handler.
chain.push({ command: options.cmdExecutable ?? 'cmd.exe', args: ['/d', '/c', 'start', '', url] })
}
// url.dll,FileProtocolHandler launches the registered protocol handler
// through ShellExecute (the route that historically worked for schemes
// `start` refused, and vice versa).
chain.push({
command: options.rundll32Executable ?? 'rundll32.exe',
args: ['url.dll,FileProtocolHandler', url],
})
return chain
}
// Termux: `termux-open-url` is the Android intent dispatcher for URLs
// (termux-open would route through the content chooser first).
case 'android':
return [{ command: 'termux-open-url', args: [url] }]
default:
if (options.wsl) {
const windowsExecutable = options.windowsExecutable ?? windowsExecutableOf
return [{
command: windowsExecutable('C:\\Windows\\System32\\rundll32.exe'),
args: ['url.dll,FileProtocolHandler', url],
}]
}
return [{ command: 'xdg-open', args: [url] }]
}
}
/** Convert the built-in VSCode-family local-file URL to its Remote-WSL form.
* A POSIX path produces `scheme://file//...`; keep one leading slash when it
* moves behind the `wsl+<distro>` authority. Other custom schemes are left
* alone and merely dispatched through the Windows protocol handler. */
export function wslRemoteEditorUrl(url: string, distroName: string): string {
const match = /^(vscode(?:-insiders)?|cursor):\/\/file(\/\/.*)$/i.exec(url)
if (match === null) return url
const scheme = match[1]
const remotePath = match[2]
if (scheme === undefined || remotePath === undefined) return url
const distro = distroName.trim()
if (distro === '') {
throw new SidebarError('internal', 'WSL_DISTRO_NAME is unavailable; cannot build a Remote-WSL editor URL', 500)
}
return `${scheme}://vscode-remote/wsl+${distro}${remotePath.slice(1)}`
}
/** Validate a URL-scheme open target: a parseable custom-scheme URL (never
* http/https — those would only dump the URL into a browser tab). */
export function validateExternalUrl(raw: string): string {
if (!/^[a-z][a-z0-9+.-]*:\/\//i.test(raw)) {
throw new SidebarError('bad-request', 'url must be a custom-scheme URL')
}
let url: URL
try {
url = new URL(raw)
} catch {
throw new SidebarError('bad-request', 'invalid url')
}
if (url.protocol === 'http:' || url.protocol === 'https:') {
throw new SidebarError('bad-request', 'only custom-scheme urls can be opened externally')
}
return raw
}
function messageOf(error: unknown): string {
return error instanceof Error ? error.message : String(error)
}
/** One attempted launch that failed; kept for the aggregated error message. */
interface LaunchAttemptFailure {
command: string
error: unknown
}
/** The structured failure of an action whose EVERY opener candidate died:
* `internal` on the wire, with each command and its underlying message named
* so the UI (and a bug report) can carry the real cause. */
function launchFailure(action: OpenExternalAction, failures: readonly LaunchAttemptFailure[]): SidebarError {
const subject = action === 'url' ? 'url' : 'path'
const attempts = failures.map(f => `"${f.command}" (${messageOf(f.error)})`).join('; ')
return new SidebarError('internal', `failed to launch external opener for the ${subject}: ${attempts}`, 500)
}
/**
* Launch the first candidate that actually starts, in order.
*
* Success is reported only after Node emits `spawn`; a synchronous throw or an
* `error` event (ENOENT, EACCES, …) advances to the next candidate. When every
* candidate fails the promise rejects with a structured `SidebarError` naming
* them all — the route must never answer `{ started: true }` for an open that
* launched nothing (#412: that silent success is why the menu "did nothing").
*
* Note what this can and cannot see: a detached candidate that starts but
* whose handler ignores the URL exits 0 (`start` and rundll32 both do), so the
* chain reports everything it can observe — the launch itself.
*/
function launchChain(
action: OpenExternalAction,
chain: readonly ExternalCommand[],
spawnExternal: typeof spawn,
): Promise<{ started: true }> {
const failures: LaunchAttemptFailure[] = []
return new Promise((resolve, reject) => {
const attempt = (index: number): void => {
const spec = chain[index]
if (spec === undefined) {
reject(launchFailure(action, failures))
return
}
let child: ReturnType<typeof spawn>
try {
child = spawnExternal(spec.command, spec.args, { detached: true, stdio: 'ignore' })
} catch (error) {
failures.push({ command: spec.command, error })
attempt(index + 1)
return
}
let settled = false
child.once('spawn', () => {
if (settled) return
settled = true
resolve({ started: true })
})
child.once('error', (error) => {
if (settled) return
settled = true
failures.push({ command: spec.command, error })
attempt(index + 1)
})
child.unref()
}
attempt(0)
})
}
/**
* Launch one external open action detached from the host. Both actions fan out
* through an ordered candidate chain (one entry on macOS/Linux/Android/WSL, the
* two Windows URL routes on win32) and reject with the aggregated failure when
* none of them starts.
*/
export function launchExternal(
action: OpenExternalAction,
value: string,
options: LaunchExternalOptions = {},
): Promise<{ started: true }> {
const platform = options.platform ?? process.platform
const wsl = options.wsl ?? (platform === 'linux' && isWslRuntime())
const commandOptions: OpenerCommandOptions = { ...options.commandOptions, wsl }
// Validation stays synchronous and ahead of every spawn: a relative path or
// a non-scheme URL throws here, so nothing is launched for a bad request.
let chain: ExternalCommand[]
if (action === 'reveal') {
chain = [revealCommand(requireAbsolute(value), platform, commandOptions)]
} else {
let url = validateExternalUrl(value)
if (wsl) url = wslRemoteEditorUrl(url, options.distroName ?? process.env.WSL_DISTRO_NAME ?? '')
chain = urlCommands(url, platform, commandOptions)
}
return launchChain(action, chain, options.spawn ?? spawn)
}