Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
201 changes: 199 additions & 2 deletions src/main/ipc/worktree-logic-wsl.test.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
import { win32 } from 'node:path'
import { beforeEach, describe, expect, it, vi } from 'vitest'

const { getWslHomeMock, parseWslPathMock } = vi.hoisted(() => ({
const { getWslHomeMock, getWslHomeAsyncMock, parseWslPathMock } = vi.hoisted(() => ({
getWslHomeMock: vi.fn(),
getWslHomeAsyncMock: vi.fn(),
parseWslPathMock: vi.fn()
}))

vi.mock('../wsl', () => ({
getWslHome: getWslHomeMock,
getWslHomeAsync: getWslHomeAsyncMock,
parseWslPath: parseWslPathMock
}))

import { computeWorktreePath } from './worktree-logic'
import {
computeWorktreePath,
computeWorktreePathAsync,
getWorktreePathSettings
} from './worktree-logic'
import {
buildKnownOrcaWorkspaceLayouts,
classifyWorktreeOwnership
} from '../../shared/worktree/ownership'
import { relativePathInsideRoot } from '../../shared/cross-platform-path'
import type { Repo } from '../../shared/repo-types'

describe('computeWorktreePath WSL layout', () => {
beforeEach(() => {
getWslHomeMock.mockReset()
getWslHomeAsyncMock.mockReset()
parseWslPathMock.mockReset()
})

Expand Down Expand Up @@ -49,6 +62,190 @@ describe('computeWorktreePath WSL layout', () => {
).toBe(win32.join('C:\\workspaces', 'feature'))
})

it('honors an absolute Linux repo worktree base path inside the repo distro (STA-4772)', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/src/.orca-worktrees'
}
const settings = { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' }

expect(computeWorktreePath('feature', repo.path, getWorktreePathSettings(repo, settings))).toBe(
'\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\.orca-worktrees\\feature'
)
// Why repeat: cached follow-up calls must resolve identically to the first.
expect(computeWorktreePath('feature', repo.path, getWorktreePathSettings(repo, settings))).toBe(
'\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\.orca-worktrees\\feature'
)
expect(getWslHomeMock).not.toHaveBeenCalled()
})

it('honors a drvfs /mnt repo base path instead of mirroring it away', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '/mnt/d/trees'
}

expect(
computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' })
)
).toBe('\\\\wsl.localhost\\Ubuntu\\mnt\\d\\trees\\feature')
})

it('collapses dotted Linux repo base paths end to end', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/src/../trees'
}

expect(
computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' })
)
).toBe('\\\\wsl.localhost\\Ubuntu\\home\\jin\\trees\\feature')
})

it('still mirrors drive-letter repo base paths into the distro home', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: 'D:\\trees'
}

// Why: desktop drive roots keep WSL worktrees on the WSL filesystem by design.
expect(
computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' })
)
).toBe('\\\\wsl.localhost\\Ubuntu\\home\\jin\\orca\\workspaces\\feature')
})

it('classifies whatever creation produces for Linux bases, dotted or not', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const settings = { nestWorkspaces: true, workspaceDir: 'C:\\workspaces' }
for (const worktreeBasePath of ['/home/jin/trees', '/home/jin/src/../trees', '/mnt/d/trees']) {
const repo = {
id: 'r1',
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
displayName: 'repo',
badgeColor: '#000',
addedAt: 0,
kind: 'git',
worktreeBasePath
} as Repo
// Why: the resolver exists so these two layers agree; assert the
// invariant directly instead of two hardcoded strings that happen to match.
const createdPath = computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, settings)
)
const layouts = buildKnownOrcaWorkspaceLayouts({ ...settings, workspaceDirHistory: [] }, repo)
// Why containment, not just ownership: a regressed resolver lands in the
// ~/orca/workspaces mirror layout, which also classifies 'external'.
// layouts[0] is the repo-base layout — it is always pushed first.
expect(relativePathInsideRoot(layouts[0].path, createdPath)).not.toBeNull()
expect(
classifyWorktreeOwnership({
repo,
settings: { ...settings, workspaceDirHistory: [] },
worktree: { path: createdPath, isMainWorktree: false },
knownOrcaLayouts: layouts
})
).toBe('external')
}
})

it('resolves the Linux repo base path identically through the async twin', async () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
getWslHomeAsyncMock.mockResolvedValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/src/.orca-worktrees'
}
const pathSettings = getWorktreePathSettings(repo, {
nestWorkspaces: false,
workspaceDir: 'C:\\workspaces'
})

