Files
archived-orca/src/shared/wsl-paths.ts
T
a7f9888d01 feat(wsl): offer the drive-path conversion in project settings, and convert the window's stores (D1c-4d) (#33143)
* feat(wsl): offer the drive-path conversion in project settings, and convert the window's stores (D1c-4d)

* fix(wsl): tell the user why saved state blocked the drive-path conversion

A refusal over saved state now names the cause: a folder with different details under both its WSL
path and its drive path, or a project that also holds a repository that isn't converting. An older
host's refusal without a cause keeps the general message.

* fix(wsl): explain a conversion refused because the distro's /mnt/<letter> is not that Windows drive

Co-Authored-By: Claude <noreply@anthropic.com>

* test(wsl): give the renderer-rewrite journal fixture the profile the journal now records

* test(wsl): give the e2e conversion journal the profile field the journal now requires

* test(wsl): carry the #33135 startup tests onto the window-store journal: it waits for the window, and finishes on any platform once the gate is open

* test(wsl): off Windows the conversion IPC still reports startup's outcomes, as the journal can finish on any platform

---------

Co-authored-by: OrcaWin <293788423+OrcaWin@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
2026-10-28 23:58:39 -07:00

185 lines
7.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
export type WslUncPathInfo = {
distro: string
linuxPath: string
}
const SLASH_CHAR_CODE = '/'.charCodeAt(0)
const BACKSLASH_CHAR_CODE = '\\'.charCodeAt(0)
function isPathSeparatorCharCode(charCode: number): boolean {
return charCode === SLASH_CHAR_CODE || charCode === BACKSLASH_CHAR_CODE
}
export function parseWslUncPath(path: string): WslUncPathInfo | null {
// Skip normalization/regex for non-UNC paths on the FS-event hot path.
if (
!isPathSeparatorCharCode(path.charCodeAt(0)) ||
!isPathSeparatorCharCode(path.charCodeAt(1))
) {
return null
}
const normalized = path.includes('\\') ? path.replace(/\\/g, '/') : path
const match = normalized.match(/^\/\/(wsl\.localhost|wsl\$)\/([^/]+)(\/.*)?$/i)
if (!match) {
return null
}
return {
distro: match[2],
linuxPath: match[3] || '/'
}
}
/** Linux form of `value` when it is a UNC path of `distro` (either spelling, either slash). */
export function wslUncPathToLinux(value: string, distro: string): string | null {
const parsed = parseWslUncPath(value)
// WSL resolves distro names case-insensitively, and so does every UNC path to one.
return parsed && parsed.distro.toLowerCase() === distro.toLowerCase() ? parsed.linuxPath : null
}
export function isWslUncPath(path: string): boolean {
return parseWslUncPath(path) !== null
}
/** Translate UNC and drive paths before passing hook/setup paths to WSL bash; leave POSIX paths unchanged. */
export function toLinuxPath(windowsPath: string): string {
// A literal //wsl$/x on POSIX is an ordinary directory and must survive unchanged.
const info = process.platform === 'win32' ? parseWslUncPath(windowsPath) : null
if (info) {
return info.linuxPath
}
const driveMatch = windowsPath.match(/^([A-Za-z]):[/\\](.*)$/)
if (!driveMatch) {
return windowsPath
}
const driveLetter = driveMatch[1].toLowerCase()
const rest = driveMatch[2].replace(/\\/g, '/')
return `/mnt/${driveLetter}/${rest}`
}
/** Convert an absolute Linux path in a known WSL distro to its Windows form. */
export function toWindowsWslPath(linuxPath: string, distro: string): string {
return toWindowsWslDrivePath(linuxPath) ?? toWindowsWslUncPath(linuxPath, distro)
}
/** Keep a Linux path addressable through its distro, including drvfs mounts. */
export function toWindowsWslUncPath(linuxPath: string, distro: string): string {
return `\\\\wsl.localhost\\${distro}${linuxPath === '/' ? '\\' : linuxPath.replace(/\//g, '\\')}`
}
/** Resolve an absolute POSIX base within the repo’s WSL distro; Windows otherwise treats it as drive-relative and discards it (STA-4772). Other forms pass through.
* Keep drvfs on the distro UNC view so mirroring retains WSL terminal routing. Preserve ambiguous multi-slash/backslash roots unchanged; collapse dot segments so creation and ownership classification agree. */
export function resolveWslRepoWorktreeBasePath(repoPath: string, basePath: string): string {
const repoWsl = parseWslUncPath(repoPath)
if (!repoWsl || !/^\/(?!\/)/.test(basePath)) {
return basePath
}
const collapsed = collapsePosixDotSegments(basePath)
return toWindowsWslUncPath(collapsed, repoWsl.distro)
}
function collapsePosixDotSegments(absolutePosixPath: string): string {
const segments: string[] = []
for (const segment of absolutePosixPath.split('/')) {
if (!segment || segment === '.') {
continue
}
if (segment === '..') {
segments.pop()
continue
}
segments.push(segment)
}
return `/${segments.join('/')}`
}
// Fold share aliases, distro and drvfs drive tails; ordinary Linux paths remain case-sensitive.
export function foldWslUncPathCaseInsensitiveParts(path: string): string | null {
const parsed = parseWslUncPath(path)
if (!parsed) {
return null
}
// Only lowercase /mnt is drvfs; /MNT stays case-sensitive.
const linuxPath = /^\/mnt\/[a-zA-Z](?:\/|$)/.test(parsed.linuxPath)
? parsed.linuxPath.toLowerCase()
: parsed.linuxPath
return `//wsl.localhost/${parsed.distro.toLowerCase()}${linuxPath === '/' ? '' : linuxPath}`
}
/**
* The spelling a WSL-hosted tool answers in for `path`. Git-in-the-distro resolves relative output
* against the Linux path, not the caller's UNC spelling, and realpath cannot bridge the two spaces.
*/
export function toWslExecutionSpace(path: string): string {
return parseWslUncPath(path)?.linuxPath ?? path
}
/** Classify the lowercase drvfs prefix only. Keep separate from toWindowsWslDrivePath’s whole-path validation, which rejects stray line terminators. */
const DRVFS_LINUX_PATH = /^\/mnt\/[a-z](?:\/|$)/
/** True for a Linux path that is really a Windows drive reached through drvfs. */
export function isDrvfsLinuxPath(linuxPath: string): boolean {
return DRVFS_LINUX_PATH.test(linuxPath)
}
/** Drvfs bytes belong to a Windows drive regardless of which distro mounted it. */
export function toWindowsWslDrivePath(linuxPath: string): string | null {
// Dot excludes line terminators, preventing stray CR/output lines from becoming drive paths.
const match = linuxPath.match(/^\/mnt\/([a-z])(\/.*)?$/)
if (!match) {
return null
}
const tail = (match[2] ?? '').replace(/\//g, '\\')
return `${match[1].toUpperCase()}:${tail || '\\'}`
}
/** Return the distro crossing the 9p/drvfs boundary: drive paths under WSL runtime, or UNC drvfs mounts regardless of runtime. Linux-native, ordinary UNC, POSIX and SSH paths do not cross. */
export function getWslFilesystemBoundaryDistro(args: {
projectPath: string
wslRuntimeDistro?: string | null
}): string | null {
const wsl = parseWslUncPath(args.projectPath)
if (wsl) {
return isDrvfsLinuxPath(wsl.linuxPath) ? wsl.distro : null
}
if (!/^[A-Za-z]:[\\/]/.test(args.projectPath)) {
return null
}
return args.wslRuntimeDistro || null
}
/** Queue dead-share subdirectories by host. Local disks need no lane because they do not block comparably. */
export function uncRouteKey(cwd: string): string | null {
if (!cwd.startsWith('\\\\')) {
return null
}
const wslInfo = parseWslUncPath(cwd)
if (wslInfo) {
return `wsl:${wslInfo.distro.trim().toLowerCase()}`
}
const server = cwd.slice(2).split(/[\\/]/, 1)[0]
return `unc:${server.toLowerCase()}`
}
/** Match command routing: a native UNC cwd takes precedence over a distro hint. */
export function wslDistroForCommand(cwd: string | undefined, override?: string): string | null {
if (process.platform !== 'win32') {
return null
}
return (cwd ? parseWslUncPath(cwd)?.distro : undefined) ?? override ?? null
}
/** `\\wsl.localhost\<distro>\mnt\c\x` → `C:\x`; null for any other path. */
export function wslUncDrivePathToWindows(path: string, distro: string): string | null {
const linux = wslUncPathToLinux(path, distro)?.replace(/\\/g, '/')
// Same rule as foldWslUncPathCaseInsensitiveParts: only lowercase `/mnt` is drvfs, any letter case.
const match = linux?.match(/^\/mnt\/([a-zA-Z])(\/.*)?$/)
if (!match) {
return null
}
const tail = (match[2] ?? '').replace(/\/+$/, '').replace(/\//g, '\\')
return `${match[1].toUpperCase()}:${tail || '\\'}`
}