Skip to content

Commit fa74a79

Browse files
author
Dara Adedeji
committed
fix(server): keep the reveal action on the file manager the label promised
A WSL host with explorer.exe but no interop PowerShell advertises the Linux "files" reveal kind, so the reveal now opens the Linux file manager in that configuration instead of routing to File Explorer. The xdg-mime handler probe also gets its own timeout well inside the editor-scan timeout, so a wedged probe costs only the file manager instead of degrading server.getConfig to an empty editor list.
1 parent 7f9f72a commit fa74a79

2 files changed

Lines changed: 133 additions & 20 deletions

File tree

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

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,16 @@ import * as ExternalLauncher from "./externalLauncher.ts";
2323
interface MockSpawnResult {
2424
readonly exitCode?: number;
2525
readonly stdout?: string;
26+
/** Never deliver an exit code, like a child wedged on a broken desktop session. */
27+
readonly stall?: boolean;
2628
}
2729

2830
function makeMockDetachedHandle(input: MockSpawnResult & { readonly onUnref?: () => void } = {}) {
2931
return ChildProcessSpawner.makeHandle({
3032
pid: ChildProcessSpawner.ProcessId(1),
31-
exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
33+
exitCode: input.stall
34+
? Effect.never
35+
: Effect.succeed(ChildProcessSpawner.ExitCode(input.exitCode ?? 0)),
3236
isRunning: Effect.succeed(true),
3337
kill: () => Effect.void,
3438
unref: Effect.sync(() => {
@@ -412,6 +416,57 @@ it.effect("does not advertise reveal from WSL when interop PowerShell is missing
412416
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
413417
);
414418

419+
// When interop PowerShell is missing the capability advertises the Linux
420+
// "files" kind (or nothing), so the reveal must open the Linux file manager
421+
// the label promised even though plain open still prefers File Explorer.
422+
it.effect("reveals through the Linux file manager when WSL lacks interop PowerShell", () =>
423+
Effect.gen(function* () {
424+
const fileSystem = yield* FileSystem.FileSystem;
425+
const path = yield* Path.Path;
426+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
427+
for (const name of ["explorer.exe", "xdg-open", "xdg-mime"]) {
428+
const filePath = path.join(binDir, name);
429+
yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
430+
yield* fileSystem.chmod(filePath, 0o755);
431+
}
432+
433+
const spawnedCommands: ChildProcess.StandardCommand[] = [];
434+
const kind = yield* Effect.gen(function* () {
435+
const launcher = yield* ExternalLauncher.ExternalLauncher;
436+
const revealKind = yield* launcher.resolveFileManagerRevealKind();
437+
yield* launcher.launchEditor({
438+
editor: "file-manager",
439+
cwd: "/home/t3/workspace/media/clip.mp4",
440+
reveal: true,
441+
});
442+
return revealKind;
443+
}).pipe(
444+
Effect.provide(
445+
testLayer({
446+
platform: "linux",
447+
env: {
448+
PATH: binDir,
449+
WSL_DISTRO_NAME: "Ubuntu-24.04",
450+
WSL_INTEROP: "/run/WSL/1_interop",
451+
DISPLAY: ":0",
452+
},
453+
onSpawn: (command) => {
454+
spawnedCommands.push(command);
455+
},
456+
spawnResult: (command) =>
457+
command.command === "xdg-mime" ? { stdout: "org.gnome.Nautilus.desktop\n" } : undefined,
458+
}),
459+
),
460+
);
461+
462+
assert.equal(kind, "files");
463+
const launch = spawnedCommands.find((command) => command.command === "xdg-open");
464+
assert.ok(launch);
465+
assert.deepEqual(launch.args, ["/home/t3/workspace/media"]);
466+
assert.isUndefined(spawnedCommands.find((command) => command.command === "explorer.exe"));
467+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
468+
);
469+
415470
// Interop can exist without `explorer.exe` on PATH (appendWindowsPath=false)
416471
// while WSLg still provides a working Linux file manager; the host must keep
417472
// the Linux open/reveal path instead of losing the editor entirely.
@@ -664,6 +719,39 @@ it.effect("does not advertise a Linux file manager when the handler query fails"
664719
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
665720
);
666721

722+
// The handler probe carries its own timeout because the editor scan's outer
723+
// timeout in server.getConfig degrades to an EMPTY editor list: a wedged
724+
// xdg-mime must cost only the file manager, never the other editors. Runs on
725+
// the live clock so the probe's real timeout fires.
726+
it.live("a stalled handler probe drops only the file manager", () =>
727+
Effect.gen(function* () {
728+
const fileSystem = yield* FileSystem.FileSystem;
729+
const path = yield* Path.Path;
730+
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-editors-" });
731+
for (const name of ["xdg-open", "xdg-mime", "code"]) {
732+
const filePath = path.join(binDir, name);
733+
yield* fileSystem.writeFileString(filePath, "#!/bin/sh\n");
734+
yield* fileSystem.chmod(filePath, 0o755);
735+
}
736+
737+
const editors = yield* Effect.gen(function* () {
738+
const launcher = yield* ExternalLauncher.ExternalLauncher;
739+
return yield* launcher.resolveAvailableEditors();
740+
}).pipe(
741+
Effect.provide(
742+
testLayer({
743+
platform: "linux",
744+
env: { PATH: binDir, DISPLAY: ":0" },
745+
spawnResult: (command) => (command.command === "xdg-mime" ? { stall: true } : undefined),
746+
}),
747+
),
748+
);
749+
750+
assert.equal(editors.includes("vscode"), true);
751+
assert.equal(editors.includes("file-manager"), false);
752+
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
753+
);
754+
667755
it.effect("does not advertise a Linux file manager when xdg-mime is missing", () =>
668756
Effect.gen(function* () {
669757
const fileSystem = yield* FileSystem.FileSystem;

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

Lines changed: 44 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -260,8 +260,14 @@ function fileManagerCommandForPlatform(
260260
// that opening a directory does anything: without an `inode/directory` MIME
261261
// handler, `xdg-open` exits nonzero after the launcher has already detached,
262262
// so the client would see a silent no-op. Require the handler before
263-
// advertising the file manager on Linux. Callers bound the runtime: discovery
264-
// runs under the same timeout `server.getConfig` applies to editor scans.
263+
// advertising the file manager on Linux.
264+
//
265+
// The probe carries its own timeout well inside the scan timeout
266+
// `server.getConfig` applies to editor discovery: that outer timeout degrades
267+
// to an empty editor list, so a hung `xdg-mime` (broken D-Bus or desktop
268+
// session) must cost only the file manager, not every discovered editor.
269+
const LINUX_DIRECTORY_HANDLER_PROBE_TIMEOUT = "2 seconds";
270+
265271
const hasUsableLinuxDirectoryHandler = Effect.fn("externalLauncher.hasUsableLinuxDirectoryHandler")(
266272
function* (
267273
env: NodeJS.ProcessEnv,
@@ -290,6 +296,7 @@ const hasUsableLinuxDirectoryHandler = Effect.fn("externalLauncher.hasUsableLinu
290296
),
291297
Effect.map(([stdout, exitCode]) => exitCode === 0 && stdout.trim().length > 0),
292298
Effect.scoped,
299+
Effect.timeout(LINUX_DIRECTORY_HANDLER_PROBE_TIMEOUT),
293300
Effect.orElseSucceed(() => false),
294301
);
295302
},
@@ -609,7 +616,11 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch
609616
// The command resolveUsableFileManagerCommand picked; a WSL host that fell
610617
// back to the Linux file manager must reveal through it as well.
611618
command: string,
612-
): Effect.fn.Return<EditorLaunch, never, FileSystem.FileSystem | Path.Path> {
619+
): Effect.fn.Return<
620+
EditorLaunch,
621+
never,
622+
FileSystem.FileSystem | Path.Path | ChildProcessSpawner.ChildProcessSpawner
623+
> {
613624
if (platform === "darwin") {
614625
return { editor: "file-manager", target, command: "open", args: ["-R", target] };
615626
}
@@ -624,24 +635,38 @@ const resolveFileManagerRevealLaunch = Effect.fn("resolveFileManagerRevealLaunch
624635
env.WSL_DISTRO_NAME !== undefined
625636
) {
626637
const explorerTarget = resolveWslFileManagerPath(target, env.WSL_DISTRO_NAME);
627-
// Explorer's raw switch cannot express a double quote, and unlike Windows
628-
// paths a WSL path may legally contain one: fall back to opening the
629-
// containing directory the same way the non-reveal launch does. The same
630-
// fallback covers a missing interop PowerShell, so the launch never
631-
// depends on an executable the capability did not advertise.
632-
if (
633-
explorerTarget.includes('"') ||
634-
!(yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env }))
635-
) {
638+
if (yield* isCommandAvailable(WSL_POWERSHELL_COMMAND, { env })) {
639+
// Explorer's raw switch cannot express a double quote, and unlike
640+
// Windows paths a WSL path may legally contain one: open the containing
641+
// directory in File Explorer instead, matching the advertised
642+
// "file-explorer" kind.
643+
if (explorerTarget.includes('"')) {
644+
const path = yield* Path.Path;
645+
return {
646+
editor: "file-manager",
647+
target,
648+
command: "explorer.exe",
649+
args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],
650+
};
651+
}
652+
return fileExplorerRevealLaunch(target, explorerTarget, WSL_POWERSHELL_COMMAND);
653+
}
654+
// Without interop PowerShell the capability advertised the Linux "files"
655+
// kind when it advertised anything at all, so the reveal must open the
656+
// Linux file manager the label promised, not File Explorer.
657+
if (hasGraphicalLinuxSession(env) && (yield* isUsableFileManagerCommand("xdg-open", env))) {
636658
const path = yield* Path.Path;
637-
return {
638-
editor: "file-manager",
639-
target,
640-
command: "explorer.exe",
641-
args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],
642-
};
659+
return { editor: "file-manager", target, command: "xdg-open", args: [path.dirname(target)] };
643660
}
644-
return fileExplorerRevealLaunch(target, explorerTarget, WSL_POWERSHELL_COMMAND);
661+
// Nothing was advertised here; open the parent in File Explorer as the
662+
// best remaining effort for a stale client.
663+
const path = yield* Path.Path;
664+
return {
665+
editor: "file-manager",
666+
target,
667+
command: "explorer.exe",
668+
args: [resolveWslFileManagerPath(path.dirname(target), env.WSL_DISTRO_NAME)],
669+
};
645670
}
646671

647672
// Linux file managers have no portable "select this file" flag, so open

0 commit comments

Comments
 (0)