From 0f89de5276b28e78be709b2f5febe1cea94bb2e9 Mon Sep 17 00:00:00 2001 From: Brennan Benson <79079362+brennanb2025@users.noreply.github.com> Date: Mon, 9 Nov 2026 12:03:33 -0800 Subject: [PATCH] Save a pasted workspace directory as the path it names (#40347) * Save a pasted workspace directory as the path it names A workspace directory with a leading space (" /Users/me/dev") was saved as typed. Readers treat it as relative, so Orca resolved it inside every registered repository and created an empty " /Users/me/dev" tree in each. Normalize the saved value once where settings are written and loaded, as the leading ~ fix did: leading whitespace before an absolute or home path is dropped, while relative paths and trailing characters stay exact. A value repaired on load keeps its old spelling in history so checkouts under the old root stay visible. The settings field shows the saved value. Co-authored-by: Eng. Mahmoud Bek ElNemer * Expand ~\ only where it is a home path; drop type casts * Classify the padded path with the host's own path rules On macOS and Linux a leading backslash is part of a relative name, so only Windows treats \work as absolute. --------- Co-authored-by: Eng. Mahmoud Bek ElNemer --- .../persistence-settings-update.test.ts | 24 ++++++++++++++ .../applying-settings/settings-update.ts | 4 +-- .../terminal-settings-migrations.ts | 31 ++++++++++++++++-- .../normalize-loaded-global-settings.test.ts | 17 ++++++++++ .../normalize-loaded-global-settings.ts | 7 ++-- .../WorkspaceDirectorySetting.test.tsx | 32 +++++++++++++++++++ .../general/WorkspaceDirectorySetting.tsx | 6 +++- src/shared/worktree/workspace-dir-input.ts | 16 ++++++++++ 8 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 src/renderer/src/components/settings/general/WorkspaceDirectorySetting.test.tsx create mode 100644 src/shared/worktree/workspace-dir-input.ts diff --git a/src/main/persistence/applying-settings/persistence-settings-update.test.ts b/src/main/persistence/applying-settings/persistence-settings-update.test.ts index 0f35da8f31b..3117cabecaa 100644 --- a/src/main/persistence/applying-settings/persistence-settings-update.test.ts +++ b/src/main/persistence/applying-settings/persistence-settings-update.test.ts @@ -82,6 +82,30 @@ describe('Store', () => { expect(isLocalWorktreeScanGenerationCurrent('r1', generation)).toBe(false) }) + it('saves a pasted workspace directory with leading spaces as the absolute path it names', async () => { + const store = await createStore() + store.updateSettings({ workspaceDir: '/Users/me/dev' }) + const historyBefore = store.getSettings().workspaceDirHistory + + // Padding before an absolute path would otherwise resolve inside every repo. + expect(store.updateSettings({ workspaceDir: ' /Users/me/dev' }).workspaceDir).toBe( + '/Users/me/dev' + ) + expect(store.getSettings().workspaceDirHistory).toEqual(historyBefore) + // Trailing characters and relative paths can be real folder names. + expect(store.updateSettings({ workspaceDir: '/tmp/workspaces ' }).workspaceDir).toBe( + '/tmp/workspaces ' + ) + expect(store.updateSettings({ workspaceDir: ' ~notes' }).workspaceDir).toBe(' ~notes') + if (process.platform === 'win32') { + expect(store.updateSettings({ workspaceDir: ' C:\\work' }).workspaceDir).toBe('C:\\work') + } else { + // ~\ and a leading \ are relative off Windows, so the value stays exact. + expect(store.updateSettings({ workspaceDir: ' ~\\work' }).workspaceDir).toBe(' ~\\work') + expect(store.updateSettings({ workspaceDir: ' \\work' }).workspaceDir).toBe(' \\work') + } + }) + it('updateSettings deep-merges and clamps notification custom sound volume', async () => { const store = await createStore() store.updateSettings({ diff --git a/src/main/persistence/applying-settings/settings-update.ts b/src/main/persistence/applying-settings/settings-update.ts index 6a3f0e2997f..32220723e6a 100644 --- a/src/main/persistence/applying-settings/settings-update.ts +++ b/src/main/persistence/applying-settings/settings-update.ts @@ -42,7 +42,7 @@ import { normalizeNotificationSettings } from './onboarding-normalization' import { retireLegacyInstructionsForClearedTextActionRecipes } from './source-control-settings' import { buildWorkspaceDirHistoryForUpdate, - expandWorkspaceDirHome, + normalizeWorkspaceDirSetting, stripRetiredGlobalSettings } from './terminal-settings-migrations' @@ -59,7 +59,7 @@ export function updateSettings( updates: Partial, options: { notifyListeners?: boolean; originWebContentsId?: number } = {} ): GlobalSettings { - const sanitizedUpdates = expandWorkspaceDirHome(stripRetiredGlobalSettings(updates)) + const sanitizedUpdates = normalizeWorkspaceDirSetting(stripRetiredGlobalSettings(updates)) const linkBehavior = updates.terminalLinkClickBehavior if (linkBehavior === 'actions' || linkBehavior === 'open' || linkBehavior === 'none') { // Loaded defaults are ambiguous; synchronize the legacy switch only on an explicit write. diff --git a/src/main/persistence/applying-settings/terminal-settings-migrations.ts b/src/main/persistence/applying-settings/terminal-settings-migrations.ts index 8aa8b021824..ae54a94349e 100644 --- a/src/main/persistence/applying-settings/terminal-settings-migrations.ts +++ b/src/main/persistence/applying-settings/terminal-settings-migrations.ts @@ -12,14 +12,39 @@ import { normalizeTuiAgentEnvRecord } from '../../../shared/agent-launch/choices/tui-agent-launch-defaults' import { expandLeadingHomePath } from '../../filesystem/home-path-expansion' +import { normalizeWorkspaceDirInput } from '../../../shared/worktree/workspace-dir-input' -/** Unexpanded, a leading ~ resolves inside each repo and worktrees land in /~ (#34944). */ -export function expandWorkspaceDirHome(settings: Partial): Partial { +/** A raw ~ or padded absolute path resolves inside each repo (#34944, STA-14308); normalize at the store boundary. */ +export function normalizeWorkspaceDirSetting( + settings: Partial +): Partial { return typeof settings.workspaceDir === 'string' - ? { ...settings, workspaceDir: expandLeadingHomePath(settings.workspaceDir, homedir()) } + ? { + ...settings, + workspaceDir: expandLeadingHomePath( + normalizeWorkspaceDirInput(settings.workspaceDir, process.platform === 'win32'), + homedir() + ) + } : settings } +/** Keep a repaired saved spelling in history so checkouts under its old root keep their visibility. */ +export function normalizeLoadedWorkspaceDirSetting( + settings: Partial, + defaults: GlobalSettings +): Partial { + const normalized = normalizeWorkspaceDirSetting(settings) + if (normalized.workspaceDir === settings.workspaceDir) { + return normalized + } + const history = buildWorkspaceDirHistoryForUpdate( + { ...defaults, ...settings }, + { workspaceDir: normalized.workspaceDir } + ) + return history ? { ...normalized, workspaceDirHistory: history } : normalized +} + export function buildWorkspaceDirHistoryForUpdate( current: GlobalSettings, updates: Partial diff --git a/src/main/persistence/loading-store/normalize-loaded-global-settings.test.ts b/src/main/persistence/loading-store/normalize-loaded-global-settings.test.ts index 1b6fa84cecf..ce56101aebd 100644 --- a/src/main/persistence/loading-store/normalize-loaded-global-settings.test.ts +++ b/src/main/persistence/loading-store/normalize-loaded-global-settings.test.ts @@ -3,6 +3,7 @@ import { expect, it } from 'vitest' import { resolveAntigravityChatAuthConfig } from '../../../shared/antigravity-chat-auth-config' import { getDefaultPersistedState, getDefaultVoiceSettings } from '../../../shared/constants' import type { PersistedState } from '../../../shared/persisted-state-types' +import { buildKnownOrcaWorkspaceLayouts } from '../../../shared/worktree/ownership' import { normalizeLoadedGlobalSettings } from './normalize-loaded-global-settings' import { prepareLoadedTerminalSettings } from './prepare-loaded-terminal-settings' import { prepareLoadedProfileSettings } from './prepare-loaded-profile-settings' @@ -57,3 +58,19 @@ it('stores Auto as Toggle plus hold-to-record so older builds keep a working sho false ]) }) + +it('loads a padded workspace directory as the absolute path while keeping its old root known', () => { + const settings = normalizeLegacyProfile({ + workspaceDir: ' /Users/me/dev', + nestWorkspaces: false, + workspaceDirHistory: [] + }) + + expect(settings.workspaceDir).toBe('/Users/me/dev') + // Checkouts an older build placed under / /Users/me/dev keep their known layout. + expect( + buildKnownOrcaWorkspaceLayouts(settings, { path: '/repo', worktreeBasePath: undefined }).map( + (layout) => layout.path + ) + ).toContain('/repo/ /Users/me/dev') +}) diff --git a/src/main/persistence/loading-store/normalize-loaded-global-settings.ts b/src/main/persistence/loading-store/normalize-loaded-global-settings.ts index 34e15219fa8..c75ab18a38c 100644 --- a/src/main/persistence/loading-store/normalize-loaded-global-settings.ts +++ b/src/main/persistence/loading-store/normalize-loaded-global-settings.ts @@ -17,7 +17,7 @@ import { projectSourceControlAiToLegacyCommitMessageAi } from '../../../shared/s import { normalizeNativeChatShellEnvironmentVariables } from '../../../shared/native-chat/native-chat-shell-environment' import { publishedLegacyChatPermissionMode } from '../../../shared/agent-launch/permissions/agent-chat-permission-mode' import { - expandWorkspaceDirHome, + normalizeLoadedWorkspaceDirSetting, stripRetiredGlobalSettings } from '../applying-settings/terminal-settings-migrations' import { readLegacySidekickFlag } from '../applying-settings/onboarding-normalization' @@ -88,7 +88,10 @@ export function normalizeLoadedGlobalSettings( return { ...defaults.settings, // Legacy stored booleans cannot distinguish opt-in from the old default; retain explicit true. - ...expandWorkspaceDirHome(stripRetiredGlobalSettings(parsed.settings)), + ...normalizeLoadedWorkspaceDirSetting( + stripRetiredGlobalSettings(parsed.settings), + defaults.settings + ), agentDefaultSurface: normalizeAgentDefaultSurface(parsed.settings?.agentDefaultSurface), nativeChatAppearance: normalizeNativeChatAppearanceSettings( parsed.settings?.nativeChatAppearance diff --git a/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.test.tsx b/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.test.tsx new file mode 100644 index 00000000000..152da49e318 --- /dev/null +++ b/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.test.tsx @@ -0,0 +1,32 @@ +// @vitest-environment happy-dom +import { cleanup, fireEvent, render, screen } from '@testing-library/react' +import { afterEach, describe, expect, it, vi } from 'vitest' +import { createGlobalSettingsFixture } from '../../../../../shared/global-settings-test-fixture' +import { WorkspaceDirectorySetting } from './WorkspaceDirectorySetting' + +vi.mock('../section-layout/SearchableSetting', () => ({ + SearchableSetting: ({ children }) => children +})) +vi.mock('../../sidebar/worktree-list/controls/use-sidebar-host-scope-options', () => ({ + useSidebarHostScopeOptions: () => ({ hostOptions: [] }) +})) +afterEach(cleanup) + +describe('WorkspaceDirectorySetting', () => { + it('shows the saved path and writes nothing when a padded copy of it is committed', () => { + const updateSettings = vi.fn() + render( + + ) + const input = screen.getByRole('textbox') + + fireEvent.change(input, { target: { value: ' /Users/me/dev' } }) + fireEvent.blur(input) + + expect(input.value).toBe('/Users/me/dev') + expect(updateSettings).not.toHaveBeenCalled() + }) +}) diff --git a/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.tsx b/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.tsx index 094a7e1107f..ade71487831 100644 --- a/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.tsx +++ b/src/renderer/src/components/settings/general/WorkspaceDirectorySetting.tsx @@ -8,6 +8,8 @@ import { clearHostSettingOverride } from '../../../../../shared/host-setting-overrides' import { LOCAL_EXECUTION_HOST_ID } from '../../../../../shared/execution-host' +import { normalizeWorkspaceDirInput } from '../../../../../shared/worktree/workspace-dir-input' +import { isWindowsUserAgent } from '@/lib/user-agent-platform' import { Button } from '../../ui/button' import { Input } from '../../ui/input' import { Label } from '../../ui/label' @@ -90,7 +92,9 @@ export function WorkspaceDirectorySetting({ } const commitDraftValue = (): void => { - const next = draftValueRef.current + // Show the value main will save, so a padded re-save of the saved path writes nothing. + const next = normalizeWorkspaceDirInput(draftValueRef.current, isWindowsUserAgent()) + setDraft(next) if (next === value) { return } diff --git a/src/shared/worktree/workspace-dir-input.ts b/src/shared/worktree/workspace-dir-input.ts new file mode 100644 index 00000000000..010144c76ed --- /dev/null +++ b/src/shared/worktree/workspace-dir-input.ts @@ -0,0 +1,16 @@ +import { isRuntimePathAbsolute } from '../cross-platform-path' + +/** Leading whitespace before an absolute or home path makes readers resolve it inside every repo (STA-14308). */ +export function normalizeWorkspaceDirInput(value: string, isWindowsHost: boolean): string { + const start = value.trimStart() + // Trailing characters and relative values stay byte-exact: they can be part of a real folder name. + return isRuntimePathAbsolute(start, isWindowsHost ? 'windows' : 'posix') || + isHomePrefixedPath(start, isWindowsHost) + ? start + : value +} + +// Only the shapes main expands; ~\ is a home path on Windows alone. +function isHomePrefixedPath(value: string, isWindowsHost: boolean): boolean { + return value === '~' || value.startsWith('~/') || (isWindowsHost && value.startsWith('~\\')) +}