Skip to content

Commit ad392ce

Browse files
authored
Merge pull request #522 from wang-kaopu/fix/517-client-ssh-open
fix(editor): open SSH remote editor links on the client
2 parents fe5aa44 + 5dbbde6 commit ad392ce

4 files changed

Lines changed: 156 additions & 11 deletions

File tree

docs/plans/2026-08-22-open-with-menu-design.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,12 @@ Cursor (SSH)
8787
- 校验:`reveal` 路径必须绝对(`requireAbsolute`);`url` 必须是 `scheme://` 自定义协议(拒绝 http/https)。
8888
- **Windows 平台命令为约定实现,需在 Windows 实机验证**(本机为 macOS;CI 覆盖 linux)。
8989

90+
**实施偏差(2026-09,#517 / PR #522**:SSH 远程编辑器链接**不再经宿主路由执行**。DSH 部署在无头远端服务器时,宿主侧 `xdg-open` 无 DISPLAY/无编辑器,`vscode://` 静默失败。`api.openExternal``src/client/api.ts`)现把 `<scheme>://vscode-remote/ssh-remote+…` 形态的 URL 在浏览器客户端同步触发 `window.location.assign`(处于用户点击链内,外部协议交给本机编辑器经 Remote-SSH 打开远端文件);reveal 与本地编辑器 URL 仍走本节宿主路由。普通浏览器可处理自定义协议;禁止/未处理 `vscode://` 的 WebView 壳客户端需各自适配(见 #517 补充信息)。
91+
9092
## 已知限制
9193

9294
- 路径含 `#`/`?` 的文件名经 URL 打开可能被浏览器当作 fragment/query(不处理,注释说明)。
93-
- 编辑器未安装/协议未注册:由 OS 弹提示或静默失败,不做安装检测。
95+
- 编辑器未安装/协议未注册:由 OS 弹提示或静默失败,不做安装检测。SSH 客户端分支同理——浏览器端 `location.assign` 无法探测协议 handler 是否存在,仍返回 `{started: true}`
9496
- 自定义编辑器仅支持 URL 模板式,不支持 CLI 命令式;无 `{dir}` 占位符。
9597
- Linux reveal 退化为打开所在目录(精确 select 需要各文件管理器私有协议)。
9698
- SSH 模式为**全局**(非每会话):DSH 无远程会话概念,文件树路径即宿主路径;该模型对应"DSH 跑在远端开发机上"的场景。
@@ -99,6 +101,7 @@ Cursor (SSH)
99101

100102
- `tests/open-with.spec.ts`:解析容错、目标解析与 SSH 过滤、URL 构建(本地/SSH/custom/坏模板)、校验器。
101103
- `tests/open-external.spec.ts`:三平台命令表、URL 校验、spawn 前校验。
104+
- `tests/open-external-client.spec.ts`#522):SSH remote URL 客户端分流(不触 fetch)、本地/reveal/http(s) 留宿主、导航抛错 reject。
102105
- `tests/file-tree-open-with.spec.tsx`(jsdom):右键 → 子菜单/图钉;pin 不选中不关闭;选子项回调关闭菜单;SSH 标签后缀;未接线隐藏。
103106
- `tests/open-with-settings.spec.tsx`:SSH 输入、添加/删除(删除剪枝 pinned)、无效提示、VSCode 系开关。
104107
- `tests/side-card-section.spec.tsx``SettingsBody` 行列表 + render 共存、仅 render 时不变。

src/client/EditorHost.tsx

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,11 @@ export function EditorHost(props: {
192192
}
193193

194194
/** The context menu's "open with" action: reveal the path in the OS file
195-
* manager, or hand the target's URL (a local `file` URL, or the SSH-remote
196-
* form for VSCode-family editors in remote mode) to the host's external
197-
* opener. Failures are logged only — a missing handler is the OS's
198-
* dialog, not a sidebar error. */
195+
* manager, or hand the target's URL to its opener — local `file` URLs go
196+
* to the host's external opener, while the SSH-remote form for
197+
* VSCode-family editors launches on the browser/client machine (see
198+
* api.openExternal). Failures are logged only — a missing handler is the
199+
* OS's/browser's dialog, not a sidebar error. */
199200
const openWith = (targetId: string, absolute: string): void => {
200201
const target = openWithTargets.find(item => item.id === targetId)
201202
if (target === undefined) return

src/client/api.ts

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,51 @@ function gitPayload(scope: SessionScope, worktree: string | undefined, extra: Re
216216
return scopePayload(scope, { ...(worktree !== undefined && worktree !== '' ? { worktree } : {}), ...extra })
217217
}
218218

219+
/** One external-open request from the file tree. */
220+
type OpenExternalPayload =
221+
| { action: 'reveal'; path: string }
222+
| { action: 'url'; url: string }
223+
224+
/** The host route's success shape. */
225+
type OpenExternalResult = { started: boolean }
226+
227+
/**
228+
* Remote VSCode-family URLs must be consumed on the browser/client machine:
229+
* the DSH host can be a headless remote server with no editor or DISPLAY.
230+
* Local editor URLs and reveal actions still belong to the host opener.
231+
*/
232+
function shouldOpenExternalOnClient(payload: OpenExternalPayload): payload is { action: 'url'; url: string } {
233+
if (payload.action !== 'url') return false
234+
let parsed: URL
235+
try {
236+
parsed = new URL(payload.url)
237+
} catch {
238+
return false
239+
}
240+
return parsed.protocol !== 'http:'
241+
&& parsed.protocol !== 'https:'
242+
&& parsed.hostname === 'vscode-remote'
243+
&& parsed.pathname.startsWith('/ssh-remote+')
244+
}
245+
246+
/**
247+
* Dispatch an external-open request to the correct machine. SSH remote-editor
248+
* URLs stay in the synchronous user-click chain and navigate the client so
249+
* its registered vscode:// / cursor:// handler can launch. Everything else
250+
* keeps using the DSH host route.
251+
*/
252+
function openExternal(payload: OpenExternalPayload): Promise<OpenExternalResult> {
253+
if (!shouldOpenExternalOnClient(payload)) {
254+
return call<OpenExternalResult>('open.external', payload)
255+
}
256+
try {
257+
window.location.assign(payload.url)
258+
return Promise.resolve({ started: true })
259+
} catch (error) {
260+
return Promise.reject(error)
261+
}
262+
}
263+
219264
/** The sidebar API surface (session scope threaded through every call). */
220265
export const api = {
221266
sessionCwd: (scope: SessionScope, signal?: AbortSignal) =>
@@ -350,12 +395,10 @@ export const api = {
350395
* check; see the host's browser.probe route). */
351396
browserProbe: (url: string, signal?: AbortSignal) =>
352397
call<BrowserProbeResult>('browser.probe', { url }, signal),
353-
/** External open for the file tree's "open with" menu: reveal a path in
354-
* the OS file manager, or hand a custom-scheme URL (vscode://, cursor://,
355-
* zed://, custom editors) to its registered handler. The host launches
356-
* the platform opener (argv, no shell). */
357-
openExternal: (payload: { action: 'reveal'; path: string } | { action: 'url'; url: string }) =>
358-
call<{ started: boolean }>('open.external', payload),
398+
/** External open for the file tree's "open with" menu. Remote SSH editor
399+
* URLs are launched on the browser/client machine; reveal and local URLs
400+
* keep using the host's platform opener. */
401+
openExternal,
359402
}
360403

361404
/** Absolute URL of the media route for one path (images only). */

tests/open-external-client.spec.ts

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Client/host routing for the file tree's external-open action. Remote
3+
* VSCode-family SSH URLs must launch on the browser machine, while local
4+
* editor URLs and reveal actions keep using the DSH host opener.
5+
*/
6+
import { afterEach, describe, expect, it, vi } from 'vitest'
7+
import { api } from '../src/client/api.ts'
8+
9+
afterEach(() => {
10+
vi.unstubAllGlobals()
11+
})
12+
13+
function hostOk(): ReturnType<typeof vi.fn> {
14+
return vi.fn(async () => ({
15+
ok: true,
16+
status: 200,
17+
json: async () => ({ ok: true, value: { started: true } }),
18+
}))
19+
}
20+
21+
describe('api.openExternal', () => {
22+
it('launches an SSH remote-editor URL on the client without calling the host', async () => {
23+
const assign = vi.fn()
24+
const fetchMock = hostOk()
25+
vi.stubGlobal('window', { location: { assign } })
26+
vi.stubGlobal('fetch', fetchMock)
27+
const url = 'vscode://vscode-remote/ssh-remote+dev/home/u/f.ts'
28+
29+
await expect(api.openExternal({ action: 'url', url })).resolves.toEqual({ started: true })
30+
expect(assign).toHaveBeenCalledOnce()
31+
expect(assign).toHaveBeenCalledWith(url)
32+
expect(fetchMock).not.toHaveBeenCalled()
33+
})
34+
35+
it('launches Cursor/custom VSCode-family SSH URLs on the client too', async () => {
36+
const assign = vi.fn()
37+
const fetchMock = hostOk()
38+
vi.stubGlobal('window', { location: { assign } })
39+
vi.stubGlobal('fetch', fetchMock)
40+
41+
await api.openExternal({
42+
action: 'url',
43+
url: 'cursor://vscode-remote/ssh-remote+dev/home/u/f.ts',
44+
})
45+
await api.openExternal({
46+
action: 'url',
47+
url: 'myfork://vscode-remote/ssh-remote+dev/home/u/f.ts',
48+
})
49+
50+
expect(assign).toHaveBeenCalledTimes(2)
51+
expect(fetchMock).not.toHaveBeenCalled()
52+
})
53+
54+
it('keeps local editor URLs on the host opener', async () => {
55+
const assign = vi.fn()
56+
const fetchMock = hostOk()
57+
vi.stubGlobal('window', { location: { assign } })
58+
vi.stubGlobal('fetch', fetchMock)
59+
60+
await expect(api.openExternal({
61+
action: 'url',
62+
url: 'vscode://file//tmp/a.ts',
63+
})).resolves.toEqual({ started: true })
64+
65+
expect(assign).not.toHaveBeenCalled()
66+
expect(fetchMock).toHaveBeenCalledOnce()
67+
expect(fetchMock.mock.calls[0]?.[0]).toBe('/sidebar/api/open.external')
68+
})
69+
70+
it('keeps reveal actions and http(s) lookalikes on the host opener', async () => {
71+
const assign = vi.fn()
72+
const fetchMock = hostOk()
73+
vi.stubGlobal('window', { location: { assign } })
74+
vi.stubGlobal('fetch', fetchMock)
75+
76+
await api.openExternal({ action: 'reveal', path: '/tmp/a.ts' })
77+
await api.openExternal({
78+
action: 'url',
79+
url: 'https://vscode-remote/ssh-remote+dev/home/u/f.ts',
80+
})
81+
82+
expect(assign).not.toHaveBeenCalled()
83+
expect(fetchMock).toHaveBeenCalledTimes(2)
84+
})
85+
86+
it('returns a rejected promise when client navigation throws', async () => {
87+
const error = new Error('navigation failed')
88+
const fetchMock = hostOk()
89+
vi.stubGlobal('window', { location: { assign: vi.fn(() => { throw error }) } })
90+
vi.stubGlobal('fetch', fetchMock)
91+
92+
await expect(api.openExternal({
93+
action: 'url',
94+
url: 'vscode://vscode-remote/ssh-remote+dev/home/u/f.ts',
95+
})).rejects.toBe(error)
96+
expect(fetchMock).not.toHaveBeenCalled()
97+
})
98+
})

0 commit comments

Comments
 (0)