diff --git a/src/main/ipc/shell.test.ts b/src/main/ipc/shell.test.ts index 1ada6db82..29a301cba 100644 --- a/src/main/ipc/shell.test.ts +++ b/src/main/ipc/shell.test.ts @@ -251,6 +251,24 @@ describe('registerShellHandlers', () => { ]) }) + it('forces Cursor launcher folders into a new window', async () => { + resolveCliCommandMock.mockReturnValueOnce('/usr/local/bin/cursor') + const workspacePath = resolve('workspace') + const handler = getHandler('shell:openInExternalEditor') + + await expect(handler({}, workspacePath, 'cursor')).resolves.toEqual({ ok: true }) + expect(getSpawnArgsForWindowsMock).toHaveBeenCalledWith('/usr/local/bin/cursor', [ + '--new-window', + normalize(workspacePath) + ]) + resolveCliCommandMock.mockReturnValueOnce('C:\\Cursor\\cursor.cmd') + await expect(handler({}, workspacePath, 'cursor')).resolves.toEqual({ ok: true }) + expect(getSpawnArgsForWindowsMock).toHaveBeenLastCalledWith('C:\\Cursor\\cursor.cmd', [ + '--new-window', + normalize(workspacePath) + ]) + }) + it('falls back to VS Code when command is blank', async () => { const workspacePath = resolve('workspace') const handler = getHandler('shell:openInExternalEditor') diff --git a/src/main/ipc/shell.ts b/src/main/ipc/shell.ts index 04192ebd6..0edbbc49b 100644 --- a/src/main/ipc/shell.ts +++ b/src/main/ipc/shell.ts @@ -1,7 +1,7 @@ import { ipcMain, shell, dialog } from 'electron' import { spawn } from 'node:child_process' import { constants, copyFile, stat } from 'node:fs/promises' -import { isAbsolute, normalize } from 'node:path' +import { basename, isAbsolute, normalize, win32 } from 'node:path' import { fileURLToPath } from 'node:url' import type { ShellOpenLocalPathResult } from '../../shared/shell-open-types' import { resolveCliCommand } from '../codex-cli/command' @@ -51,9 +51,26 @@ function resolveExternalEditorCommand(command?: string): string { return resolveCliCommand(trimmed || EXTERNAL_EDITOR_CLI_COMMAND) } +function getLauncherBaseName(command: string): string { + const name = command.includes('\\') ? win32.basename(command) : basename(command) + return name.replace(/\.(?:cmd|exe|bat)$/i, '').toLowerCase() +} + +function buildExternalEditorArgs(editorCommand: string, pathValue: string): string[] { + if (getLauncherBaseName(editorCommand) === 'cursor') { + // Why: Cursor can route bare folder launches through the last active + // workbench. A new window keeps "Open in Cursor" scoped to this worktree. + return ['--new-window', pathValue] + } + return [pathValue] +} + async function launchExternalEditor(pathValue: string, command?: string): Promise { const editorCommand = resolveExternalEditorCommand(command) - const { spawnCmd, spawnArgs } = getSpawnArgsForWindows(editorCommand, [pathValue]) + const { spawnCmd, spawnArgs } = getSpawnArgsForWindows( + editorCommand, + buildExternalEditorArgs(editorCommand, pathValue) + ) await new Promise((resolvePromise, rejectPromise) => { const child = spawn(spawnCmd, spawnArgs, {