From be23cedeb92f5571073c030e2c7c060a0b40b276 Mon Sep 17 00:00:00 2001 From: "t3-code[bot]" <269035359+t3-code[bot]@users.noreply.github.com> Date: Wed, 5 Aug 2026 05:44:17 +0000 Subject: [PATCH] fix: select files when revealing supported --- .../src/process/externalLauncher.test.ts | 71 ++++++++++++++++++- apps/server/src/process/externalLauncher.ts | 26 ++++++- 2 files changed, 92 insertions(+), 5 deletions(-) diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.ts index 33a61122e8..e9b096440f 100644 --- a/apps/server/src/process/externalLauncher.test.ts +++ b/apps/server/src/process/externalLauncher.test.ts @@ -125,7 +125,7 @@ it.effect("reveals files with the linux file manager", () => }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); -it.effect("opens the containing folder with Finder", () => +it.effect("opens the containing folder when the Finder target is missing", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -156,7 +156,41 @@ it.effect("opens the containing folder with Finder", () => }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); -it.effect("opens the containing folder with Windows Explorer", () => +it.effect("selects an existing file in Finder", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" }); + const commandPath = path.join(binDir, "open"); + const targetPath = path.join(binDir, "project with spaces", "src", "index.ts"); + yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n"); + yield* fileSystem.chmod(commandPath, 0o755); + yield* fileSystem.makeDirectory(path.dirname(targetPath), { recursive: true }); + yield* fileSystem.writeFileString(targetPath, ""); + + let spawned: ChildProcess.StandardCommand | undefined; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + yield* launcher.revealInFileManager({ path: targetPath }); + }).pipe( + Effect.provide( + testLayer({ + platform: "darwin", + env: { PATH: binDir }, + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); + + assert.ok(spawned); + assert.equal(spawned.command, "open"); + assert.deepEqual(spawned.args, ["-R", targetPath]); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + +it.effect("opens the containing folder when the Explorer target is missing", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; @@ -186,6 +220,39 @@ it.effect("opens the containing folder with Windows Explorer", () => }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), ); +it.effect("selects an existing file in Windows Explorer", () => + Effect.gen(function* () { + const fileSystem = yield* FileSystem.FileSystem; + const path = yield* Path.Path; + const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-file-manager-" }); + const targetPath = path.join(binDir, "project files", "src", "index.ts"); + yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n"); + yield* fileSystem.makeDirectory(path.dirname(targetPath), { recursive: true }); + yield* fileSystem.writeFileString(targetPath, ""); + + let spawned: ChildProcess.StandardCommand | undefined; + yield* Effect.gen(function* () { + const launcher = yield* ExternalLauncher.ExternalLauncher; + yield* launcher.revealInFileManager({ path: targetPath }); + }).pipe( + Effect.provide( + testLayer({ + platform: "win32", + env: { PATH: binDir, PATHEXT: ".COM;.EXE;.BAT;.CMD" }, + onSpawn: (command) => { + spawned = command; + }, + }), + ), + ); + + assert.ok(spawned); + assert.equal(spawned.command, "explorer"); + assert.deepEqual(spawned.args, ["/select,", targetPath.replaceAll("/", "\\")]); + assert.equal(spawned.options.shell, false); + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), +); + it.effect("launches an installed editor with platform-safe arguments", () => Effect.gen(function* () { const fileSystem = yield* FileSystem.FileSystem; diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.ts index 39ffccd085..26d205332b 100644 --- a/apps/server/src/process/externalLauncher.ts +++ b/apps/server/src/process/externalLauncher.ts @@ -243,16 +243,32 @@ function hasGraphicalFileManagerSession( return true; } +function normalizeWindowsFileManagerPath(target: string): string { + return target.replaceAll("/", "\\"); +} + function fileManagerFolderPath(platform: NodeJS.Platform, target: string, path: Path.Path): string { if (platform !== "win32") return path.dirname(target); - const normalized = target.replaceAll("/", "\\"); + const normalized = normalizeWindowsFileManagerPath(target); const separatorIndex = normalized.lastIndexOf("\\"); if (separatorIndex < 0) return "."; if (separatorIndex === 2 && normalized[1] === ":") return normalized.slice(0, 3); return normalized.slice(0, separatorIndex) || "\\"; } +function fileManagerRevealArgs( + platform: NodeJS.Platform, + target: string, + targetExists: boolean, + path: Path.Path, +): ReadonlyArray { + if (!targetExists) return [fileManagerFolderPath(platform, target, path)]; + if (platform === "darwin") return ["-R", target]; + if (platform === "win32") return ["/select,", normalizeWindowsFileManagerPath(target)]; + return [fileManagerFolderPath(platform, target, path)]; +} + function fileManagerCommandForPlatform(platform: NodeJS.Platform): string { switch (platform) { case "darwin": @@ -422,14 +438,18 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* ( const resolveFileManagerRevealLaunch = Effect.fn("externalLauncher.resolveFileManagerRevealLaunch")( function* ( input: RevealInFileManagerInput, - ): Effect.fn.Return { + ): Effect.fn.Return { const platform = yield* HostProcessPlatform; const env = yield* readCommandLookupEnv; if (!hasGraphicalFileManagerSession(platform, env)) { return yield* new ExternalLauncherUnsupportedEditorError({ editor: "file-manager" }); } + const fileSystem = yield* FileSystem.FileSystem; const path = yield* Path.Path; - const args = [fileManagerFolderPath(platform, input.path, path)]; + const targetExists = yield* fileSystem + .exists(input.path) + .pipe(Effect.orElseSucceed(() => false)); + const args = fileManagerRevealArgs(platform, input.path, targetExists, path); yield* Effect.annotateCurrentSpan({ "externalLauncher.target": input.path,