现象(#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,
「已启动但没打开」仍不可从退出码探测。
306 lines
13 KiB
TypeScript
306 lines
13 KiB
TypeScript
/**
|
|
* Host external-open helpers: per-platform opener commands (pure, injected
|
|
* platform) and the URL validation that guards the spawn route. The actual
|
|
* OS handlers are not launched in unit tests.
|
|
*/
|
|
import { EventEmitter } from 'node:events'
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import {
|
|
launchExternal,
|
|
revealCommand,
|
|
urlCommands,
|
|
validateExternalUrl,
|
|
wslRemoteEditorUrl,
|
|
} from '../src/open-external.ts'
|
|
import { SidebarError } from '../src/wire.ts'
|
|
|
|
const wslCommands = {
|
|
wsl: true,
|
|
toWindowsPath: (path: string): string => path === '/a/b.txt' ? 'C:\\a\\b.txt' : path,
|
|
windowsExecutable: (path: string): string => path.endsWith('explorer.exe')
|
|
? '/windows/explorer.exe'
|
|
: '/windows/System32/rundll32.exe',
|
|
}
|
|
|
|
describe('revealCommand', () => {
|
|
it('darwin: `open -R <path>` selects the file in Finder', () => {
|
|
expect(revealCommand('/a/b.txt', 'darwin')).toEqual({ command: 'open', args: ['-R', '/a/b.txt'] })
|
|
})
|
|
|
|
it('win32: passes /select,<path> as one shell-free Explorer argument', () => {
|
|
expect(revealCommand('C:\\work\\two words\\a.txt', 'win32')).toEqual({
|
|
command: 'explorer.exe',
|
|
args: ['/select,C:\\work\\two words\\a.txt'],
|
|
})
|
|
})
|
|
|
|
it('linux: `xdg-open` opens the containing directory (no common select protocol)', () => {
|
|
expect(revealCommand('/a/b.txt', 'linux')).toEqual({ command: 'xdg-open', args: ['/a'] })
|
|
expect(revealCommand('/', 'linux')).toEqual({ command: 'xdg-open', args: ['/'] })
|
|
})
|
|
|
|
it('android (Termux): `termux-open` hands the parent dir to the Android chooser', () => {
|
|
expect(revealCommand('/a/b.txt', 'android')).toEqual({ command: 'termux-open', args: ['/a'] })
|
|
expect(revealCommand('/', 'android')).toEqual({ command: 'termux-open', args: ['/'] })
|
|
})
|
|
|
|
it('wsl: translates the path and selects it with an absolute Windows Explorer command', () => {
|
|
expect(revealCommand('/a/b.txt', 'linux', wslCommands)).toEqual({
|
|
command: '/windows/explorer.exe',
|
|
args: ['/select,C:\\a\\b.txt'],
|
|
})
|
|
})
|
|
})
|
|
|
|
describe('urlCommands', () => {
|
|
it('darwin: `open <url>` launches the registered protocol handler', () => {
|
|
expect(urlCommands('vscode://file/x', 'darwin')).toEqual([{ command: 'open', args: ['vscode://file/x'] }])
|
|
})
|
|
|
|
it('win32: `cmd /d /c start "" <url>` first, rundll32 as the fallback', () => {
|
|
expect(urlCommands('cursor://file/x', 'win32')).toEqual([
|
|
{ command: 'cmd.exe', args: ['/d', '/c', 'start', '', 'cursor://file/x'] },
|
|
{ command: 'rundll32.exe', args: ['url.dll,FileProtocolHandler', 'cursor://file/x'] },
|
|
])
|
|
})
|
|
|
|
it('win32: a URL with spaces stays ONE argv element (no shell string is built)', () => {
|
|
expect(urlCommands('cursor://file/C:/Program Files/a.ts', 'win32')[0]).toEqual({
|
|
command: 'cmd.exe',
|
|
args: ['/d', '/c', 'start', '', 'cursor://file/C:/Program Files/a.ts'],
|
|
})
|
|
})
|
|
|
|
// `cmd /c` re-parses the command line after Node's quoting: an unskipped
|
|
// metacharacter would be expanded (`%VAR%`), split (`&`/`|`), escaped (`^`)
|
|
// or re-quoted (`"`) instead of reaching the handler verbatim.
|
|
it.each([
|
|
['&', 'myapp://file/a&b.ts'],
|
|
['%', 'myapp://file/%PATH%/a.ts'],
|
|
['^', 'myapp://file/a^b.ts'],
|
|
['!', 'myapp://file/!x!/a.ts'],
|
|
['|', 'myapp://file/a|b.ts'],
|
|
['<', 'myapp://file/a<b.ts'],
|
|
['>', 'myapp://file/a>b.ts'],
|
|
['"', 'myapp://file/a"b.ts'],
|
|
])('win32: skips the cmd branch for a URL carrying %s (rundll32 only)', (_char, url) => {
|
|
expect(urlCommands(url, 'win32')).toEqual([
|
|
{ command: 'rundll32.exe', args: ['url.dll,FileProtocolHandler', url] },
|
|
])
|
|
})
|
|
|
|
it('win32: skips the cmd branch for a URL carrying a raw control character', () => {
|
|
// `new URL` strips CR/LF, but validateExternalUrl returns the RAW string,
|
|
// and cmd treats a newline as a command separator.
|
|
const url = 'myapp://file/a\n--x'
|
|
expect(urlCommands(url, 'win32').map(spec => spec.command)).toEqual(['rundll32.exe'])
|
|
})
|
|
|
|
it('win32: the opener names are injectable (the Windows-lane failure seam)', () => {
|
|
expect(urlCommands('zed://file/x', 'win32', {
|
|
cmdExecutable: 'missing-cmd-412.exe',
|
|
rundll32Executable: 'missing-rundll-412.exe',
|
|
})).toEqual([
|
|
{ command: 'missing-cmd-412.exe', args: ['/d', '/c', 'start', '', 'zed://file/x'] },
|
|
{ command: 'missing-rundll-412.exe', args: ['url.dll,FileProtocolHandler', 'zed://file/x'] },
|
|
])
|
|
})
|
|
|
|
it('linux: `xdg-open <url>` launches the registered protocol handler', () => {
|
|
expect(urlCommands('zed://file/x', 'linux')).toEqual([{ command: 'xdg-open', args: ['zed://file/x'] }])
|
|
})
|
|
|
|
it('android (Termux): `termux-open-url <url>` dispatches the Android intent', () => {
|
|
expect(urlCommands('vscode://file/x', 'android')).toEqual([{ command: 'termux-open-url', args: ['vscode://file/x'] }])
|
|
})
|
|
|
|
it('wsl: dispatches custom schemes through the absolute Windows URL handler', () => {
|
|
expect(urlCommands('zed://file//home/u/f.ts', 'linux', wslCommands)).toEqual([{
|
|
command: '/windows/System32/rundll32.exe',
|
|
args: ['url.dll,FileProtocolHandler', 'zed://file//home/u/f.ts'],
|
|
}])
|
|
})
|
|
})
|
|
|
|
describe('wslRemoteEditorUrl', () => {
|
|
it('converts VS Code and Cursor POSIX file URLs to Remote-WSL URLs', () => {
|
|
expect(wslRemoteEditorUrl('vscode://file//home/u/f.ts', 'Ubuntu-22.04'))
|
|
.toBe('vscode://vscode-remote/wsl+Ubuntu-22.04/home/u/f.ts')
|
|
expect(wslRemoteEditorUrl('cursor://file//mnt/d/dev/f.ts', 'Ubuntu-22.04'))
|
|
.toBe('cursor://vscode-remote/wsl+Ubuntu-22.04/mnt/d/dev/f.ts')
|
|
})
|
|
|
|
it('leaves non-VSCode-family schemes unchanged', () => {
|
|
expect(wslRemoteEditorUrl('zed://file//home/u/f.ts', 'Ubuntu-22.04'))
|
|
.toBe('zed://file//home/u/f.ts')
|
|
})
|
|
|
|
it('rejects a Remote-WSL conversion when the distro name is unavailable', () => {
|
|
expect(() => wslRemoteEditorUrl('vscode://file//home/u/f.ts', '')).toThrow(SidebarError)
|
|
})
|
|
})
|
|
|
|
describe('validateExternalUrl', () => {
|
|
it('accepts custom-scheme URLs (incl. the SSH-remote form)', () => {
|
|
expect(validateExternalUrl('vscode://vscode-remote/ssh-remote+dev/home/u/f.ts'))
|
|
.toBe('vscode://vscode-remote/ssh-remote+dev/home/u/f.ts')
|
|
expect(validateExternalUrl('myapp://file/{path}')).toBe('myapp://file/{path}')
|
|
})
|
|
|
|
it('rejects http/https (only custom schemes make sense here)', () => {
|
|
expect(() => validateExternalUrl('https://example.com')).toThrow(SidebarError)
|
|
expect(() => validateExternalUrl('http://example.com')).toThrow(SidebarError)
|
|
})
|
|
|
|
it('rejects non-URL / non-`scheme://` strings', () => {
|
|
expect(() => validateExternalUrl('/home/u/f.ts')).toThrow(SidebarError)
|
|
expect(() => validateExternalUrl('a:file/x')).toThrow(SidebarError)
|
|
expect(() => validateExternalUrl('')).toThrow(SidebarError)
|
|
})
|
|
})
|
|
|
|
/** One recorded spawn call plus the fake child it handed back. */
|
|
interface RecordedSpawn {
|
|
command: string
|
|
args: readonly string[]
|
|
child: EventEmitter
|
|
}
|
|
|
|
/**
|
|
* A `spawn` seam that records the argv of every ATTEMPT and never starts a
|
|
* process: the caller decides each attempt's outcome with `settle()`, so the
|
|
* chain's order, its fallback and its terminal error shape are all observable.
|
|
*/
|
|
function recordingSpawn(): {
|
|
spawn: typeof import('node:child_process').spawn
|
|
calls: RecordedSpawn[]
|
|
unref: ReturnType<typeof vi.fn>
|
|
settle: (index: number, outcome: 'spawn' | 'error', message?: string) => void
|
|
} {
|
|
const calls: RecordedSpawn[] = []
|
|
const unref = vi.fn()
|
|
const spawnFn = ((command: string, args: readonly string[]) => {
|
|
const child = new EventEmitter()
|
|
Object.assign(child, { unref })
|
|
calls.push({ command, args, child })
|
|
return child
|
|
}) as unknown as typeof import('node:child_process').spawn
|
|
const settle = (index: number, outcome: 'spawn' | 'error', message = 'spawn failed'): void => {
|
|
const call = calls[index]
|
|
if (call === undefined) throw new Error(`no spawn attempt #${index} (got ${calls.length})`)
|
|
if (outcome === 'spawn') call.child.emit('spawn')
|
|
else call.child.emit('error', Object.assign(new Error(message), { code: 'ENOENT' }))
|
|
}
|
|
return { spawn: spawnFn, calls, unref, settle }
|
|
}
|
|
|
|
describe('launchExternal validation and spawn lifecycle', () => {
|
|
it('rejects relative reveal paths before anything is spawned', () => {
|
|
expect(() => launchExternal('reveal', 'relative/path')).toThrow(SidebarError)
|
|
})
|
|
|
|
it('rejects invalid URLs before anything is spawned', () => {
|
|
expect(() => launchExternal('url', 'https://example.com')).toThrow(SidebarError)
|
|
})
|
|
|
|
it('rejects instead of reporting started when the opener emits a spawn error', async () => {
|
|
const child = new EventEmitter()
|
|
const unref = vi.fn()
|
|
const fakeSpawn = vi.fn(() => Object.assign(child, { unref })) as unknown as typeof import('node:child_process').spawn
|
|
const pending = launchExternal('url', 'zed://file//tmp/a.ts', {
|
|
platform: 'linux',
|
|
wsl: false,
|
|
spawn: fakeSpawn,
|
|
})
|
|
const assertion = expect(pending).rejects.toMatchObject({ code: 'internal' })
|
|
|
|
child.emit('error', Object.assign(new Error('spawn xdg-open ENOENT'), { code: 'ENOENT' }))
|
|
|
|
await assertion
|
|
expect(unref).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|
|
|
|
describe('launchExternal Windows branch order and failure reporting', () => {
|
|
const url = 'vscode://file/C:/a.ts'
|
|
const win = { platform: 'win32' as const, wsl: false }
|
|
|
|
it('spawns `cmd /d /c start "" <url>` first, with the URL as one argv element', () => {
|
|
const fake = recordingSpawn()
|
|
void launchExternal('url', url, { ...win, spawn: fake.spawn }).catch(() => {})
|
|
expect(fake.calls.map(call => ({ command: call.command, args: call.args }))).toEqual([
|
|
{ command: 'cmd.exe', args: ['/d', '/c', 'start', '', url] },
|
|
])
|
|
})
|
|
|
|
it('falls through to rundll32 when the cmd branch cannot spawn', async () => {
|
|
const fake = recordingSpawn()
|
|
const pending = launchExternal('url', url, { ...win, spawn: fake.spawn })
|
|
fake.settle(0, 'error', 'spawn cmd.exe ENOENT')
|
|
expect(fake.calls.map(call => call.command)).toEqual(['cmd.exe', 'rundll32.exe'])
|
|
fake.settle(1, 'spawn')
|
|
await expect(pending).resolves.toEqual({ started: true })
|
|
expect(fake.unref).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('advances on a synchronous spawn throw as well', async () => {
|
|
const fake = recordingSpawn()
|
|
const calls: string[] = []
|
|
const spawnFn = ((command: string, args: readonly string[]) => {
|
|
calls.push(command)
|
|
if (calls.length === 1) throw new Error('EINVAL: bad command line')
|
|
return fake.spawn(command, args)
|
|
}) as unknown as typeof import('node:child_process').spawn
|
|
const pending = launchExternal('url', url, { ...win, spawn: spawnFn })
|
|
expect(calls).toEqual(['cmd.exe', 'rundll32.exe'])
|
|
fake.settle(0, 'spawn')
|
|
await expect(pending).resolves.toEqual({ started: true })
|
|
})
|
|
|
|
it('skips the cmd branch for a `&` URL — the whole launch only tries rundll32', () => {
|
|
const fake = recordingSpawn()
|
|
void launchExternal('url', 'myapp://file/a&b.ts', { ...win, spawn: fake.spawn }).catch(() => {})
|
|
expect(fake.calls.map(call => call.command)).toEqual(['rundll32.exe'])
|
|
expect(fake.calls[0]?.args).toEqual(['url.dll,FileProtocolHandler', 'myapp://file/a&b.ts'])
|
|
})
|
|
|
|
it('rejects with a structured failure naming EVERY dead branch (never a silent success)', async () => {
|
|
const fake = recordingSpawn()
|
|
const pending = launchExternal('url', url, { ...win, spawn: fake.spawn })
|
|
fake.settle(0, 'error', 'spawn cmd.exe ENOENT')
|
|
fake.settle(1, 'error', 'spawn rundll32.exe EACCES')
|
|
const error = await pending.then(() => null, (reason: unknown) => reason)
|
|
|
|
expect(error).toBeInstanceOf(SidebarError)
|
|
const failure = error as SidebarError
|
|
expect(failure.code).toBe('internal')
|
|
expect(failure.status).toBe(500)
|
|
expect(failure.message).toBe(
|
|
'failed to launch external opener for the url: "cmd.exe" (spawn cmd.exe ENOENT); '
|
|
+ '"rundll32.exe" (spawn rundll32.exe EACCES)',
|
|
)
|
|
expect(fake.unref).toHaveBeenCalledTimes(2)
|
|
})
|
|
|
|
it('reports a reveal failure with the same shape (one branch, `path` subject)', async () => {
|
|
const fake = recordingSpawn()
|
|
// Path absoluteness is checked with the HOST path module, so this case uses
|
|
// the linux branch (an absolute POSIX path) to pin the aggregated message
|
|
// of the reveal half.
|
|
const pending = launchExternal('reveal', '/a/b.txt', {
|
|
platform: 'linux',
|
|
wsl: false,
|
|
spawn: fake.spawn,
|
|
})
|
|
fake.settle(0, 'error', 'spawn xdg-open ENOENT')
|
|
const error = await pending.then(() => null, (reason: unknown) => reason)
|
|
|
|
expect(fake.calls.map(call => ({ command: call.command, args: call.args }))).toEqual([
|
|
{ command: 'xdg-open', args: ['/a'] },
|
|
])
|
|
expect((error as SidebarError).message).toBe(
|
|
'failed to launch external opener for the path: "xdg-open" (spawn xdg-open ENOENT)',
|
|
)
|
|
})
|
|
})
|