diff --git a/apps/server/spawned2.txt b/apps/server/spawned2.txt new file mode 100644 index 0000000000..3b31732c2d --- /dev/null +++ b/apps/server/spawned2.txt @@ -0,0 +1,2 @@ +started +started diff --git a/apps/server/src/process/Layers/DesktopLauncher.test.ts b/apps/server/src/process/Layers/DesktopLauncher.test.ts index f8978d43f0..8db3a7eef9 100644 --- a/apps/server/src/process/Layers/DesktopLauncher.test.ts +++ b/apps/server/src/process/Layers/DesktopLauncher.test.ts @@ -29,6 +29,7 @@ interface SpawnCall { readonly detached?: boolean | undefined; readonly shell?: boolean | string | undefined; readonly windowsVerbatimArguments?: boolean | undefined; + readonly windowsHide?: boolean | undefined; readonly stdin?: unknown; readonly stdout?: unknown; readonly stderr?: unknown; @@ -154,6 +155,7 @@ function spawnHarness( detached: input.detached, shell: input.shell, windowsVerbatimArguments: input.windowsVerbatimArguments, + windowsHide: input.windowsHide, stdin: input.stdin, stdout: input.stdout, stderr: input.stderr, @@ -313,11 +315,11 @@ it.effect("openInEditor uses --goto for editors that support it", () => }).pipe(Effect.provide(NodeFileSystem.layer)), ); -it.effect("openInEditor launches Windows batch shims through cmd.exe without shell mode", () => +it.effect("openInEditor uses explorer with a VS Code protocol target on Windows", () => Effect.gen(function* () { const fs = yield* FileSystem.FileSystem; const dir = yield* fs.makeTempDirectoryScoped({ prefix: "t3-open-vscode-win32-" }); - yield* fs.writeFileString(`${dir}/code.cmd`, "@echo off\r\n"); + yield* fs.writeFileString(`${dir}/explorer.exe`, "MZ"); const calls: Array = []; const open = yield* runOpen( @@ -337,28 +339,17 @@ it.effect("openInEditor launches Windows batch shims through cmd.exe without she editor: "vscode", }); - assert.equal(calls.length, 1); - assert.equal(calls[0]?.command.toLowerCase().includes("powershell"), true); - assert.deepEqual(calls[0]?.args.slice(0, 5), [ - "-NoProfile", - "-NonInteractive", - "-ExecutionPolicy", - "Bypass", - "-EncodedCommand", + assert.deepEqual(calls, [ + { + command: `${dir}/explorer.exe`, + args: ["vscode://file/C:/work/100%25%20real/file.ts:12:4"], + detached: true, + shell: false, + stdin: "ignore", + stdout: "ignore", + stderr: "ignore", + }, ]); - const encoded = calls[0]?.args[5]; - assert.isString(encoded); - const decoded = decodePowerShellCommand(encoded!); - assert.equal(decoded.includes("Start-Process"), true); - assert.equal(decoded.toLowerCase().includes(`${dir}/code.cmd`.toLowerCase()), true); - assert.equal(decoded.includes("--goto"), true); - assert.equal(decoded.includes("100% real"), true); - assert.equal(calls[0]?.detached, true); - assert.equal(calls[0]?.windowsVerbatimArguments, undefined); - assert.equal(calls[0]?.shell, false); - assert.equal(calls[0]?.stdin, "ignore"); - assert.equal(calls[0]?.stdout, "ignore"); - assert.equal(calls[0]?.stderr, "ignore"); }).pipe(Effect.provide(NodeFileSystem.layer)), ); diff --git a/apps/server/src/process/Layers/DesktopLauncher.ts b/apps/server/src/process/Layers/DesktopLauncher.ts index 85302c2335..64b32d60b2 100644 --- a/apps/server/src/process/Layers/DesktopLauncher.ts +++ b/apps/server/src/process/Layers/DesktopLauncher.ts @@ -9,6 +9,7 @@ */ import OS from "node:os"; import { spawn as spawnNodeChildProcess } from "node:child_process"; +import { pathToFileURL } from "node:url"; import { EDITORS, type EditorId } from "@t3tools/contracts"; import { Array, Effect, FileSystem, Layer, Option, Path, Scope } from "effect"; @@ -38,6 +39,7 @@ export interface DetachedSpawnInput { readonly detached?: boolean; readonly shell?: boolean; readonly windowsVerbatimArguments?: boolean; + readonly windowsHide?: boolean; readonly stdin?: "ignore"; readonly stdout?: "ignore"; readonly stderr?: "ignore"; @@ -119,6 +121,11 @@ const WSL_POWERSHELL_CANDIDATES = [ "powershell.exe", "pwsh.exe", ] as const; +const WINDOWS_EDITOR_URI_SCHEMES: Partial> = { + vscode: "vscode", + "vscode-insiders": "vscode-insiders", + vscodium: "vscodium", +}; function shouldUseGotoFlag(editor: (typeof EDITORS)[number], target: string): boolean { return editor.supportsGoto && LINE_COLUMN_SUFFIX_PATTERN.test(target); @@ -128,6 +135,24 @@ function stripWrappingQuotes(value: string): string { return value.replace(/^"+|"+$/g, ""); } +function splitLineColumnSuffix(target: string): { + readonly filePath: string; + readonly suffix: string; +} { + const match = target.match(LINE_COLUMN_SUFFIX_PATTERN); + if (!match) { + return { + filePath: target, + suffix: "", + }; + } + + return { + filePath: target.slice(0, -match[0].length), + suffix: match[0], + }; +} + function resolvePathEnvironmentVariable(env: NodeJS.ProcessEnv): string { return env.PATH ?? env.Path ?? env.path ?? ""; } @@ -188,6 +213,10 @@ function quotePowerShellValue(value: string): string { return `'${value.replaceAll("'", "''")}'`; } +function quotePowerShellArgument(value: string): string { + return `"\`"${value.replaceAll("`", "``").replaceAll('"', '`"')}\`""`; +} + function encodePowerShellCommand(command: string): string { return Buffer.from(command, "utf16le").toString("base64"); } @@ -218,6 +247,19 @@ function shouldPreferWindowsOpenerOnWsl(input: OpenExternalInput, runtime: Launc return runtime.isWsl && !runtime.isInsideContainer && isUriLikeTarget(input.target); } +function makeWindowsEditorProtocolTarget(editor: EditorId, target: string): string | undefined { + const scheme = WINDOWS_EDITOR_URI_SCHEMES[editor]; + if (!scheme) return undefined; + + const { filePath, suffix } = splitLineColumnSuffix(target); + const fileUrl = pathToFileURL(filePath).href; + const fileTarget = fileUrl.startsWith("file:///") + ? fileUrl.slice("file:///".length) + : fileUrl.replace(/^file:\/\//, ""); + + return `${scheme}://file/${fileTarget}${suffix}`; +} + function makeLaunchPlan( command: string, args: ReadonlyArray, @@ -309,8 +351,8 @@ function makePowerShellStartProcessArgs( args: ReadonlyArray, ): ReadonlyArray { const argumentList = - args.length > 0 ? ` -ArgumentList ${args.map(quotePowerShellValue).join(", ")}` : ""; - const command = `Start-Process -FilePath ${quotePowerShellValue(commandPath)}${argumentList} -WindowStyle Hidden`; + args.length > 0 ? ` -ArgumentList ${args.map(quotePowerShellArgument).join(",")}` : ""; + const command = `Start ${quotePowerShellArgument(commandPath)}${argumentList} -WindowStyle Hidden`; return [ "-NoProfile", "-NonInteractive", @@ -336,6 +378,8 @@ function makeWindowsExplorerPlan(input: OpenExternalInput): LaunchPlan { return makeLaunchPlan("explorer", [input.target], { wait: false, allowNonzeroExitCode: false, + detached: true, + stdio: "ignore", shell: false, }); } @@ -408,6 +452,9 @@ function resolveSpawnInput( args: makePowerShellStartProcessArgs(resolvedCommand.path, plan.args), ...(plan.detached !== undefined ? { detached: plan.detached } : {}), ...(plan.shell !== undefined ? { shell: plan.shell } : {}), + ...(runtime.platform === "win32" + ? { windowsVerbatimArguments: true, windowsHide: true } + : {}), ...(plan.stdio === "ignore" ? { stdin: "ignore" as const, @@ -417,6 +464,7 @@ function resolveSpawnInput( : {}), }; } + const windowsHide = runtime.platform === "win32" && plan.detached ? true : undefined; return { command: resolvedCommand.usesCmdWrapper ? resolveWindowsCommandShell(runtime.env) @@ -426,6 +474,7 @@ function resolveSpawnInput( : [...plan.args], ...(plan.detached !== undefined ? { detached: plan.detached } : {}), ...(plan.shell !== undefined ? { shell: plan.shell } : {}), + ...(windowsHide ? { windowsHide } : {}), ...(resolvedCommand.usesCmdWrapper ? { windowsVerbatimArguments: true } : {}), ...(plan.stdio === "ignore" ? { @@ -525,6 +574,7 @@ function defaultSpawnDetached( spawnNodeChildProcess(input.command, [...input.args], { ...(input.detached !== undefined ? { detached: input.detached } : {}), ...(input.shell !== undefined ? { shell: input.shell } : {}), + ...(input.windowsHide !== undefined ? { windowsHide: input.windowsHide } : {}), ...(input.windowsVerbatimArguments !== undefined ? { windowsVerbatimArguments: input.windowsVerbatimArguments } : {}), @@ -891,6 +941,21 @@ export const make = Effect.fn("makeDesktopLauncher")(function* ( return yield* new DesktopLauncherUnknownEditorError({ editor: input.editor }); } + const windowsEditorProtocolTarget = + runtime.platform === "win32" + ? makeWindowsEditorProtocolTarget(input.editor, input.cwd) + : undefined; + if (windowsEditorProtocolTarget) { + return yield* runFirstAvailablePlan( + [makeWindowsExplorerPlan({ target: windowsEditorProtocolTarget })], + { + operation: "openInEditor", + target: input.cwd, + editor: input.editor, + }, + ); + } + if (editor.command) { return yield* runFirstAvailablePlan( [ diff --git a/apps/server/tmp-test.cmd b/apps/server/tmp-test.cmd new file mode 100644 index 0000000000..b095f6e5ac --- /dev/null +++ b/apps/server/tmp-test.cmd @@ -0,0 +1,3 @@ +@echo off +echo started >> spawned2.txt +