feat(wsl): add support for finding WSL Claude root candidates

- Implemented a new IPC handler to find Windows UNC paths for WSL Claude root candidates.
- Introduced utility functions to list WSL distributions and resolve their home paths.
- Updated the configuration handlers to include the new functionality for detecting WSL Claude roots.
- Enhanced the UI to allow users to select WSL Claude root paths, including handling cases where the projects directory is missing.
- Refactored related components to integrate the new WSL functionality seamlessly.
This commit is contained in:
matt 2026-02-14 13:53:13 +09:00
parent ff0c8cc978
commit 07b3839d3f
8 changed files with 295 additions and 71 deletions

View file

@ -27,6 +27,7 @@ import { execFile } from 'child_process';
import { BrowserWindow, dialog, type IpcMain, type IpcMainInvokeEvent } from 'electron';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';
import {
type AppConfig,
@ -42,9 +43,14 @@ import { validateConfigUpdatePayload } from './configValidation';
import { validateTriggerId } from './guards';
import type { TriggerColor } from '@shared/constants/triggerColors';
import type { ClaudeRootFolderSelection, ClaudeRootInfo } from '@shared/types';
import type {
ClaudeRootFolderSelection,
ClaudeRootInfo,
WslClaudeRootCandidate,
} from '@shared/types';
const logger = createLogger('IPC:config');
const execFileAsync = promisify(execFile);
// Get singleton instance
const configManager = ConfigManager.getInstance();
@ -109,6 +115,7 @@ export function registerConfigHandlers(ipcMain: IpcMain): void {
ipcMain.handle('config:selectFolders', handleSelectFolders);
ipcMain.handle('config:selectClaudeRootFolder', handleSelectClaudeRootFolder);
ipcMain.handle('config:getClaudeRootInfo', handleGetClaudeRootInfo);
ipcMain.handle('config:findWslClaudeRoots', handleFindWslClaudeRoots);
// Editor handlers
ipcMain.handle('config:openInEditor', handleOpenInEditor);
@ -724,6 +731,97 @@ async function handleGetClaudeRootInfo(
}
}
function normalizeWslHomePath(home: string): string | null {
const trimmed = home.trim();
if (!trimmed.startsWith('/')) {
return null;
}
let normalized = path.posix.normalize(trimmed);
if (normalized.length > 1 && normalized.endsWith('/')) {
normalized = normalized.slice(0, -1);
}
return normalized;
}
function toWslUncPath(distro: string, posixPath: string): string {
const uncSuffix = posixPath.replace(/\//g, '\\');
return `\\\\wsl.localhost\\${distro}${uncSuffix}`;
}
async function listWslDistros(): Promise<string[]> {
const { stdout } = await execFileAsync('wsl.exe', ['-l', '-q'], { timeout: 4000 });
return stdout
.split(/\r?\n/)
.map((line) => line.trim())
.filter((line) => line.length > 0);
}
async function resolveWslHome(distro: string): Promise<string | null> {
try {
const { stdout } = await execFileAsync(
'wsl.exe',
['-d', distro, '--', 'sh', '-lc', 'printf %s "$HOME"'],
{ timeout: 4000 }
);
return normalizeWslHomePath(stdout);
} catch {
return null;
}
}
/**
* Handler for 'config:findWslClaudeRoots' - Find Windows UNC candidates for WSL Claude roots.
*/
async function handleFindWslClaudeRoots(
_event: IpcMainInvokeEvent
): Promise<ConfigResult<WslClaudeRootCandidate[]>> {
try {
if (process.platform !== 'win32') {
return { success: true, data: [] };
}
const distros = await listWslDistros();
if (distros.length === 0) {
return { success: true, data: [] };
}
const candidates: WslClaudeRootCandidate[] = [];
for (const distro of distros) {
const homePath = await resolveWslHome(distro);
if (!homePath) {
continue;
}
const claudePosixPath = path.posix.join(homePath, '.claude');
const claudeUncPath = toWslUncPath(distro, claudePosixPath);
const projectsPath = path.join(claudeUncPath, 'projects');
const hasProjectsDir = (() => {
try {
return fs.existsSync(projectsPath) && fs.statSync(projectsPath).isDirectory();
} catch {
return false;
}
})();
candidates.push({
distro,
path: claudeUncPath,
hasProjectsDir,
});
}
return { success: true, data: candidates };
} catch (error) {
logger.error('Error in config:findWslClaudeRoots:', error);
return {
success: false,
error: error instanceof Error ? error.message : 'Failed to detect WSL Claude paths',
};
}
}
// =============================================================================
// Cleanup
// =============================================================================
@ -751,6 +849,7 @@ export function removeConfigHandlers(ipcMain: IpcMain): void {
ipcMain.removeHandler('config:selectFolders');
ipcMain.removeHandler('config:selectClaudeRootFolder');
ipcMain.removeHandler('config:getClaudeRootInfo');
ipcMain.removeHandler('config:findWslClaudeRoots');
ipcMain.removeHandler('config:openInEditor');
logger.info('Config handlers removed');
}

View file

@ -1,4 +1,3 @@
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
@ -236,75 +235,7 @@ function getHomeDir(): string {
let claudeBasePathOverride: string | null = null;
function isWslEnvironment(): boolean {
if (process.platform !== 'linux') {
return false;
}
if (process.env.WSL_DISTRO_NAME || process.env.WSL_INTEROP) {
return true;
}
// Fallback for environments where WSL vars are not exported.
return os.release().toLowerCase().includes('microsoft');
}
function toWslPathFromWindowsPath(windowsPath: string): string | null {
const normalized = windowsPath.trim().replace(/\\/g, '/');
if (!normalized) {
return null;
}
if (normalized.startsWith('/mnt/')) {
return normalized;
}
const match = /^([a-zA-Z]):\/(.+)$/.exec(normalized);
if (!match) {
return null;
}
const drive = match[1].toLowerCase();
const rest = match[2];
return `/mnt/${drive}/${rest}`;
}
function getWslClaudeBaseCandidates(): string[] {
const candidates = new Set<string>();
const addCandidate = (baseHome: string | null | undefined): void => {
if (!baseHome) return;
const withClaude = path.posix.join(baseHome, '.claude');
candidates.add(path.posix.normalize(withClaude));
};
// WSL-native home (e.g. /home/<user>) should be preferred when present.
addCandidate(getHomeDir());
addCandidate(toWslPathFromWindowsPath(process.env.USERPROFILE ?? ''));
const homeDrivePath =
process.env.HOMEDRIVE && process.env.HOMEPATH
? `${process.env.HOMEDRIVE}${process.env.HOMEPATH}`
: '';
addCandidate(toWslPathFromWindowsPath(homeDrivePath));
if (process.env.USER) {
addCandidate(`/mnt/c/Users/${process.env.USER}`);
}
return Array.from(candidates);
}
function getDefaultClaudeBasePath(): string {
if (isWslEnvironment()) {
const candidates = getWslClaudeBaseCandidates();
const existing = candidates.find((candidate) => fs.existsSync(candidate));
if (existing) {
return existing;
}
}
return path.join(getHomeDir(), '.claude');
}

View file

@ -56,6 +56,9 @@ export const CONFIG_SELECT_CLAUDE_ROOT_FOLDER = 'config:selectClaudeRootFolder';
/** Get effective/default Claude root folder info */
export const CONFIG_GET_CLAUDE_ROOT_INFO = 'config:getClaudeRootInfo';
/** Find WSL Claude root candidates (Windows only) */
export const CONFIG_FIND_WSL_CLAUDE_ROOTS = 'config:findWslClaudeRoots';
/** Open config file in external editor */
export const CONFIG_OPEN_IN_EDITOR = 'config:openInEditor';

View file

@ -32,6 +32,7 @@ import {
CONFIG_ADD_IGNORE_REPOSITORY,
CONFIG_ADD_TRIGGER,
CONFIG_CLEAR_SNOOZE,
CONFIG_FIND_WSL_CLAUDE_ROOTS,
CONFIG_GET,
CONFIG_GET_CLAUDE_ROOT_INFO,
CONFIG_GET_TRIGGERS,
@ -64,6 +65,7 @@ import type {
SshConnectionStatus,
SshLastConnection,
TriggerTestResult,
WslClaudeRootCandidate,
} from '@shared/types';
// =============================================================================
@ -278,6 +280,9 @@ const electronAPI: ElectronAPI = {
getClaudeRootInfo: async (): Promise<ClaudeRootInfo> => {
return invokeIpcWithResult<ClaudeRootInfo>(CONFIG_GET_CLAUDE_ROOT_INFO);
},
findWslClaudeRoots: async (): Promise<WslClaudeRootCandidate[]> => {
return invokeIpcWithResult<WslClaudeRootCandidate[]>(CONFIG_FIND_WSL_CLAUDE_ROOTS);
},
openInEditor: async (): Promise<void> => {
return invokeIpcWithResult<void>(CONFIG_OPEN_IN_EDITOR);
},

View file

@ -39,6 +39,7 @@ import type {
TriggerTestResult,
UpdaterAPI,
WaterfallData,
WslClaudeRootCandidate,
} from '@shared/types';
export class HttpAPIClient implements ElectronAPI {
@ -424,6 +425,10 @@ export class HttpAPIClient implements ElectronAPI {
customPath: config.general.claudeRootPath,
};
},
findWslClaudeRoots: async (): Promise<WslClaudeRootCandidate[]> => {
console.warn('[HttpAPIClient] findWslClaudeRoots is not available in browser mode');
return [];
},
openInEditor: async (): Promise<void> => {
console.warn('[HttpAPIClient] openInEditor is not available in browser mode');
},

View file

@ -14,7 +14,7 @@ import { api } from '@renderer/api';
import { confirm } from '@renderer/components/common/ConfirmDialog';
import { useStore } from '@renderer/store';
import { getFullResetState } from '@renderer/store/utils/stateResetHelpers';
import { FolderOpen, Loader2, Monitor, RotateCcw, Server, Wifi, WifiOff } from 'lucide-react';
import { FolderOpen, Laptop, Loader2, Monitor, RotateCcw, Server, Wifi, WifiOff } from 'lucide-react';
import { SettingRow } from '../components/SettingRow';
import { SettingsSectionHeader } from '../components/SettingsSectionHeader';
@ -26,6 +26,7 @@ import type {
SshConfigHostEntry,
SshConnectionConfig,
SshConnectionProfile,
WslClaudeRootCandidate,
} from '@shared/types';
const authMethodOptions: readonly { value: SshAuthMethod; label: string }[] = [
@ -71,6 +72,9 @@ export const ConnectionSection = (): React.JSX.Element => {
const [claudeRootInfo, setClaudeRootInfo] = useState<ClaudeRootInfo | null>(null);
const [updatingClaudeRoot, setUpdatingClaudeRoot] = useState(false);
const [claudeRootError, setClaudeRootError] = useState<string | null>(null);
const [findingWslRoots, setFindingWslRoots] = useState(false);
const [wslCandidates, setWslCandidates] = useState<WslClaudeRootCandidate[]>([]);
const [showWslModal, setShowWslModal] = useState(false);
const loadProfiles = useCallback(async () => {
try {
@ -281,11 +285,67 @@ export const ConnectionSection = (): React.JSX.Element => {
await applyClaudeRootPath(null);
}, [applyClaudeRootPath]);
const applyWslCandidate = useCallback(
async (candidate: WslClaudeRootCandidate): Promise<void> => {
if (!candidate.hasProjectsDir) {
const proceed = await confirm({
title: 'WSL path missing projects directory',
message: `"${candidate.path}" does not contain a "projects" directory. Continue anyway?`,
confirmLabel: 'Use Path',
});
if (!proceed) {
return;
}
}
await applyClaudeRootPath(candidate.path);
setShowWslModal(false);
},
[applyClaudeRootPath]
);
const handleUseWslForClaude = useCallback(async (): Promise<void> => {
try {
setFindingWslRoots(true);
setClaudeRootError(null);
const candidates = await api.config.findWslClaudeRoots();
setWslCandidates(candidates);
if (candidates.length === 0) {
const pickManually = await confirm({
title: 'No WSL Claude paths found',
message: 'Could not find WSL distros with Claude data automatically. Select folder manually?',
confirmLabel: 'Select Folder',
});
if (pickManually) {
await handleSelectClaudeRootFolder();
}
return;
}
const candidatesWithProjects = candidates.filter((candidate) => candidate.hasProjectsDir);
if (candidatesWithProjects.length === 1) {
await applyWslCandidate(candidatesWithProjects[0]);
return;
}
setShowWslModal(true);
} catch (error) {
setClaudeRootError(
error instanceof Error ? error.message : 'Failed to detect WSL Claude root paths'
);
} finally {
setFindingWslRoots(false);
}
}, [applyWslCandidate, handleSelectClaudeRootFolder]);
const isConnecting = connectionState === 'connecting';
const isConnected = connectionState === 'connected';
const isCustomClaudeRoot = Boolean(claudeRootInfo?.customPath);
const resolvedClaudeRootPath = claudeRootInfo?.resolvedPath ?? '~/.claude';
const defaultClaudeRootPath = claudeRootInfo?.defaultPath ?? '~/.claude';
const isWindowsStyleDefaultPath =
/^[a-zA-Z]:\\/.test(defaultClaudeRootPath) || defaultClaudeRootPath.startsWith('\\\\');
const inputClass = 'w-full rounded-md border px-3 py-1.5 text-sm focus:outline-none focus:ring-1';
const inputStyle = {
@ -349,6 +409,27 @@ export const ConnectionSection = (): React.JSX.Element => {
Use Auto-Detect
</span>
</button>
{isWindowsStyleDefaultPath && (
<button
onClick={() => void handleUseWslForClaude()}
disabled={updatingClaudeRoot || findingWslRoots}
className="rounded-md px-4 py-1.5 text-sm transition-colors disabled:opacity-50"
style={{
backgroundColor: 'var(--color-surface-raised)',
color: 'var(--color-text-secondary)',
}}
>
<span className="flex items-center gap-2">
{findingWslRoots ? (
<Loader2 className="size-3 animate-spin" />
) : (
<Laptop className="size-3" />
)}
Using Linux/WSL?
</span>
</button>
)}
</div>
{claudeRootError && (
@ -357,6 +438,93 @@ export const ConnectionSection = (): React.JSX.Element => {
</div>
)}
{showWslModal && (
<div className="fixed inset-0 z-50 flex items-center justify-center">
<button
className="absolute inset-0 cursor-default"
style={{ backgroundColor: 'rgba(0, 0, 0, 0.6)' }}
onClick={() => setShowWslModal(false)}
aria-label="Close WSL path modal"
tabIndex={-1}
/>
<div
className="relative mx-4 w-full max-w-2xl rounded-lg border p-5 shadow-xl"
style={{
backgroundColor: 'var(--color-surface-overlay)',
borderColor: 'var(--color-border-emphasis)',
}}
>
<h3 className="text-sm font-semibold" style={{ color: 'var(--color-text)' }}>
Select WSL Claude Root
</h3>
<p className="mt-1 text-xs" style={{ color: 'var(--color-text-muted)' }}>
Detected WSL distributions and Claude root candidates
</p>
<div className="mt-4 space-y-2">
{wslCandidates.map((candidate) => (
<div
key={`${candidate.distro}:${candidate.path}`}
className="flex items-center justify-between gap-3 rounded-md border px-3 py-2"
style={{ borderColor: 'var(--color-border)' }}
>
<div className="min-w-0">
<p className="text-xs font-medium" style={{ color: 'var(--color-text)' }}>
{candidate.distro}
</p>
<p
className="truncate font-mono text-[11px]"
style={{ color: 'var(--color-text-muted)' }}
>
{candidate.path}
</p>
{!candidate.hasProjectsDir && (
<p className="text-[11px] text-amber-400">No projects directory detected</p>
)}
</div>
<button
onClick={() => void applyWslCandidate(candidate)}
className="rounded-md px-3 py-1.5 text-xs transition-colors"
style={{
backgroundColor: 'var(--color-surface-raised)',
color: 'var(--color-text)',
}}
>
Use This Path
</button>
</div>
))}
</div>
<div className="mt-4 flex items-center justify-end gap-2">
<button
onClick={() => setShowWslModal(false)}
className="rounded-md border px-3 py-1.5 text-xs transition-colors hover:bg-white/5"
style={{
borderColor: 'var(--color-border)',
color: 'var(--color-text-secondary)',
}}
>
Cancel
</button>
<button
onClick={() => {
setShowWslModal(false);
void handleSelectClaudeRootFolder();
}}
className="rounded-md px-3 py-1.5 text-xs transition-colors"
style={{
backgroundColor: 'var(--color-surface-raised)',
color: 'var(--color-text)',
}}
>
Select Folder Manually
</button>
</div>
</div>
</div>
)}
<SettingsSectionHeader title="Remote Connection" />
<p className="text-sm" style={{ color: 'var(--color-text-muted)' }}>
Connect to a remote machine to view Claude Code sessions running there

View file

@ -91,6 +91,8 @@ export interface ConfigAPI {
selectClaudeRootFolder: () => Promise<ClaudeRootFolderSelection | null>;
/** Get resolved Claude root path info for local mode */
getClaudeRootInfo: () => Promise<ClaudeRootInfo>;
/** Find Windows WSL Claude root candidates (UNC paths) */
findWslClaudeRoots: () => Promise<WslClaudeRootCandidate[]>;
/** Opens the config JSON file in an external editor */
openInEditor: () => Promise<void>;
/** Pin a session for a project */
@ -117,6 +119,15 @@ export interface ClaudeRootFolderSelection {
hasProjectsDir: boolean;
}
export interface WslClaudeRootCandidate {
/** WSL distribution name (e.g. Ubuntu) */
distro: string;
/** Candidate Claude root path in UNC format */
path: string;
/** True if this root contains "projects" directory */
hasProjectsDir: boolean;
}
// =============================================================================
// Session API
// =============================================================================

View file

@ -70,6 +70,7 @@ export interface MockElectronAPI {
selectFolders: ReturnType<typeof vi.fn>;
selectClaudeRootFolder: ReturnType<typeof vi.fn>;
getClaudeRootInfo: ReturnType<typeof vi.fn>;
findWslClaudeRoots: ReturnType<typeof vi.fn>;
openInEditor: ReturnType<typeof vi.fn>;
pinSession: ReturnType<typeof vi.fn>;
unpinSession: ReturnType<typeof vi.fn>;
@ -171,6 +172,7 @@ export function createMockElectronAPI(): MockElectronAPI {
resolvedPath: '~/.claude',
customPath: null,
}),
findWslClaudeRoots: vi.fn().mockResolvedValue([]),
openInEditor: vi.fn(),
pinSession: vi.fn(),
unpinSession: vi.fn(),