Improve WSL project opening from command palette

This commit is contained in:
Josh Gratton committed 2026-06-02 15:57:49 -04:00
1 parent 9752f9e9ce
commit b9e12d3377
3 files changed
+159 -12

No files matched your search

+107 -12
View File
@@ -81,6 +81,7 @@ import {
} from "../store";
import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../terminalUiStateStore";
import { buildThreadRouteParams, resolveThreadRouteTarget } from "../threadRoutes";
import { parseWslUncPath, type WslUncPath } from "../wslPaths";
import {
ADDON_ICON_CLASS,
buildBrowseGroups,
@@ -145,12 +146,20 @@ function getEnvironmentBrowsePlatform(os: string | null | undefined): string {
return typeof navigator === "undefined" ? "" : navigator.platform;
}
function isDesktopLocalWslInstance(instanceId: string | undefined): boolean {
return instanceId?.startsWith("wsl:") === true;
}
interface AddProjectEnvironmentOption {
readonly environmentId: EnvironmentId;
readonly label: string;
readonly isPrimary: boolean;
}
interface WslProjectSelection extends WslUncPath {
readonly environmentId: EnvironmentId;
}
type AddProjectRemoteProviderKind = Extract<
SourceControlProviderKind,
"github" | "gitlab" | "bitbucket" | "azure-devops"
@@ -471,6 +480,15 @@ function OpenCommandPaletteDialog() {
savedEnvironmentRegistry,
savedEnvironmentRuntimeById,
]);
const wslAddProjectEnvironmentOption = useMemo(
() =>
addProjectEnvironmentOptions.find((option) =>
isDesktopLocalWslInstance(
savedEnvironmentRegistry[option.environmentId]?.desktopLocal?.instanceId,
),
) ?? null,
[addProjectEnvironmentOptions, savedEnvironmentRegistry],
);
const defaultAddProjectEnvironmentId = addProjectEnvironmentOptions[0]?.environmentId ?? null;
const browseEnvironmentId = addProjectEnvironmentId ?? defaultAddProjectEnvironmentId;
const browseEnvironmentPlatform = useMemo(() => {
@@ -587,6 +605,26 @@ function OpenCommandPaletteDialog() {
[browseEnvironmentId, currentProjectCwdForBrowse, fetchBrowseResult, queryClient],
);
const resolveWslUncProjectSelection = useCallback(
(path: string): WslProjectSelection | null => {
const parsed = parseWslUncPath(path);
if (!parsed) {
return null;
}
const wslRecords = Object.values(savedEnvironmentRegistry).filter((record) =>
isDesktopLocalWslInstance(record.desktopLocal?.instanceId),
);
const exactRecord = wslRecords.find(
(record) =>
record.desktopLocal?.instanceId.toLowerCase() === `wsl:${parsed.distro}`.toLowerCase(),
);
const record = exactRecord ?? (wslRecords.length === 1 ? wslRecords[0] : null);
return record ? { ...parsed, environmentId: record.environmentId } : null;
},
[savedEnvironmentRegistry],
);
// Prefetch the parent and the most likely next child so browse navigation
// stays warm without scanning every child directory in large trees.
useEffect(() => {
@@ -1050,6 +1088,21 @@ function OpenCommandPaletteDialog() {
},
});
if (wslAddProjectEnvironmentOption) {
actionItems.push({
kind: "action",
value: "action:add-project:wsl-folder",
searchTerms: ["add project", "open", "wsl", "linux", "ubuntu", "folder", "directory"],
title: "Open WSL folder",
description: wslAddProjectEnvironmentOption.label,
icon: <FolderPlusIcon className={ITEM_ICON_CLASS} />,
keepOpen: true,
run: async () => {
startAddProjectBrowse(wslAddProjectEnvironmentOption.environmentId);
},
});
}
actionItems.push({
kind: "action",
value: "action:settings",
@@ -1072,13 +1125,17 @@ function OpenCommandPaletteDialog() {
threadSearchItems: allThreadItems,
});
const handleAddProject = useCallback(
async (rawCwd: string) => {
if (!browseEnvironmentId) return;
const api = readEnvironmentApi(browseEnvironmentId);
const handleAddProjectForEnvironment = useCallback(
async (input: {
readonly environmentId: EnvironmentId;
readonly rawCwd: string;
readonly platform: string;
readonly currentProjectCwd: string | null;
}) => {
const api = readEnvironmentApi(input.environmentId);
if (!api) return;
if (isUnsupportedWindowsProjectPath(rawCwd.trim(), browseEnvironmentPlatform)) {
if (isUnsupportedWindowsProjectPath(input.rawCwd.trim(), input.platform)) {
toastManager.add(
stackedThreadToast({
type: "error",
@@ -1089,7 +1146,7 @@ function OpenCommandPaletteDialog() {
return;
}
if (isExplicitRelativeProjectPath(rawCwd.trim()) && !currentProjectCwdForBrowse) {
if (isExplicitRelativeProjectPath(input.rawCwd.trim()) && !input.currentProjectCwd) {
toastManager.add(
stackedThreadToast({
type: "error",
@@ -1100,11 +1157,11 @@ function OpenCommandPaletteDialog() {
return;
}
const cwd = resolveProjectPathForDispatch(rawCwd, currentProjectCwdForBrowse);
const cwd = resolveProjectPathForDispatch(input.rawCwd, input.currentProjectCwd);
if (cwd.length === 0) return;
const existing = findProjectByPath(
projects.filter((project) => project.environmentId === browseEnvironmentId),
projects.filter((project) => project.environmentId === input.environmentId),
cwd,
);
if (existing) {
@@ -1144,7 +1201,7 @@ function OpenCommandPaletteDialog() {
},
createdAt: new Date().toISOString(),
});
await handleNewThread(scopeProjectRef(browseEnvironmentId, projectId), {
await handleNewThread(scopeProjectRef(input.environmentId, projectId), {
envMode: settings.defaultThreadEnvMode,
}).catch(() => undefined);
setOpen(false);
@@ -1159,9 +1216,6 @@ function OpenCommandPaletteDialog() {
}
},
[
browseEnvironmentId,
browseEnvironmentPlatform,
currentProjectCwdForBrowse,
handleNewThread,
navigate,
projects,
@@ -1172,6 +1226,24 @@ function OpenCommandPaletteDialog() {
],
);
const handleAddProject = useCallback(
async (rawCwd: string) => {
if (!browseEnvironmentId) return;
await handleAddProjectForEnvironment({
environmentId: browseEnvironmentId,
rawCwd,
platform: browseEnvironmentPlatform,
currentProjectCwd: currentProjectCwdForBrowse,
});
},
[
browseEnvironmentId,
browseEnvironmentPlatform,
currentProjectCwdForBrowse,
handleAddProjectForEnvironment,
],
);
function getDefaultCloneParentPath(environmentId: EnvironmentId): string {
return getAddProjectInitialQueryForEnvironment(environmentId);
}
@@ -1532,14 +1604,37 @@ function OpenCommandPaletteDialog() {
if (!pickedPath) {
return;
}
const pickedWslPath = parseWslUncPath(pickedPath);
if (pickedWslPath) {
const wslSelection = resolveWslUncProjectSelection(pickedPath);
if (!wslSelection) {
toastManager.add(
stackedThreadToast({
type: "error",
title: "Could not add WSL project",
description: "Start the WSL backend, then choose the folder again.",
}),
);
return;
}
await handleAddProjectForEnvironment({
environmentId: wslSelection.environmentId,
rawCwd: wslSelection.linuxPath,
platform: "Linux",
currentProjectCwd: null,
});
return;
}
await handleAddProject(pickedPath);
}, [
browseEnvironmentId,
canOpenProjectFromFileManager,
fileManagerInitialPath,
handleAddProject,
handleAddProjectForEnvironment,
isPickingProjectFolder,
primaryEnvironmentId,
resolveWslUncProjectSelection,
]);
return (
+24
View File
@@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import { parseWslUncPath } from "./wslPaths";
describe("parseWslUncPath", () => {
it("parses wsl.localhost UNC paths into distro and POSIX path", () => {
expect(parseWslUncPath("\\\\wsl.localhost\\Ubuntu-22.04\\home\\josh\\repo")).toEqual({
distro: "Ubuntu-22.04",
linuxPath: "/home/josh/repo",
});
});
it("parses wsl$ UNC roots as distro root", () => {
expect(parseWslUncPath("\\\\wsl$\\Debian")).toEqual({
distro: "Debian",
linuxPath: "/",
});
});
it("rejects non-WSL paths and invalid distro names", () => {
expect(parseWslUncPath("C:\\Users\\Josh\\repo")).toBeNull();
expect(parseWslUncPath("\\\\wsl.localhost\\bad!name\\home")).toBeNull();
});
});
+28
View File
@@ -0,0 +1,28 @@
export interface WslUncPath {
readonly distro: string;
readonly linuxPath: string;
}
const WSL_UNC_PREFIXES = ["\\\\wsl.localhost\\", "\\\\wsl$\\"] as const;
const WSL_DISTRO_NAME_PATTERN = /^[A-Za-z0-9._-]+$/;
export function parseWslUncPath(input: string): WslUncPath | null {
const normalized = input.trim().replaceAll("/", "\\");
const prefix = WSL_UNC_PREFIXES.find((candidate) =>
normalized.toLowerCase().startsWith(candidate.toLowerCase()),
);
if (!prefix) {
return null;
}
const rest = normalized.slice(prefix.length);
const [distro, ...segments] = rest.split("\\").filter((segment) => segment.length > 0);
if (!distro || !WSL_DISTRO_NAME_PATTERN.test(distro)) {
return null;
}
return {
distro,
linuxPath: segments.length === 0 ? "/" : `/${segments.join("/")}`,
};
}