await expect(computeWorktreePathAsync('feature', repo.path, pathSettings)).resolves.toBe(
computeWorktreePath('feature', repo.path, pathSettings)
)
})

it('honors a Linux repo base path even when the distro home lookup fails', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue(null)
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/trees'
}

expect(
computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' })
)
).toBe('\\\\wsl.localhost\\Ubuntu\\home\\jin\\trees\\feature')
})

it('keeps relative repo base paths anchored to the WSL repo', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
linuxPath: '/home/jin/src/repo'
})
getWslHomeMock.mockReturnValue('\\\\wsl.localhost\\Ubuntu\\home\\jin')
const repo = {
path: '\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\repo',
worktreeBasePath: '../worktrees'
}

expect(
computeWorktreePath(
'feature',
repo.path,
getWorktreePathSettings(repo, { nestWorkspaces: false, workspaceDir: 'C:\\workspaces' })
)
).toBe('\\\\wsl.localhost\\Ubuntu\\home\\jin\\src\\worktrees\\feature')
})

it('uses an explicit WSL UNC workspace root without remapping it', () => {
parseWslPathMock.mockReturnValue({
distro: 'Ubuntu',
Expand Down
8 changes: 6 additions & 2 deletions src/main/ipc/worktree-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { resolve, relative, isAbsolute, posix, sep, win32 } from 'node:path'
import type { GlobalSettings, OrcaWorkspaceLayout } from '../../shared/global-settings-types'
import type { Repo } from '../../shared/repo-types'
import { isWindowsAbsolutePathLike, resolveRuntimePath } from '../../shared/cross-platform-path'
import { isWslUncPath } from '../../shared/wsl-paths'
import { isWslUncPath, resolveWslRepoWorktreeBasePath } from '../../shared/wsl-paths'
import { splitWorktreeId } from '../../shared/worktree/id'
import { replaceKnownEmojiWithShortcodes } from '../../shared/emoji-shortcode-catalog'
import { getWslHome, getWslHomeAsync, parseWslPath } from '../wsl'
Expand Down Expand Up @@ -226,7 +226,11 @@ function getEffectiveWorktreeBasePath(
repo: WorktreeBasePathRepo,
settings: WorktreePathSettings
): string {
return getRepoWorktreeBasePath(repo) ?? settings.workspaceDir
const basePath = getRepoWorktreeBasePath(repo)
if (basePath === undefined) {
return settings.workspaceDir
}
return resolveWslRepoWorktreeBasePath(repo.path, basePath)
}

function getRepoWorktreeBasePath(repo: Pick<Repo, 'worktreeBasePath'>): string | undefined {
Expand Down
48 changes: 48 additions & 0 deletions src/shared/worktree/ownership-worktree-base-path.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,54 @@ describe('repo-specific worktree ownership layouts', () => {
).toBe('external')
})

it('resolves an absolute Linux base path of a WSL repo into its distro layout (STA-4772)', () => {
const repo = makeRepo({
path: '\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/src/.orca-worktrees'
})
const settings = makeSettings({ workspaceDir: 'C:\\global' })
const layouts = buildKnownOrcaWorkspaceLayouts(settings, repo)

expect(layouts[0]).toEqual({
path: '//wsl.localhost/Ubuntu-24.04/home/jin/src/.orca-worktrees',
nestWorkspaces: true
})
expect(
classifyWorktreeOwnership({
repo,
settings,
worktree: makeWorktree(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\src\\.orca-worktrees\\repo\\feature'
),
knownOrcaLayouts: layouts
})
).toBe('external')
})

it('classifies worktrees under a dotted Linux base exactly where creation collapses it', () => {
const repo = makeRepo({
path: '\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\src\\repo',
worktreeBasePath: '/home/jin/src/../.orca-worktrees'
})
const settings = makeSettings({ workspaceDir: 'C:\\global' })
const layouts = buildKnownOrcaWorkspaceLayouts(settings, repo)

expect(layouts[0]).toEqual({
path: '//wsl.localhost/Ubuntu-24.04/home/jin/.orca-worktrees',
nestWorkspaces: true
})
expect(
classifyWorktreeOwnership({
repo,
settings,
worktree: makeWorktree(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\.orca-worktrees\\repo\\feature'
),
knownOrcaLayouts: layouts
})
).toBe('external')
})

it('includes relative global layouts for SSH repos without applying absolute desktop paths', () => {
const repo = makeRepo({ path: '/remote/repo', connectionId: 'ssh-1' })
const relativeSettings = makeSettings({ workspaceDir: '../worktrees' })
Expand Down
11 changes: 8 additions & 3 deletions src/shared/worktree/ownership.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
relativePathInsideRoot,
resolveRuntimePath
} from '../cross-platform-path'
import { parseWslUncPath } from '../wsl-paths'
import { parseWslUncPath, resolveWslRepoWorktreeBasePath } from '../wsl-paths'
import {
isAgentScratchWorktreePath,
type AgentScratchWorktreePathMatcher
Expand Down Expand Up @@ -87,10 +87,15 @@ function appendWorkspaceLayouts(
}

function getRepoWorktreeBasePath(
repo: Pick<Repo, 'worktreeBasePath'> | undefined
repo: Pick<Repo, 'path' | 'worktreeBasePath'> | undefined
): string | undefined {
const trimmed = repo?.worktreeBasePath?.trim()
return trimmed || undefined
if (!repo || !trimmed) {
return undefined
}
// Why: creation resolves Linux base paths of WSL repos to their UNC form,
// so ownership layouts must match or those worktrees would never classify.
return resolveWslRepoWorktreeBasePath(repo.path, trimmed)
}

function resolveWorkspaceLayoutPath(repoPath: string, layoutPath: string): string {
Expand Down
51 changes: 51 additions & 0 deletions src/shared/wsl-paths.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
foldWslUncPathCaseInsensitiveParts,
isWslUncPath,
parseWslUncPath,
resolveWslRepoWorktreeBasePath,
toWindowsWslPath
} from './wsl-paths'

Expand Down Expand Up @@ -34,6 +35,56 @@ describe('wsl path helpers', () => {
})
})

describe('resolveWslRepoWorktreeBasePath', () => {
const WSL_REPO = '\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\src\\repo'

it('resolves an absolute Linux base path into the repo distro (STA-4772)', () => {
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '/home/jin/project/.orca-worktrees')).toBe(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\project\\.orca-worktrees'
)
expect(resolveWslRepoWorktreeBasePath('\\\\wsl$\\Debian\\srv\\repo', '/srv/trees')).toBe(
'\\\\wsl.localhost\\Debian\\srv\\trees'
)
})

it('keeps drvfs base paths on the distro UNC view so mirroring cannot discard them', () => {
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '/mnt/d/trees')).toBe(
'\\\\wsl.localhost\\Ubuntu-24.04\\mnt\\d\\trees'
)
})

it('collapses dot segments and trailing slashes so ownership layouts match creation', () => {
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '/home/jin/src/../trees')).toBe(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\trees'
)
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '/home/jin/./trees/')).toBe(
'\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\trees'
)
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '/../..')).toBe(
'\\\\wsl.localhost\\Ubuntu-24.04\\'
)
})

