Skip to content
TypeScript

fix(web): open remote preview files in VS Code (#19898)

fixfonee7f8dc02cbd
diff --git a/apps/web/src/components/chat/OpenInPicker.tsx b/apps/web/src/components/chat/OpenInPicker.tsx
index 050725964af..5c6ddc06746 100644
--- a/apps/web/src/components/chat/OpenInPicker.tsx
+++ b/apps/web/src/components/chat/OpenInPicker.tsx
@@ -208,6 +208,7 @@ export const OpenInPicker = memo(function OpenInPicker({
keybindings,
availableEditors,
openInCwd,
+ openInCwdIsFile = false,
presentation = "toolbar",
compact = false,
enableShortcut = true,
@@ -218,6 +219,7 @@ export const OpenInPicker = memo(function OpenInPicker({
keybindings: ResolvedKeybindingsConfig;
availableEditors: ReadonlyArray<EditorId>;
openInCwd: string | null;
+ openInCwdIsFile?: boolean;
presentation?: "toolbar" | "menu";
compact?: boolean;
enableShortcut?: boolean;
@@ -251,6 +253,7 @@ export const OpenInPicker = memo(function OpenInPicker({
editor,
host: remote.host.host,
absolutePath: openInCwd,
+ isFile: openInCwdIsFile,
});
if (url === undefined) return;
// Only record hint-seen/preferred when the shell actually accepted
diff --git a/apps/web/src/components/files/FilePreviewPanel.tsx b/apps/web/src/components/files/FilePreviewPanel.tsx
index 07cee367aae..3ffcb29369b 100644
--- a/apps/web/src/components/files/FilePreviewPanel.tsx
+++ b/apps/web/src/components/files/FilePreviewPanel.tsx
@@ -1243,6 +1243,7 @@ export default function FilePreviewPanel({
availableEditors={availableEditors}
enableShortcut={openInShortcutEnabled}
openInCwd={absolutePath}
+ openInCwdIsFile={file.isFile}
compact
revealFileManager={revealsInFileManager}
/>
diff --git a/apps/web/src/components/files/projectFilesQueryState.test.tsx b/apps/web/src/components/files/projectFilesQueryState.test.tsx
index 4a7f70a988f..3688848460d 100644
--- a/apps/web/src/components/files/projectFilesQueryState.test.tsx
+++ b/apps/web/src/components/files/projectFilesQueryState.test.tsx
@@ -8,6 +8,7 @@ import {
} from "@t3tools/contracts";
import * as Cause from "effect/Cause";
import * as Effect from "effect/Effect";
+import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import { AsyncResult, Atom, AtomRegistry } from "effect/reactivity";
import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
@@ -506,6 +507,51 @@ describe("project query refresh", () => {
}
});
+ it("confirms files from reads and preserves that identity until a definitive folder result", () => {
+ const contents = file("# readme");
+ const success = AsyncResult.success(contents);
+ const failure = (kind: ProjectReadFileError["failure"], cached = false) =>
+ AsyncResult.failure(
+ Cause.fail(
+ new ProjectReadFileError({ cwd: "/repo", relativePath: "src/preview.ts", failure: kind }),
+ ),
+ { previousSuccess: cached ? Option.some(success) : Option.none() },
+ );
+ const states = [
+ ["initial read", AsyncResult.initial(true), false, false, null],
+ ["unknown path", failure("operation_failed"), false, false, null],
+ ["binary file", failure("binary_file"), true, false, null],
+ ["read success", success, true, false, contents],
+ ["pending refresh", AsyncResult.waiting(success), true, false, contents],
+ ["transient refresh failure", failure("operation_failed", true), true, false, contents],
+ ["became a folder", failure("path_not_file", true), false, true, contents],
+ ] as const;
+ const readAtom = Atom.make<
+ AsyncResult.AsyncResult<ProjectReadFileResult, ProjectReadFileError>
+ >(AsyncResult.initial(true));
+ const registry = AtomRegistry.make();
+ const unmount = registry.mount(readAtom);
+ projectMocks.readFile.mockReturnValue(readAtom);
+ projectMocks.optimisticFile.mockReturnValue(Atom.make(null));
+ atomHooks.registry = registry;
+
+ try {
+ for (const [label, result, isFile, isNotFile, data] of states) {
+ registry.set(readAtom, result);
+ reactHooks.beginRender();
+ const query = useProjectFileQuery(environmentId, "/repo", "src/preview.ts");
+ expect(query.isFile, label).toBe(isFile);
+ expect(query.isNotFile, label).toBe(isNotFile);
+ expect(query.data, label).toBe(data);
+ expect(query.isPending, label).toBe(result.waiting);
+ }
+ } finally {
+ unmount();
+ registry.dispose();
+ atomHooks.registry = null;
+ }
+ });
+
it("retains decoded read failure context for the connected environment", async () => {
const error = decodeReadError({
_tag: "ProjectReadFileError",
diff --git a/apps/web/src/components/files/projectFilesQueryState.ts b/apps/web/src/components/files/projectFilesQueryState.ts
index b4f2b30f470..b68f7a3f951 100644
--- a/apps/web/src/components/files/projectFilesQueryState.ts
+++ b/apps/web/src/components/files/projectFilesQueryState.ts
@@ -50,6 +50,7 @@ interface ProjectQueryState<A> {
interface ProjectFileQueryState extends ProjectQueryState<ProjectReadFileResult> {
readonly readError: ProjectReadFileError | null;
readonly isNotFile: boolean;
+ readonly isFile: boolean;
}
function getProjectEntriesQueryAtom(
@@ -258,16 +259,19 @@ export function useProjectFileQuery(
const result = useAtomValue(atom);
const refreshAtom = useAtomRefresh(atom);
const refresh = () => refreshAtom();
- const data = Option.getOrNull(AsyncResult.value(result));
const optimisticResult = useAtomValue(
optimisticFileAtom(environmentId, cwd, relativePath ?? EMPTY_PROJECT_FILE_PATH),
);
const optimisticFile = relativePath === null ? null : optimisticResult;
const cause = failureCause(result);
const readError = isProjectReadFileError(cause) ? cause : null;
+ const data = canReadFiles
+ ? (optimisticFile?.data ?? Option.getOrNull(AsyncResult.value(result)))
+ : null;
+ const isNotFile = readError?.failure === "path_not_file";
return {
- data: canReadFiles ? (optimisticFile?.data ?? data) : null,
+ data,
error:
!isQueryEnabled || fileAccess.isPending
? null
@@ -276,7 +280,9 @@ export function useProjectFileQuery(
: (fileAccess.error ?? "This connection cannot read host files."),
isPending: isQueryEnabled && (fileAccess.isPending || result.waiting),
readError,
- isNotFile: readError?.failure === "path_not_file",
+ isNotFile,
+ // A folder error overrides cached contents retained across failed refreshes.
+ isFile: !isNotFile && (data !== null || readError?.failure === "binary_file"),
refresh,
};
}
diff --git a/apps/web/src/remoteOpen.test.ts b/apps/web/src/remoteOpen.test.ts
index d64161c6691..339423a7da0 100644
--- a/apps/web/src/remoteOpen.test.ts
+++ b/apps/web/src/remoteOpen.test.ts
@@ -177,6 +177,33 @@ describe("buildRemoteOpenUrl", () => {
);
});
+ it("adds a file position only for confirmed VS Code-family file targets", () => {
+ const targets = [
+ ["vscode", true, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
+ ["cursor", true, "cursor://vscode-remote/ssh-remote+sol/tmp/my%20file.md:1:1"],
+ ["vscode", false, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
+ ["vscode", undefined, "vscode://vscode-remote/ssh-remote+sol/tmp/my%20file.md"],
+ ["zed", true, "zed://ssh/sol/tmp/my%20file.md"],
+ [
+ "pycharm",
+ true,
+ "jetbrains://gateway/ssh/environment?h=sol&launchIde=true&ideHint=PY&projectHint=%2Ftmp%2Fmy+file.md",
+ ],
+ ] as const;
+
+ for (const [editor, isFile, expected] of targets) {
+ expect(
+ buildRemoteOpenUrl({
+ editor,
+ host: "sol",
+ absolutePath: "/tmp/my file.md",
+ ...(isFile === undefined ? {} : { isFile }),
+ }),
+ `${editor}, isFile=${isFile}`,
+ ).toBe(expected);
+ }
+ });
+
it("returns undefined for editors without remote support", () => {
expect(buildRemoteOpenUrl({ editor: "kiro", host: "sol", absolutePath: "/tmp/x" })).toBe(
undefined,
diff --git a/packages/contracts/src/editor.ts b/packages/contracts/src/editor.ts
index f40d1823b9a..afdb04dd59f 100644
--- a/packages/contracts/src/editor.ts
+++ b/packages/contracts/src/editor.ts
@@ -181,6 +181,7 @@ export const buildRemoteOpenUrl = (input: {
readonly editor: EditorId;
readonly host: string;
readonly absolutePath: string;
+ readonly isFile?: boolean;
}): string | undefined => {
const scheme = remoteSchemeForEditor(input.editor);
if (scheme === undefined) {
@@ -210,7 +211,8 @@ export const buildRemoteOpenUrl = (input: {
return `${scheme}://ssh/${encodedHost}${encodedZedPath}`;
}
const encodedPath = rootedPath.split("/").map(encodeURIComponent).join("/");
- return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}`;
+ // VS Code opens a bare vscode-remote path as a folder; a trailing position makes it open the file.
+ return `${scheme}://vscode-remote/ssh-remote+${encodedHost}${encodedPath}${input.isFile ? ":1:1" : ""}`;
};
export const RemoteOpenTargetKind = Schema.Literals(["tailscale", "mdns"]);