Skip to content

Commit a3de2ae

Browse files
fix(server): select linked files when revealing on Linux (pingdotgg#14044)
Co-authored-by: Julius Marminge <[email protected]>
1 parent 89081d0 commit a3de2ae

2 files changed

Lines changed: 201 additions & 7 deletions

File tree

‎apps/server/src/process/externalLauncher.test.ts‎

Lines changed: 139 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ import * as Effect from "effect/Effect";
1111
import * as Exit from "effect/Exit";
1212
import * as Fiber from "effect/Fiber";
1313
import * as FileSystem from "effect/FileSystem";
14+
import * as Latch from "effect/Latch";
1415
import * as Layer from "effect/Layer";
1516
import * as Path from "effect/Path";
17+
import * as PlatformError from "effect/PlatformError";
1618
import * as Sink from "effect/Sink";
1719
import * as Stream from "effect/Stream";
1820
import * as TestClock from "effect/testing/TestClock";
@@ -29,6 +31,7 @@ import * as ExternalLauncher from "./externalLauncher.ts";
2931
const windowsHost = HostProcess.Platform.defaultValue() === "win32";
3032

3133
interface MockSpawnResult {
34+
readonly spawnError?: PlatformError.PlatformError;
3235
readonly exitCode?: number;
3336
readonly stdout?: string;
3437
readonly stall?: boolean;
@@ -69,15 +72,17 @@ const layerTest = (input: {
6972
const layerSpawner = Layer.succeed(
7073
ChildProcessSpawner.ChildProcessSpawner,
7174
ChildProcessSpawner.make((command) =>
72-
Effect.sync(() => {
75+
Effect.gen(function* () {
7376
assert.equal(ChildProcess.isStandardCommand(command), true);
7477
if (!ChildProcess.isStandardCommand(command)) {
7578
throw new Error("Expected a standard command");
7679
}
7780
input.onSpawn?.(command);
81+
const result = input.spawnResult?.(command);
82+
if (result?.spawnError) return yield* result.spawnError;
7883
return makeMockDetachedHandle({
7984
...(input.onUnref === undefined ? {} : { onUnref: input.onUnref }),
80-
...input.spawnResult?.(command),
85+
...result,
8186
});
8287
}),
8388
),
@@ -857,6 +862,138 @@ it.effect.skipIf(windowsHost)("reveals by opening the containing directory on Li
857862
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
858863
);
859864

865+
for (const wsl of [false, true]) {
866+
it.effect.skipIf(windowsHost)(
867+
"selects a file over D-Bus on " + (wsl ? "WSLg without interop PowerShell" : "Linux"),
868+
() =>
869+
Effect.gen(function* () {
870+
const fileSystem = yield* FileSystem.FileSystem;
871+
const path = yield* Path.Path;
872+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
873+
for (const name of ["xdg-open", "xdg-mime", "gdbus", ...(wsl ? ["explorer.exe"] : [])]) {
874+
const filePath = path.join(binDir, name);
875+
yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
876+
yield* fileSystem.chmod(filePath, 0o755);
877+
}
878+
const target = "/workspace/media/a 'quoted' \"clip\" #100% ? é\\name.mp4";
879+
const spawnedCommands: ChildProcess.StandardCommand[] = [];
880+
yield* Effect.gen(function* () {
881+
const launcher = yield* ExternalLauncher.ExternalLauncher;
882+
yield* launcher.launchEditor({ editor: "file-manager", cwd: target, reveal: true });
883+
}).pipe(
884+
Effect.provide(
885+
layerTest({
886+
platform: "linux",
887+
env: {
888+
PATH: binDir,
889+
WAYLAND_DISPLAY: "wayland-1",
890+
...(wsl
891+
? { WSL_DISTRO_NAME: "Ubuntu-24.04", WSL_INTEROP: "/run/WSL/1_interop" }
892+
: {}),
893+
},
894+
onSpawn: (command) => spawnedCommands.push(command),
895+
spawnResult: (command) =>
896+
command.command === "xdg-mime"
897+
? { stdout: "com.thisisgm.flea.desktop\n" }
898+
: undefined,
899+
}),
900+
),
901+
);
902+
903+
const reveal = spawnedCommands.find((command) => command.command === "gdbus");
904+
assert.ok(reveal);
905+
assert.deepEqual(reveal.args, [
906+
"call",
907+
"--session",
908+
"--dest",
909+
"org.freedesktop.FileManager1",
910+
"--object-path",
911+
"/org/freedesktop/FileManager1",
912+
"--method",
913+
"org.freedesktop.FileManager1.ShowItems",
914+
"[\"file:///workspace/media/a%20'quoted'%20%22clip%22%20%23100%25%20%3F%20%C3%A9%5Cname.mp4\"]",
915+
"",
916+
]);
917+
assert.deepEqual(
918+
spawnedCommands.filter((command) => command.command !== "xdg-mime"),
919+
[reveal],
920+
);
921+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
922+
);
923+
}
924+
925+
for (const scenario of ["rejected", "spawn-error", "stalled"] as const) {
926+
it.effect.skipIf(windowsHost)("opens the containing directory when D-Bus is " + scenario, () =>
927+
Effect.gen(function* () {
928+
const fileSystem = yield* FileSystem.FileSystem;
929+
const path = yield* Path.Path;
930+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
931+
for (const name of ["xdg-open", "xdg-mime", "gdbus"]) {
932+
const filePath = path.join(binDir, name);
933+
yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
934+
yield* fileSystem.chmod(filePath, 0o755);
935+
}
936+
937+
const spawnedCommands: ChildProcess.StandardCommand[] = [];
938+
const started = yield* Latch.make();
939+
const launch = Effect.gen(function* () {
940+
const launcher = yield* ExternalLauncher.ExternalLauncher;
941+
yield* launcher.launchEditor({
942+
editor: "file-manager",
943+
cwd: "/workspace/media/linux-mini-v2.mp4",
944+
reveal: true,
945+
});
946+
}).pipe(
947+
Effect.provide(
948+
layerTest({
949+
platform: "linux",
950+
env: { PATH: binDir, DISPLAY: ":0" },
951+
onSpawn: (command) => {
952+
spawnedCommands.push(command);
953+
if (command.command === "gdbus") started.openUnsafe();
954+
},
955+
spawnResult: (command) => {
956+
if (command.command === "xdg-mime") return { stdout: "org.gnome.Nautilus.desktop\n" };
957+
if (command.command !== "gdbus") return undefined;
958+
if (scenario === "spawn-error") {
959+
return {
960+
spawnError: new PlatformError.PlatformError(
961+
new PlatformError.SystemError({
962+
_tag: "NotFound",
963+
module: "ChildProcess",
964+
method: "spawn",
965+
pathOrDescriptor: "gdbus",
966+
}),
967+
),
968+
};
969+
}
970+
return { exitCode: 1, stall: scenario === "stalled" };
971+
},
972+
}),
973+
),
974+
);
975+
if (scenario === "stalled") {
976+
const fiber = yield* launch.pipe(Effect.forkChild);
977+
yield* started.await;
978+
yield* TestClock.adjust("1999 millis");
979+
assert.isUndefined(spawnedCommands.find((command) => command.command === "xdg-open"));
980+
yield* TestClock.adjust("1 millis");
981+
yield* Fiber.join(fiber);
982+
} else {
983+
yield* launch;
984+
}
985+
986+
assert.deepEqual(
987+
spawnedCommands
988+
.filter((command) => command.command !== "xdg-mime")
989+
.map((command) => command.command),
990+
["gdbus", "xdg-open"],
991+
);
992+
assert.deepEqual(spawnedCommands.at(-1)?.args, ["/workspace/media"]);
993+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
994+
);
995+
}
996+
860997
it.effect.skipIf(windowsHost)(
861998
"does not advertise a Linux file manager without a graphical session",
862999
() =>

‎apps/server/src/process/externalLauncher.ts‎

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -652,12 +652,69 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch
652652
};
653653
}
654654

655-
// Linux file managers have no portable "select this file" flag, so open
656-
// the containing directory instead.
657655
const path = yield* Path.Path;
658656
return { editor: "file-manager", target, command, args: [path.dirname(target)] };
659657
});
660658

659+
const tryRevealLinuxItem = Effect.fn("externalLauncher.tryRevealLinuxItem")(
660+
function* (target: string) {
661+
const env = yield* readCommandLookupEnv;
662+
if (!(yield* isCommandAvailable("gdbus", { env }))) return false;
663+
664+
const path = yield* Path.Path;
665+
const uri = yield* path.toFileUrl(target);
666+
const spawner = yield* ChildProcessSpawner.ChildProcessSpawner;
667+
const exitCode = yield* spawner.exitCode(
668+
ChildProcess.make(
669+
"gdbus",
670+
[
671+
"call",
672+
"--session",
673+
"--dest",
674+
"org.freedesktop.FileManager1",
675+
"--object-path",
676+
"/org/freedesktop/FileManager1",
677+
"--method",
678+
"org.freedesktop.FileManager1.ShowItems",
679+
// File URLs percent-encode the double quotes and backslashes GVariant would interpret.
680+
`["${uri.href}"]`,
681+
"",
682+
],
683+
{
684+
stdin: "ignore",
685+
stdout: "ignore",
686+
stderr: "ignore",
687+
// Timeout waits for scoped cleanup, even if the helper ignores SIGTERM.
688+
forceKillAfter: "1 second",
689+
},
690+
),
691+
);
692+
return exitCode === 0;
693+
},
694+
Effect.timeout("2 seconds"),
695+
Effect.catchTags({
696+
BadArgument: () => Effect.succeed(false),
697+
PlatformError: () => Effect.succeed(false),
698+
TimeoutError: () => Effect.succeed(false),
699+
}),
700+
);
701+
702+
const launchEditor = Effect.fn("externalLauncher.launchEditor")(function* (
703+
input: LaunchEditorInput,
704+
) {
705+
const launch = yield* resolveEditorLaunch(input);
706+
if (
707+
input.editor === "file-manager" &&
708+
input.reveal === true &&
709+
launch.command === "xdg-open" &&
710+
(yield* HostProcess.Platform) === "linux" &&
711+
(yield* tryRevealLinuxItem(launch.target))
712+
) {
713+
return;
714+
}
715+
yield* launchEditorProcess(launch);
716+
});
717+
661718
const launchAndUnref = Effect.fn("externalLauncher.launchAndUnref")(function* (
662719
launch: ProcessLaunch,
663720
onError: (cause: unknown) => ExternalLauncherError,
@@ -817,9 +874,9 @@ export const make = Effect.gen(function* () {
817874
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
818875
),
819876
launchEditor: (input) =>
820-
provideCommandResolutionServices(
821-
Effect.flatMap(resolveEditorLaunch(input), launchEditorProcess),
822-
).pipe(Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner)),
877+
provideCommandResolutionServices(launchEditor(input)).pipe(
878+
Effect.provideService(ChildProcessSpawner.ChildProcessSpawner, spawner),
879+
),
823880
});
824881
});
825882

0 commit comments

Comments
 (0)