fix: harden open in folder behavior

This commit is contained in:
t3-code[bot] committed 2026-08-15 02:51:33 +00:00
1 parent 39788e86e9
commit f5965c2083
7 files changed
+246 -25

No files matched your search

@@ -106,12 +106,12 @@ it.effect("reveals files with the linux file manager", () =>
let spawned: ChildProcess.StandardCommand | undefined;
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" });
yield* launcher.revealInFileManager({ path: "/tmp/project with spaces/src/index.ts" });
}).pipe(
Effect.provide(
testLayer({
platform: "linux",
env: { PATH: binDir },
env: { PATH: binDir, DISPLAY: ":0" },
onSpawn: (command) => {
spawned = command;
},
@@ -121,11 +121,11 @@ it.effect("reveals files with the linux file manager", () =>
assert.ok(spawned);
assert.equal(spawned.command, "xdg-open");
assert.deepEqual(spawned.args, ["/tmp/project/src"]);
assert.deepEqual(spawned.args, ["/tmp/project with spaces/src"]);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("reveals files with Finder", () =>
it.effect("opens the containing folder with Finder", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -137,7 +137,7 @@ it.effect("reveals files with Finder", () =>
let spawned: ChildProcess.StandardCommand | undefined;
yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
yield* launcher.revealInFileManager({ path: "/tmp/project/src/index.ts" });
yield* launcher.revealInFileManager({ path: "/tmp/project with spaces/src/index.ts" });
}).pipe(
Effect.provide(
testLayer({
@@ -152,11 +152,11 @@ it.effect("reveals files with Finder", () =>
assert.ok(spawned);
assert.equal(spawned.command, "open");
assert.deepEqual(spawned.args, ["-R", "/tmp/project/src/index.ts"]);
assert.deepEqual(spawned.args, ["/tmp/project with spaces/src"]);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("reveals files with Windows Explorer", () =>
it.effect("opens the containing folder with Windows Explorer", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
@@ -181,7 +181,8 @@ it.effect("reveals files with Windows Explorer", () =>
assert.ok(spawned);
assert.equal(spawned.command, "explorer");
assert.deepEqual(spawned.args, [`/select,"C:\\project files\\src\\index.ts"`]);
assert.deepEqual(spawned.args, [`C:\\project files\\src`]);
assert.equal(spawned.options.shell, false);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
@@ -371,6 +372,107 @@ it.effect("rescans after an interrupted discovery instead of caching the interru
),
);
});
it.effect("does not advertise a file manager on headless Linux", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-headless-linux-" });
const commandPath = path.join(binDir, "xdg-open");
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
yield* fileSystem.chmod(commandPath, 0o755);
const editors = yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.resolveAvailableEditors();
}).pipe(Effect.provide(testLayer({ platform: "linux", env: { PATH: binDir } })));
assert.equal(editors.includes("file-manager"), false);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("advertises a file manager in a Linux graphical session", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-desktop-linux-" });
const commandPath = path.join(binDir, "xdg-open");
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
yield* fileSystem.chmod(commandPath, 0o755);
const editors = yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.resolveAvailableEditors();
}).pipe(
Effect.provide(
testLayer({ platform: "linux", env: { PATH: binDir, WAYLAND_DISPLAY: "wayland-0" } }),
),
);
assert.equal(editors.includes("file-manager"), true);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("rejects a direct file manager reveal on headless Linux", () =>
Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
const error = yield* launcher
.revealInFileManager({ path: "/tmp/project/src/index.ts" })
.pipe(Effect.flip);
assert.instanceOf(error, ExternalLauncher.ExternalLauncherUnsupportedEditorError);
}).pipe(Effect.provide(testLayer({ platform: "linux", env: { PATH: "" } }))),
);
it.effect("does not advertise a file manager over SSH", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-ssh-macos-" });
const commandPath = path.join(binDir, "open");
yield* fileSystem.writeFileString(commandPath, "#!/bin/sh\n");
yield* fileSystem.chmod(commandPath, 0o755);
const editors = yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.resolveAvailableEditors();
}).pipe(
Effect.provide(
testLayer({
platform: "darwin",
env: { PATH: binDir, SSH_CONNECTION: "client server" },
}),
),
);
assert.equal(editors.includes("file-manager"), false);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("does not advertise Explorer from a Windows service", () =>
Effect.gen(function* () {
const fileSystem = yield* FileSystem.FileSystem;
const path = yield* Path.Path;
const binDir = yield* fileSystem.makeTempDirectoryScoped({ prefix: "t3-windows-service-" });
yield* fileSystem.writeFileString(path.join(binDir, "explorer.CMD"), "@echo off\r\n");
const editors = yield* Effect.gen(function* () {
const launcher = yield* ExternalLauncher.ExternalLauncher;
return yield* launcher.resolveAvailableEditors();
}).pipe(
Effect.provide(
testLayer({
platform: "win32",
env: {
PATH: binDir,
PATHEXT: ".COM;.EXE;.BAT;.CMD",
SESSIONNAME: "Services",
},
}),
),
);
assert.equal(editors.includes("file-manager"), false);
}).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
);
it.effect("rejects unknown editors through the service API", () =>
Effect.gen(function* () {
+38 -7
View File
@@ -107,6 +107,11 @@ const CommandLookupEnvConfig = Config.all({
Path: Config.string("Path").pipe(Config.option),
path: Config.string("path").pipe(Config.option),
PATHEXT: Config.string("PATHEXT").pipe(Config.option),
DISPLAY: Config.string("DISPLAY").pipe(Config.option),
WAYLAND_DISPLAY: Config.string("WAYLAND_DISPLAY").pipe(Config.option),
SSH_CONNECTION: Config.string("SSH_CONNECTION").pipe(Config.option),
SSH_TTY: Config.string("SSH_TTY").pipe(Config.option),
SESSIONNAME: Config.string("SESSIONNAME").pipe(Config.option),
}).pipe(Config.map(compactEnv));
const readBrowserLaunchEnv = BrowserLaunchEnvConfig.pipe(Effect.orElseSucceed(() => ({})));
@@ -224,6 +229,30 @@ function resolveWindowsBrowserLaunch(target: string, command: string): ProcessLa
};
}
function hasGraphicalFileManagerSession(
platform: NodeJS.Platform,
env: NodeJS.ProcessEnv,
): boolean {
if (env.SSH_CONNECTION?.trim() || env.SSH_TTY?.trim()) return false;
if (platform === "linux") {
return Boolean(env.DISPLAY?.trim() || env.WAYLAND_DISPLAY?.trim());
}
if (platform === "win32") {
return env.SESSIONNAME?.trim().toLowerCase() !== "services";
}
return true;
}
function fileManagerFolderPath(platform: NodeJS.Platform, target: string, path: Path.Path): string {
if (platform !== "win32") return path.dirname(target);
const normalized = target.replaceAll("/", "\\");
const separatorIndex = normalized.lastIndexOf("\\");
if (separatorIndex < 0) return ".";
if (separatorIndex === 2 && normalized[1] === ":") return normalized.slice(0, 3);
return normalized.slice(0, separatorIndex) || "\\";
}
function fileManagerCommandForPlatform(platform: NodeJS.Platform): string {
switch (platform) {
case "darwin":
@@ -271,6 +300,7 @@ const buildAvailableEditors = Effect.fn("externalLauncher.buildAvailableEditors"
for (const editor of EDITORS) {
if (editor.commands === null) {
if (!hasGraphicalFileManagerSession(platform, env)) continue;
const command = fileManagerCommandForPlatform(platform);
if (yield* isCommandAvailable(command, { env })) {
available.push(editor.id);
@@ -390,15 +420,16 @@ const resolveEditorLaunch = Effect.fn("resolveEditorLaunch")(function* (
});
const resolveFileManagerRevealLaunch = Effect.fn("externalLauncher.resolveFileManagerRevealLaunch")(
function* (input: RevealInFileManagerInput): Effect.fn.Return<EditorLaunch, never, Path.Path> {
function* (
input: RevealInFileManagerInput,
): Effect.fn.Return<EditorLaunch, ExternalLauncherError, Path.Path> {
const platform = yield* HostProcessPlatform;
const env = yield* readCommandLookupEnv;
if (!hasGraphicalFileManagerSession(platform, env)) {
return yield* new ExternalLauncherUnsupportedEditorError({ editor: "file-manager" });
}
const path = yield* Path.Path;
const args =
platform === "darwin"
? ["-R", input.path]
: platform === "win32"
? [`/select,"${input.path}"`]
: [path.dirname(input.path)];
const args = [fileManagerFolderPath(platform, input.path, path)];
yield* Effect.annotateCurrentSpan({
"externalLauncher.target": input.path,