fix: harden open in folder behavior
This commit is contained in:
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* () {
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -51,7 +51,11 @@ import {
|
||||
resolveExternalWebLinkHost,
|
||||
showExternalLinkContextMenu,
|
||||
} from "./chat/externalLinkContextMenu";
|
||||
import { buildFileLinkContextMenuItems } from "./chat/fileLinkContextMenu";
|
||||
import {
|
||||
buildFileLinkContextMenuItems,
|
||||
canRevealFileLinkInManager,
|
||||
resolveFileLinkEnvironmentId,
|
||||
} from "./chat/fileLinkContextMenu";
|
||||
import { hasSpecificPierreIconForFileName, syntheticFileNameForLanguageId } from "../pierre-icons";
|
||||
import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
|
||||
import { Button } from "./ui/button";
|
||||
@@ -85,6 +89,7 @@ import { readLocalApi } from "../localApi";
|
||||
import { cn } from "../lib/utils";
|
||||
import { useRightPanelStore } from "../rightPanelStore";
|
||||
import { useActiveEnvironmentId } from "../state/entities";
|
||||
import { useEnvironment } from "../state/environments";
|
||||
import { serverEnvironment } from "../state/server";
|
||||
import { assetEnvironment } from "../state/assets";
|
||||
import { usePreparedConnection } from "../state/session";
|
||||
@@ -1394,15 +1399,17 @@ function ChatMarkdown({
|
||||
reportFailure: false,
|
||||
});
|
||||
const activeEnvironmentId = useActiveEnvironmentId();
|
||||
const environmentId = threadRef?.environmentId ?? activeEnvironmentId;
|
||||
const environmentId = resolveFileLinkEnvironmentId(threadRef?.environmentId, activeEnvironmentId);
|
||||
const environment = useEnvironment(environmentId);
|
||||
const preparedConnection = usePreparedConnection(environmentId);
|
||||
const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId));
|
||||
const availableEditors = serverConfig?.availableEditors ?? [];
|
||||
const openInPreferredEditor = useOpenInPreferredEditor(environmentId, availableEditors);
|
||||
const canRevealInFileManager =
|
||||
preparedConnection._tag === "Some" &&
|
||||
serverConfig?.environment.capabilities.fileManagerReveal === true &&
|
||||
availableEditors.includes("file-manager");
|
||||
const canRevealInFileManager = canRevealFileLinkInManager({
|
||||
connectionPhase: environment?.connection.phase,
|
||||
supportsRevealRpc: serverConfig?.environment.capabilities.fileManagerReveal === true,
|
||||
availableEditors,
|
||||
});
|
||||
const diffThemeName = resolveDiffThemeName(resolvedTheme);
|
||||
const markdownFileLinkMetaByHref = useMemo(() => {
|
||||
const metaByHref = new Map<
|
||||
|
||||
@@ -1,6 +1,47 @@
|
||||
import { EnvironmentId } from "@t3tools/contracts";
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { buildFileLinkContextMenuItems } from "./fileLinkContextMenu";
|
||||
import {
|
||||
buildFileLinkContextMenuItems,
|
||||
canRevealFileLinkInManager,
|
||||
resolveFileLinkEnvironmentId,
|
||||
} from "./fileLinkContextMenu";
|
||||
|
||||
describe("file link environment", () => {
|
||||
it("routes through the thread environment before the active environment", () => {
|
||||
const threadEnvironmentId = EnvironmentId.make("thread-environment");
|
||||
expect(
|
||||
resolveFileLinkEnvironmentId(threadEnvironmentId, EnvironmentId.make("active-environment")),
|
||||
).toBe(threadEnvironmentId);
|
||||
});
|
||||
|
||||
it("falls back to the active environment without thread context", () => {
|
||||
const activeEnvironmentId = EnvironmentId.make("active-environment");
|
||||
expect(resolveFileLinkEnvironmentId(undefined, activeEnvironmentId)).toBe(activeEnvironmentId);
|
||||
});
|
||||
|
||||
it.each([
|
||||
{
|
||||
connectionPhase: "reconnecting",
|
||||
supportsRevealRpc: true,
|
||||
availableEditors: ["file-manager"],
|
||||
},
|
||||
{ connectionPhase: "connected", supportsRevealRpc: false, availableEditors: ["file-manager"] },
|
||||
{ connectionPhase: "connected", supportsRevealRpc: true, availableEditors: [] },
|
||||
] as const)("hides reveal when support is incomplete", (input) => {
|
||||
expect(canRevealFileLinkInManager(input)).toBe(false);
|
||||
});
|
||||
|
||||
it("allows reveal only when connected with rpc and file manager support", () => {
|
||||
expect(
|
||||
canRevealFileLinkInManager({
|
||||
connectionPhase: "connected",
|
||||
supportsRevealRpc: true,
|
||||
availableEditors: ["file-manager"],
|
||||
}),
|
||||
).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("chat file link context menu", () => {
|
||||
it("puts Open in folder first when the environment supports it", () => {
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import type { ContextMenuItem } from "@t3tools/contracts";
|
||||
import type { ContextMenuItem, EditorId, EnvironmentId } from "@t3tools/contracts";
|
||||
|
||||
export type FileLinkContextMenuAction =
|
||||
| "open-in-folder"
|
||||
@@ -7,6 +7,25 @@ export type FileLinkContextMenuAction =
|
||||
| "copy-relative"
|
||||
| "copy-full";
|
||||
|
||||
export function resolveFileLinkEnvironmentId(
|
||||
threadEnvironmentId: EnvironmentId | undefined,
|
||||
activeEnvironmentId: EnvironmentId | null,
|
||||
): EnvironmentId | null {
|
||||
return threadEnvironmentId ?? activeEnvironmentId;
|
||||
}
|
||||
|
||||
export function canRevealFileLinkInManager(input: {
|
||||
readonly connectionPhase: string | undefined;
|
||||
readonly supportsRevealRpc: boolean;
|
||||
readonly availableEditors: ReadonlyArray<EditorId>;
|
||||
}): boolean {
|
||||
return (
|
||||
input.connectionPhase === "connected" &&
|
||||
input.supportsRevealRpc &&
|
||||
input.availableEditors.includes("file-manager")
|
||||
);
|
||||
}
|
||||
|
||||
export function buildFileLinkContextMenuItems(input: {
|
||||
readonly canRevealInFileManager: boolean;
|
||||
readonly canOpenInBrowser: boolean;
|
||||
|
||||
@@ -108,6 +108,27 @@ describe("resolveMarkdownFileLinkTarget", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves the exact file path used by open in folder", () => {
|
||||
expect(resolveMarkdownFileLinkMeta("src/file%20name.ts:12:4", "/repo/project")).toMatchObject({
|
||||
filePath: "/repo/project/src/file name.ts",
|
||||
targetPath: "/repo/project/src/file name.ts:12:4",
|
||||
line: 12,
|
||||
column: 4,
|
||||
});
|
||||
expect(resolveMarkdownFileLinkMeta("/tmp/report.ts:9", "/repo/project")).toMatchObject({
|
||||
filePath: "/tmp/report.ts",
|
||||
targetPath: "/tmp/report.ts:9",
|
||||
line: 9,
|
||||
});
|
||||
});
|
||||
|
||||
it("resolves missing paths without requiring browser-side file access", () => {
|
||||
expect(resolveMarkdownFileLinkMeta("src/not-created-yet.ts", "/repo/project")).toMatchObject({
|
||||
filePath: "/repo/project/src/not-created-yet.ts",
|
||||
workspaceRelativePath: "src/not-created-yet.ts",
|
||||
});
|
||||
});
|
||||
|
||||
it("normalizes slash-prefixed windows drive paths before resolving", () => {
|
||||
expect(
|
||||
resolveMarkdownFileLinkTarget(
|
||||
|
||||
@@ -5,8 +5,8 @@ const WINDOWS_DRIVE_PATH_PATTERN = /^[A-Za-z]:[\\/]/;
|
||||
const WINDOWS_UNC_PATH_PATTERN = /^\\\\/;
|
||||
const EXTERNAL_SCHEME_PATTERN = /^([A-Za-z][A-Za-z0-9+.-]*):(.*)$/;
|
||||
const RELATIVE_PATH_PREFIX_PATTERN = /^(~\/|\.{1,2}\/)/;
|
||||
const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._-]+(?:\/[A-Za-z0-9._-]+)+(?::\d+){0,2}$/;
|
||||
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._-]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
|
||||
const RELATIVE_FILE_PATH_PATTERN = /^[A-Za-z0-9._ -]+(?:\/[A-Za-z0-9._ -]+)+(?::\d+){0,2}$/;
|
||||
const RELATIVE_FILE_NAME_PATTERN = /^[A-Za-z0-9._ -]+\.[A-Za-z0-9_-]+(?::\d+){0,2}$/;
|
||||
const POSITION_SUFFIX_PATTERN = /:\d+(?::\d+)?$/;
|
||||
const POSITION_ONLY_PATTERN = /^\d+(?::\d+)?$/;
|
||||
// Standard OS and dev-container roots; deliberately excludes app-route-ish
|
||||
|
||||
Reference in new issue
Block a user