diff --git a/src/renderer/src/components/settings/ProjectWindowsRuntimeSetting.test.tsx b/src/renderer/src/components/settings/ProjectWindowsRuntimeSetting.test.tsx index 6caa188c9d1e..80dab9968c28 100644 --- a/src/renderer/src/components/settings/ProjectWindowsRuntimeSetting.test.tsx +++ b/src/renderer/src/components/settings/ProjectWindowsRuntimeSetting.test.tsx @@ -196,6 +196,113 @@ describe('ProjectWindowsRuntimeSetting', () => { } }) + it('drops a pending runtime change when the storage lock engages', () => { + const updateProject = vi.fn() + const container = document.createElement('div') + document.body.appendChild(container) + const root = createRoot(container) + + try { + act(() => { + root.render( + + ) + }) + + clickButton(container, 'WSL') + expect(container.textContent).toContain('Runtime change pending') + + // Why: moving the project onto \\wsl.localhost\Debian must lock the runtime + // and retire the half-made pending choice — not leave both on screen. + act(() => { + root.render( + + ) + }) + + expect(container.textContent).not.toContain('Runtime change pending') + expect(container.textContent).toContain('runtime is locked to that distro') + } finally { + act(() => { + root.unmount() + }) + container.remove() + } + }) + + it('refuses to commit a pending change while the storage lock is active', () => { + const updateProject = vi.fn().mockResolvedValue(true) + const container = document.createElement('div') + document.body.appendChild(container) + const root = createRoot(container) + // Why windows-host: a stored preference that contradicts the lock makes the + // mount effect normalize once, so a second updateProject can only come from + // an Apply the guard must block. + const lockedProject: Project = { + ...project, + localWindowsRuntimePreference: { kind: 'windows-host' } + } + + try { + act(() => { + root.render( + + ) + }) + + // The mount effect normalizes the contradicting stored preference once. + expect(updateProject).toHaveBeenCalledTimes(1) + + clickButton(container, 'WSL') + // Why assert first: without a rendered Apply button the test would pass + // vacuously, never exercising the commit guard it exists to pin. + const applyButton = Array.from(container.querySelectorAll('button')).find((button) => + button.textContent?.includes('Apply runtime change') + ) + expect(applyButton).toBeTruthy() + act(() => { + applyButton?.dispatchEvent(new MouseEvent('click', { bubbles: true })) + }) + + // Why still 1: the lock guard no-ops the commit, so Apply adds no update. + expect(updateProject).toHaveBeenCalledTimes(1) + } finally { + act(() => { + root.unmount() + }) + container.remove() + } + }) + it('shows repair copy instead of silently falling back when a selected WSL distro is missing', () => { const markup = renderToStaticMarkup( { expect(markup).toContain('Choose an installed distro or switch this project to Windows.') }) + it('locks the runtime to the storage distro for projects on a WSL UNC path', () => { + vi.useFakeTimers() + const updateProject = vi.fn() + const { container, root } = renderClient({ + project, + settings: getDefaultSettings('/tmp'), + isLocalWindowsProject: true, + repoPath: '\\\\wsl.localhost\\Debian\\home\\u\\sample-project', + wslAvailable: true, + wslDistros: ['Debian'], + wslCapabilitiesLoading: false, + updateProject + }) + + try { + act(() => { + vi.runAllTimers() + }) + // Why: storage and execution must agree — the selector locks to the + // storage distro and normalizes a contradicting stored preference. + expect(updateProject).toHaveBeenCalledWith('project-1', { + localWindowsRuntimePreference: { kind: 'wsl', distro: 'Debian' } + }) + expect(container.textContent).toContain('runtime is locked to that distro') + // Why aria-disabled: a native disabled button leaves the tab order (see + // SettingsFormControls), so the segments disable via aria-disabled. + const disabledButtons = Array.from( + container.querySelectorAll('button[aria-disabled="true"]') + ).map((button) => button.textContent?.trim()) + expect(disabledButtons).toContain('Windows') + expect(disabledButtons).toContain('Default (Windows)') + } finally { + cleanupClient(container, root) + vi.useRealTimers() + } + }) + + it('keeps the runtime switchable when the storage distro is no longer installed', () => { + const markup = renderToStaticMarkup( + + ) + + expect(markup).not.toContain('runtime is locked to that distro') + expect(markup).not.toContain('aria-disabled="true"') + }) + it('does not render for remote or non-Windows-owned projects', () => { const markup = renderToStaticMarkup( isLocalWindowsProject: boolean + /** Where this project's files live; a WSL UNC pins the runtime to that distro. */ + repoPath?: string | null wslAvailable: boolean wslDistros: string[] wslCapabilitiesLoading: boolean @@ -33,6 +44,7 @@ export function ProjectWindowsRuntimeSetting({ project, settings, isLocalWindowsProject, + repoPath, wslAvailable, wslDistros, wslCapabilitiesLoading, @@ -42,18 +54,59 @@ export function ProjectWindowsRuntimeSetting({ const [pendingPreference, setPendingPreference] = useState( null ) + const normalizedLockRef = useRef(null) + + // Why locked: storage and execution must agree — a project on + // \\wsl.localhost\ that ran on the Windows host would push every git + // and terminal call across 9P. Unlocks when the distro disappears (repair) + // or WSL goes unavailable (the repair copy's "switch to Windows" must work). + const storageWslDistro = repoPath ? (parseWslUncPath(repoPath)?.distro ?? null) : null + const lockedWslDistro = + storageWslDistro && + (wslCapabilitiesLoading || (wslAvailable && wslDistros.includes(storageWslDistro))) + ? storageWslDistro + : null + const lockedPreference = lockedWslDistro + ? ({ kind: 'wsl', distro: lockedWslDistro } as LocalWindowsRuntimePreference) + : null + + // Why: legacy projects could carry a non-WSL preference over a WSL UNC path; + // align the stored preference with the lock instead of displaying a lie. + useEffect(() => { + if (!project || !lockedWslDistro) { + // Why reset: the lock is off (distro gone / WSL unavailable). Clearing the + // ref lets a later re-engage re-normalize instead of skipping on a stale key. + normalizedLockRef.current = null + return + } + // Why: the lock overrides any half-made choice — drop a pending change so the + // Apply/Cancel banner cannot linger over a locked, non-editable control. + setPendingPreference(null) + const stored = normalizeProjectRuntimePreference(project.localWindowsRuntimePreference) + if (stored.kind === 'wsl' && stored.distro === lockedWslDistro) { + return + } + const lockKey = `${project.id}:${lockedWslDistro}` + if (normalizedLockRef.current === lockKey) { + return + } + normalizedLockRef.current = lockKey + void updateProject(project.id, { + localWindowsRuntimePreference: { kind: 'wsl', distro: lockedWslDistro } + }) + }, [lockedWslDistro, project, updateProject]) if (!project || !isLocalWindowsProject) { return null } const preference = normalizeProjectRuntimePreference(project.localWindowsRuntimePreference) - const selectedPreference = pendingPreference ?? preference + const selectedPreference = lockedPreference ?? pendingPreference ?? preference const nextWslDistro = getNextProjectWslDistro(selectedPreference, settings, wslDistros) const resolution = resolveProjectExecutionRuntime({ appPlatform: 'win32', projectId: project.id, - projectRuntimePreference: preference, + projectRuntimePreference: lockedPreference ?? preference, globalWindowsRuntimeDefault: settings.localWindowsRuntimeDefault, wslAvailable: wslCapabilitiesLoading ? undefined : wslAvailable, availableWslDistros: wslCapabilitiesLoading ? null : wslDistros @@ -66,6 +119,12 @@ export function ProjectWindowsRuntimeSetting({ const defaultRuntimeLabel = getDefaultRuntimeLabel(settings) const commitRuntimePreference = (nextPreference: LocalWindowsRuntimePreference): void => { setPendingPreference(null) + // Why: storage pins the runtime while locked, so no commit is accepted — + // normalization already persisted the locked distro, and this no-op stops a + // stale pending Apply from writing anything over it. + if (lockedWslDistro !== null) { + return + } if (nextPreference.kind === 'inherit-global') { void updateProject(project.id, { localWindowsRuntimePreference: undefined }) return @@ -105,6 +164,11 @@ export function ProjectWindowsRuntimeSetting({ } } const handleDistroChange = (distro: string): void => { + // Why: while locked, storage pins the runtime — a different distro would + // recreate the storage/execution mismatch the lock exists to prevent. + if (lockedWslDistro !== null && distro !== lockedWslDistro) { + return + } requestRuntimePreference({ kind: 'wsl', distro }) } @@ -129,14 +193,16 @@ export function ProjectWindowsRuntimeSetting({ options={[ { value: 'inherit-global', - label: {defaultRuntimeLabel} + label: {defaultRuntimeLabel}, + disabled: lockedWslDistro !== null }, { value: 'windows-host', label: translate( 'auto.components.settings.ProjectWindowsRuntimeSetting.windows', 'Windows' - ) + ), + disabled: lockedWslDistro !== null }, { value: 'wsl', @@ -148,11 +214,15 @@ export function ProjectWindowsRuntimeSetting({ } ]} /> - {isWslSelected ? ( + {isWslSelected || lockedWslDistro !== null ? (