it('keeps UNC, drive, and relative base paths untouched for WSL repos', () => {
expect(
resolveWslRepoWorktreeBasePath(WSL_REPO, '\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\trees')
).toBe('\\\\wsl.localhost\\Ubuntu-24.04\\home\\jin\\trees')
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '//wsl.localhost/Ubuntu-24.04/trees')).toBe(
'//wsl.localhost/Ubuntu-24.04/trees'
)
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, 'D:\\trees')).toBe('D:\\trees')
expect(resolveWslRepoWorktreeBasePath(WSL_REPO, '../worktrees')).toBe('../worktrees')
})

it('never rewrites base paths of non-WSL repos', () => {
expect(resolveWslRepoWorktreeBasePath('C:\\src\\repo', '/home/jin/trees')).toBe(
'/home/jin/trees'
)
expect(resolveWslRepoWorktreeBasePath('/srv/repo', '/srv/trees')).toBe('/srv/trees')
expect(resolveWslRepoWorktreeBasePath('//server/share/repo', '/srv/trees')).toBe('/srv/trees')
})
})

describe('foldWslUncPathCaseInsensitiveParts', () => {
it('folds share spelling, distro casing, and separators but not the Linux tail', () => {
expect(foldWslUncPathCaseInsensitiveParts('\\\\WSL$\\Ubuntu\\home\\jin\\Repo')).toBe(
Expand Down
Loading