TypeScript
fix(server): reveal opens the default Windows file manager (#19526)
Cody Stevense67b555f20ec
diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.tsindex 8ac601738bb..0dedda030dc 100644--- a/apps/server/src/process/externalLauncher.test.ts+++ b/apps/server/src/process/externalLauncher.test.ts@@ -465,16 +465,21 @@ it.effect("reveals a file in File Explorer through PowerShell on Windows", () =>const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");assert.equal(decodedCommand,- "$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + 'C:\\workspace with spaces\\media\\author''s clip.mp4' + '\"')",+ "$ProgressPreference = 'SilentlyContinue'; $folderVerb = (Get-ItemProperty -LiteralPath 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell' -ErrorAction SilentlyContinue).'(default)'; if ($folderVerb -and $folderVerb -ne 'none') { Invoke-Item -LiteralPath 'C:\\workspace with spaces\\media' } else { Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + 'C:\\workspace with spaces\\media\\author''s clip.mp4' + '\"') }",);assert.equal(spawned.options.shell, false);assert.equal(spawned.options.detached, false);}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),);-for (const kind of ["browser", "reveal"] as const) {+for (const { kind, folderVerb } of [+ { kind: "browser", folderVerb: "none" },+ { kind: "reveal", folderVerb: "none" },+ { kind: "reveal", folderVerb: "" },+ { kind: "reveal", folderVerb: "open" },+] as const) {it.live.skipIf(!windowsHost)(- `completes a ${kind} launch through real PowerShell after the launcher scope closes`,+ `completes a ${kind} launch through real PowerShell after the launcher scope closes (folder verb: ${folderVerb || "empty"})`,() =>Effect.gen(function* () {const tempDir = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3-reveal-smoke-"));@@ -499,22 +504,28 @@ for (const kind of ["browser", "reveal"] as const) {);});- const target = "C:/workspace with spaces/media/author's clip.mp4";+ const target = "C:/workspace with spaces/author's media/clip.mp4";yield* Effect.all([Effect.gen(function* () {const realSpawner = yield* ChildProcessSpawner.ChildProcessSpawner;const recordingSpawner = ChildProcessSpawner.make((command) => {assert.ok(ChildProcess.isStandardCommand(command));+ const recorderLiteral = `'${recorderPath.replaceAll("'", "''")}'`;const source = Buffer.from(command.args.at(-1) ?? "", "base64").toString("utf16le")- .replace("'explorer.exe'", `'${recorderPath.replaceAll("'", "''")}'`);+ .replace("'explorer.exe'", recorderLiteral);+ // Control the association without changing the host registry or opening its file manager.+ const handlers =+ kind === "reveal"+ ? `function Get-ItemProperty { [CmdletBinding()] param([string]$LiteralPath); if ($LiteralPath -ne 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell') { throw 'Unexpected registry path' }; @{ '(default)' = '${folderVerb}' } }; function Invoke-Item { param([string]$LiteralPath); Start-Process ${recorderLiteral} -ArgumentList ('"' + $LiteralPath + '"') }; `+ : "";return realSpawner.spawn(ChildProcess.make(command.command,[...command.args.slice(0, -1),- Buffer.from(source, "utf16le").toString("base64"),+ Buffer.from(handlers + source, "utf16le").toString("base64"),],command.options,),@@ -537,7 +548,11 @@ for (const kind of ["browser", "reveal"] as const) {recorded.then((value) => {assert.equal(value,- kind === "browser" ? "" : `/select,"${target.replaceAll("/", "\\")}"`,+ kind === "browser"+ ? ""+ : folderVerb === "open"+ ? `"C:\\workspace with spaces\\author's media"`+ : `/select,"${target.replaceAll("/", "\\")}"`,);}),),@@ -606,7 +621,7 @@ it.effect.skipIf(windowsHost)(const editors = yield* launcher.resolveAvailableEditors();yield* launcher.launchEditor({editor: "file-manager",- cwd: "/home/t3/workspace/media/clip.mp4",+ cwd: "/home/t3/workspace [draft]/author's $media/clip|draft.mp4",reveal: true,});return { kind, editors };@@ -635,7 +650,7 @@ it.effect.skipIf(windowsHost)(const decodedCommand = Buffer.from(encodedCommand, "base64").toString("utf16le");assert.equal(decodedCommand,- "$ProgressPreference = 'SilentlyContinue'; Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace\\media\\clip.mp4' + '\"')",+ "$ProgressPreference = 'SilentlyContinue'; $folderVerb = (Get-ItemProperty -LiteralPath 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell' -ErrorAction SilentlyContinue).'(default)'; if ($folderVerb -and $folderVerb -ne 'none') { Invoke-Item -LiteralPath '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s $media' } else { Start-Process 'explorer.exe' -ArgumentList ('/select,\"' + '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\workspace [draft]\\author''s $media\\clip|draft.mp4' + '\"') }",);}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),);@@ -798,7 +813,7 @@ it.effect.skipIf(windowsHost)(const launcher = yield* ExternalLauncher.ExternalLauncher;yield* launcher.launchEditor({editor: "file-manager",- cwd: '/home/t3/work "quoted"/clip.mp4',+ cwd: '/home/t3/work/clip "quoted".mp4',reveal: true,});}).pipe(@@ -818,8 +833,14 @@ it.effect.skipIf(windowsHost)();assert.ok(spawned);- assert.equal(spawned.command, "explorer.exe");- assert.deepEqual(spawned.args, ['\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\work "quoted"']);+ assert.equal(spawned.command, "powershell.exe");+ assert.equal(spawned.options.detached, true);+ assert.equal(spawned.options.shell, false);+ const source = Buffer.from(spawned.args.at(-1) ?? "", "base64").toString("utf16le");+ assert.equal(+ source,+ `Invoke-Item -LiteralPath '\\\\wsl.localhost\\Ubuntu-24.04\\home\\t3\\work'`,+ );}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),);diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.tsindex aec18ea4c73..2c24857555b 100644--- a/apps/server/src/process/externalLauncher.ts+++ b/apps/server/src/process/externalLauncher.ts@@ -1,3 +1,4 @@+import * as NodePath from "@effect/platform-node/NodePath";import {EDITORS,ExternalLauncherError,@@ -561,30 +562,24 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (};});-/**- * Explorer's contract is the single argument `/select,"<path>"` with only the- * path quoted; Node's default spawn quoting wraps the whole argument when the- * path has spaces and Explorer misparses it, silently opening a fallback- * folder. A single `-ArgumentList` string in Windows PowerShell 5.1 reaches the- * child's command line verbatim, preserving the raw switch.- */-function buildFileExplorerRevealPowerShellSource(explorerCommand: string, target: string): string {- return `$ProgressPreference = 'SilentlyContinue'; Start-Process ${escapePowerShellStringLiteral(explorerCommand)} -ArgumentList ('/select,"' + ${escapePowerShellStringLiteral(target)} + '"')`;+// A replacement manager registers a default Directory shell verb; stock Windows+// uses "none". Explorer needs its /select argument passed verbatim through+// PowerShell because Node's argument quoting changes how Explorer parses it.+function buildFileManagerRevealPowerShellSource(target: string, parent: string): string {+ const literal = escapePowerShellStringLiteral(target);+ return `$ProgressPreference = 'SilentlyContinue'; $folderVerb = (Get-ItemProperty -LiteralPath 'Registry::HKEY_CLASSES_ROOT\\Directory\\shell' -ErrorAction SilentlyContinue).'(default)'; if ($folderVerb -and $folderVerb -ne 'none') { Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(parent)} } else { Start-Process 'explorer.exe' -ArgumentList ('/select,"' + ${literal} + '"') }`;}-function fileExplorerRevealLaunch(+function powerShellFileManagerLaunch(target: string,- explorerTarget: string,+ source: string,powershellCommand: string,): EditorLaunch {return {editor: "file-manager",target,command: powershellCommand,- args: [- ...POWERSHELL_ARGUMENTS_PREFIX,- encodeUtf16LeBase64(buildFileExplorerRevealPowerShellSource("explorer.exe", explorerTarget)),- ],+ args: [...POWERSHELL_ARGUMENTS_PREFIX, encodeUtf16LeBase64(source)],};}@@ -607,10 +602,12 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch}if (platform === "win32") {+ const path = yield* Path.Path.pipe(Effect.provide(NodePath.layerWin32));+ const windowsTarget = normalizeWindowsFileManagerPath(target);return {- ...fileExplorerRevealLaunch(+ ...powerShellFileManagerLaunch(target,- normalizeWindowsFileManagerPath(target),+ buildFileManagerRevealPowerShellSource(windowsTarget, path.dirname(windowsTarget)),resolvePowerShellPath(env),),detached: WINDOWS_POWERSHELL_DETACHED,@@ -624,20 +621,15 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch) {const explorerTarget = resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME);if (yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env })) {- // Explorer's raw switch cannot express a double quote, and unlike- // Windows paths a WSL path may legally contain one: open the containing- // directory in File Explorer instead, matching the advertised- // "file-explorer" kind.- if (explorerTarget.includes('"')) {- const path = yield* Path.Path;- return {- editor: "file-manager",- target,- command: "explorer.exe",- args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],- };- }- return fileExplorerRevealLaunch(target, explorerTarget, WSL_POWERSHELL_COMMAND);+ // WSL names may contain quotes that Explorer's /select cannot encode.+ // Open the parent through the shell so replacement managers still apply.+ const path = yield* Path.Path;+ // .NET path parsing rejects characters that are legal in WSL filenames.+ const parent = resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME);+ const source = explorerTarget.includes('"')+ ? `Invoke-Item -LiteralPath ${escapePowerShellStringLiteral(parent)}`+ : buildFileManagerRevealPowerShellSource(explorerTarget, parent);+ return powerShellFileManagerLaunch(target, source, WSL_POWERSHELL_COMMAND);}if (hasGraphicalLinuxSession(env) && (yield* isUsableFileManagerCommand("xdg-open", env))) {const path = yield* Path.Path;