TypeScript
fix(server): select linked files when revealing on Linux (#14044)
Akshar Patela3de2ae057b9
diff --git a/apps/server/src/process/externalLauncher.test.ts b/apps/server/src/process/externalLauncher.test.tsindex 9c0935de308..8ac601738bb 100644--- a/apps/server/src/process/externalLauncher.test.ts+++ b/apps/server/src/process/externalLauncher.test.ts@@ -11,8 +11,10 @@ import * as Effect from "effect/Effect";import * as Exit from "effect/Exit";import * as Fiber from "effect/Fiber";import * as FileSystem from "effect/FileSystem";+import * as Latch from "effect/Latch";import * as Layer from "effect/Layer";import * as Path from "effect/Path";+import * as PlatformError from "effect/PlatformError";import * as Sink from "effect/Sink";import * as Stream from "effect/Stream";import * as TestClock from "effect/testing/TestClock";@@ -29,6 +31,7 @@ import * as ExternalLauncher from "./externalLauncher.ts";const windowsHost = HostProcess.Platform.defaultValue() === "win32";interface MockSpawnResult {+ readonly spawnError?: PlatformError.PlatformError;readonly exitCode?: number;readonly stdout?: string;readonly stall?: boolean;@@ -69,15 +72,17 @@ const layerTest = (input: {const layerSpawner = Layer.succeed(ChildProcessSpawner.ChildProcessSpawner,ChildProcessSpawner.make((command) =>- Effect.sync(() => {+ Effect.gen(function* () {assert.equal(ChildProcess.isStandardCommand(command), true);if (!ChildProcess.isStandardCommand(command)) {throw new Error("Expected a standard command");}input.onSpawn?.(command);+ const result = input.spawnResult?.(command);+ if (result?.spawnError) return yield* result.spawnError;return makeMockDetachedHandle({...(input.onUnref === undefined ? {} : { onUnref: input.onUnref }),- ...input.spawnResult?.(command),+ ...result,});}),),@@ -857,6 +862,138 @@ it.effect.skipIf(windowsHost)("reveals by opening the containing directory on Li}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),);+for (const wsl of [false, true]) {+ it.effect.skipIf(windowsHost)(+ "selects a file over D-Bus on " + (wsl ? "WSLg without interop PowerShell" : "Linux"),+ () =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });+ for (const name of ["xdg-open", "xdg-mime", "gdbus", ...(wsl ? ["explorer.exe"] : [])]) {+ const filePath = path.join(binDir, name);+ yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");+ yield* fileSystem.chmod(filePath, 0o755);+ }+ const target = "/workspace/media/a 'quoted' \"clip\" #100% ? é\\name.mp4";+ const spawnedCommands: ChildProcess.StandardCommand[] = [];+ yield* Effect.gen(function* () {+ const launcher = yield* ExternalLauncher.ExternalLauncher;+ yield* launcher.launchEditor({ editor: "file-manager", cwd: target, reveal: true });+ }).pipe(+ Effect.provide(+ layerTest({+ platform: "linux",+ env: {+ PATH: binDir,+ WAYLAND_DISPLAY: "wayland-1",+ ...(wsl+ ? { WSL_DISTRO_NAME: "Ubuntu-24.04", WSL_INTEROP: "/run/WSL/1_interop" }+ : {}),+ },+ onSpawn: (command) => spawnedCommands.push(command),+ spawnResult: (command) =>+ command.command === "xdg-mime"+ ? { stdout: "com.thisisgm.flea.desktop\n" }+ : undefined,+ }),+ ),+ );++ const reveal = spawnedCommands.find((command) => command.command === "gdbus");+ assert.ok(reveal);+ assert.deepEqual(reveal.args, [+ "call",+ "--session",+ "--dest",+ "org.freedesktop.FileManager1",+ "--object-path",+ "/org/freedesktop/FileManager1",+ "--method",+ "org.freedesktop.FileManager1.ShowItems",+ "[\"file:///workspace/media/a%20'quoted'%20%22clip%22%20%23100%25%20%3F%20%C3%A9%5Cname.mp4\"]",+ "",+ ]);+ assert.deepEqual(+ spawnedCommands.filter((command) => command.command !== "xdg-mime"),+ [reveal],+ );+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );+}++for (const scenario of ["rejected", "spawn-error", "stalled"] as const) {+ it.effect.skipIf(windowsHost)("opens the containing directory when D-Bus is " + scenario, () =>+ Effect.gen(function* () {+ const fileSystem = yield* FileSystem.FileSystem;+ const path = yield* Path.Path;+ const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });+ for (const name of ["xdg-open", "xdg-mime", "gdbus"]) {+ const filePath = path.join(binDir, name);+ yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");+ yield* fileSystem.chmod(filePath, 0o755);+ }++ const spawnedCommands: ChildProcess.StandardCommand[] = [];+ const started = yield* Latch.make();+ const launch = Effect.gen(function* () {+ const launcher = yield* ExternalLauncher.ExternalLauncher;+ yield* launcher.launchEditor({+ editor: "file-manager",+ cwd: "/workspace/media/linux-mini-v2.mp4",+ reveal: true,+ });+ }).pipe(+ Effect.provide(+ layerTest({+ platform: "linux",+ env: { PATH: binDir, DISPLAY: ":0" },+ onSpawn: (command) => {+ spawnedCommands.push(command);+ if (command.command === "gdbus") started.openUnsafe();+ },+ spawnResult: (command) => {+ if (command.command === "xdg-mime") return { stdout: "org.gnome.Nautilus.desktop\n" };+ if (command.command !== "gdbus") return undefined;+ if (scenario === "spawn-error") {+ return {+ spawnError: new PlatformError.PlatformError(+ new PlatformError.SystemError({+ _tag: "NotFound",+ module: "ChildProcess",+ method: "spawn",+ pathOrDescriptor: "gdbus",+ }),+ ),+ };+ }+ return { exitCode: 1, stall: scenario === "stalled" };+ },+ }),+ ),+ );+ if (scenario === "stalled") {+ const fiber = yield* launch.pipe(Effect.forkChild);+ yield* started.await;+ yield* TestClock.adjust("1999 millis");+ assert.isUndefined(spawnedCommands.find((command) => command.command === "xdg-open"));+ yield* TestClock.adjust("1 millis");+ yield* Fiber.join(fiber);+ } else {+ yield* launch;+ }++ assert.deepEqual(+ spawnedCommands+ .filter((command) => command.command !== "xdg-mime")+ .map((command) => command.command),+ ["gdbus", "xdg-open"],+ );+ assert.deepEqual(spawnedCommands.at(-1)?.args, ["/workspace/media"]);+ }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),+ );+}+it.effect.skipIf(windowsHost)("does not advertise a Linux file manager without a graphical session",() =>diff --git a/apps/server/src/process/externalLauncher.ts b/apps/server/src/process/externalLauncher.tsindex 668e7f74690..aec18ea4c73 100644--- a/apps/server/src/process/externalLauncher.ts+++ b/apps/server/src/process/externalLauncher.ts@@ -652,12 +652,69 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch};}- // Linux file managers have no portable "select this file" flag, so open- // the containing directory instead.const path = yield* Path.Path;return { editor: "file-manager", target, command, args: [path.dirname(target)] };});+const tryRevealLinuxItem = Effect.fn("externalLauncher.tryRevealLinuxItem")(+ function* (target: string) {+ const env = yield* readCommandLookupEnv;+ if (!(yield* isCommandAvailable("gdbus", { env }))) return false;++ const path = yield* Path.Path;+ const uri = yield* path.toFileUrl(target);+ const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;+ const exitCode = yield* spawner.exitCode(+ ChildProcess.make(+ "gdbus",+ [+ "call",+ "--session",+ "--dest",+ "org.freedesktop.FileManager1",+ "--object-path",+ "/org/freedesktop/FileManager1",+ "--method",+ "org.freedesktop.FileManager1.ShowItems",+ // File URLs percent-encode the double quotes and backslashes GVariant would interpret.+ `["${uri.href}"]`,+ "",+ ],+ {+ stdin: "ignore",+ stdout: "ignore",+ stderr: "ignore",+ // Timeout waits for scoped cleanup, even if the helper ignores SIGTERM.+ forceKillAfter: "1 second",+ },+ ),+ );+ return exitCode === 0;+ },+ Effect.timeout("2 seconds"),+ Effect.catchTags({+ BadArgument: () => Effect.succeed(false),+ PlatformError: () => Effect.succeed(false),+ TimeoutError: () => Effect.succeed(false),+ }),+);++const launchEditor = Effect.fn("externalLauncher.launchEditor")(function* (+ input: LaunchEditorInput,+) {+ const launch = yield* resolveEditorLaunch(input);+ if (+ input.editor === "file-manager" &&+ input.reveal === true &&+ launch.command === "xdg-open" &&+ (yield* HostProcess.Platform) === "linux" &&+ (yield* tryRevealLinuxItem(launch.target))+ ) {+ return;+ }+ yield* launchEditorProcess(launch);+});+const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (launch: ProcessLaunch,onError: (cause: unknown) => ExternalLauncherError,@@ -817,9 +874,9 @@ export const make = Effect.gen(function* () {Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),),launchEditor: (input) =>- provideCommandResolutionServices(- Effect.flatMap(resolveEditorLaunch(input), launchEditorProcess),- ).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),+ provideCommandResolutionServices(launchEditor(input)).pipe(+ Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),+ ),});});