From c6b6d84b632ec5c53b04a2a6557c330accc82163 Mon Sep 17 00:00:00 2001 From: Lesley Murfin Date: Mon, 8 Jun 2026 14:28:16 -0600 Subject: [PATCH 1/2] fix(ssh): sync ~/.ssh/config on import and Manage open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit importFromSshConfig only inserted unknown hosts and never updated an existing target, so a rotated port (or any changed config field) was silently ignored on re-import. Make it an upsert: config-sourced targets are refreshed in place, genuinely new hosts are inserted, and the SSH Manage pane auto-syncs when it opens so changes appear without a manual Import click. A new optional SshTarget.source ('ssh-config' | 'manual') protects user-created and user-edited targets from being overwritten by sync — editing a target in the UI detaches it from config-sync. Unchanged targets are skipped so a repeat sync performs no disk write. Editing a target now also carries cleared optional fields (identity file, proxy command, jump host) so removing a config-derived value actually deletes it instead of silently keeping the stale value through the partial merge. Resolves item #1 of #4684 (item #2 was fixed in #4860). Co-Authored-By: Claude Opus 4.8 (1M context) --- src/main/persistence.test.ts | 86 ++++++++ src/main/ssh/ssh-connection-store.test.ts | 205 +++++++++++++++--- src/main/ssh/ssh-connection-store.ts | 86 +++++++- .../src/components/settings/SshPane.tsx | 44 +++- src/shared/ssh-types.ts | 7 + 5 files changed, 383 insertions(+), 45 deletions(-) diff --git a/src/main/persistence.test.ts b/src/main/persistence.test.ts index a7ba01e8db3d..2ef7f77d4098 100644 --- a/src/main/persistence.test.ts +++ b/src/main/persistence.test.ts @@ -31,9 +31,23 @@ import { ONBOARDING_FINAL_STEP, ONBOARDING_FLOW_VERSION } from '../shared/constants' +import { SshConnectionStore } from './ssh/ssh-connection-store' // Shared mutable state so the electron mock can reference a per-test directory const testState = { dir: '' } + +// Stub the ~/.ssh/config parser so the SSH-import integration test below drives +// the real Store (real normalizeSshTarget + disk round-trip) with deterministic +// config hosts instead of the operator's actual ~/.ssh/config. +const { loadUserSshConfigMock, sshConfigHostsToTargetsMock } = vi.hoisted(() => ({ + loadUserSshConfigMock: vi.fn(), + sshConfigHostsToTargetsMock: vi.fn() +})) + +vi.mock('./ssh/ssh-config-parser', () => ({ + loadUserSshConfig: loadUserSshConfigMock, + sshConfigHostsToTargets: sshConfigHostsToTargetsMock +})) const TEST_LEAF_1 = '11111111-1111-4111-8111-111111111111' const TEST_LEAF_2 = '22222222-2222-4222-8222-222222222222' const TEST_LEAF_LIVE = '33333333-3333-4333-8333-333333333333' @@ -732,6 +746,78 @@ describe('Store', () => { } }) + it('persists the SSH target source field through add, update, and disk round-trip', async () => { + const store = await createStore() + store.addSshTarget({ + id: 'ssh-src-1', + label: 'cluster', + configHost: 'cluster', + host: '10.0.0.5', + port: 2200, + username: 'dev', + source: 'ssh-config' + }) + + // normalizeSshTarget must not strip `source` on update, and the new port + // must take effect — this is the persistence-layer guard for #4684 item #1. + const updated = store.updateSshTarget('ssh-src-1', { port: 2222, source: 'ssh-config' }) + expect(updated?.port).toBe(2222) + expect(updated?.source).toBe('ssh-config') + + expect(store.getSshTarget('ssh-src-1')?.source).toBe('ssh-config') + expect(store.getSshTarget('ssh-src-1')?.port).toBe(2222) + + store.flush() + const persisted = readDataFile() as { sshTargets?: Record[] } + const onDisk = persisted.sshTargets?.find((t) => t.id === 'ssh-src-1') + expect(onDisk?.source).toBe('ssh-config') + expect(onDisk?.port).toBe(2222) + }) + + it('upserts ~/.ssh/config through the real store: rotated port updates in place and persists', async () => { + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + const candidate = (port: number, id: string) => [ + { id, label: 'cluster', configHost: 'cluster', host: '10.0.0.5', port, username: 'dev' } + ] + + const store = await createStore() + const sshStore = new SshConnectionStore(store) + + // First sync inserts the config host, stamped as config-managed. + sshConfigHostsToTargetsMock.mockReturnValue(candidate(2200, 'ssh-cfg-1')) + const inserted = sshStore.importFromSshConfig() + expect(inserted).toHaveLength(1) + expect(inserted[0]?.source).toBe('ssh-config') + expect(inserted[0]?.port).toBe(2200) + + // Rotated port: the upsert must update the SAME target in place — and the + // real normalizeSshTarget must keep `source` and not falsely re-derive + // configHost into a permanently-dirty state. + sshConfigHostsToTargetsMock.mockReturnValue(candidate(2222, 'ssh-cfg-2')) + const changed = sshStore.importFromSshConfig() + expect(changed).toHaveLength(1) + expect(changed[0]?.port).toBe(2222) + expect(changed[0]?.source).toBe('ssh-config') + + // A third identical sync is a no-op (dirty-check against the real persisted + // fields) — proving repeated auto-sync on every pane open writes nothing. + expect(sshStore.importFromSshConfig()).toHaveLength(0) + + // Exactly one cluster target on disk with the rotated port and source kept. + store.flush() + const onDisk = (readDataFile() as { sshTargets?: Record[] }).sshTargets + const clusterTargets = (onDisk ?? []).filter((t) => t.configHost === 'cluster') + expect(clusterTargets).toHaveLength(1) + expect(clusterTargets[0]?.port).toBe(2222) + expect(clusterTargets[0]?.source).toBe('ssh-config') + + // Survives a fresh load from the same data file. + const reloaded = await createStore() + const reloadedCluster = reloaded.getSshTargets().find((t) => t.configHost === 'cluster') + expect(reloadedCluster?.port).toBe(2222) + expect(reloadedCluster?.source).toBe('ssh-config') + }) + it('drops malformed migration-unsupported PTY entries on load', async () => { const repo = makeRepo() writeDataFile({ diff --git a/src/main/ssh/ssh-connection-store.test.ts b/src/main/ssh/ssh-connection-store.test.ts index c6cfd6ab2c8c..0e462eb4f387 100644 --- a/src/main/ssh/ssh-connection-store.test.ts +++ b/src/main/ssh/ssh-connection-store.test.ts @@ -70,6 +70,27 @@ describe('SshConnectionStore', () => { expect(mockStore.addSshTarget).toHaveBeenCalledWith(target) }) + it('addTarget stamps source as manual by default', () => { + const target = sshStore.addTarget({ + label: 'My Server', + host: 'example.com', + port: 22, + username: 'deploy' + }) + expect(target.source).toBe('manual') + }) + + it('addTarget preserves an explicitly provided source', () => { + const target = sshStore.addTarget({ + label: 'My Server', + host: 'example.com', + port: 22, + username: 'deploy', + source: 'ssh-config' + }) + expect(target.source).toBe('ssh-config') + }) + it('updateTarget delegates to store', () => { const original: SshTarget = { id: 'ssh-1', @@ -91,46 +112,172 @@ describe('SshConnectionStore', () => { }) describe('importFromSshConfig', () => { - it('imports new hosts from SSH config', () => { - const configHosts = [{ host: 'staging', hostname: 'staging.example.com' }] - const newTargets: SshTarget[] = [ - { - id: 'ssh-new-1', - label: 'staging', - host: 'staging.example.com', - port: 22, - username: '' - } - ] - - loadUserSshConfigMock.mockReturnValue(configHosts) - sshConfigHostsToTargetsMock.mockReturnValue(newTargets) + function candidate(overrides: Partial & { configHost: string }): SshTarget { + return { + id: `tmp-${overrides.configHost}`, + label: overrides.configHost, + host: `${overrides.configHost}.example.com`, + port: 22, + username: '', + ...overrides + } + } + + it('inserts a new config host stamped as ssh-config', () => { + loadUserSshConfigMock.mockReturnValue([{ host: 'staging' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ configHost: 'staging', host: 'staging.example.com' }) + ]) const result = sshStore.importFromSshConfig() - expect(result).toEqual(newTargets) - expect(mockStore.addSshTarget).toHaveBeenCalledWith(newTargets[0]) + expect(mockStore.addSshTarget).toHaveBeenCalledWith( + expect.objectContaining({ configHost: 'staging', source: 'ssh-config' }) + ) + expect(result).toHaveLength(1) + expect(result[0].source).toBe('ssh-config') }) - it('passes existing target labels to avoid duplicates', () => { - const existing: SshTarget = { - id: 'ssh-existing', - label: 'production', - host: 'prod.example.com', - port: 22, - username: 'deploy' - } - mockStore.addSshTarget(existing) - - loadUserSshConfigMock.mockReturnValue([]) + it('asks the parser for all hosts — reconciliation happens in the store', () => { + loadUserSshConfigMock.mockReturnValue([{ host: 'a' }]) sshConfigHostsToTargetsMock.mockReturnValue([]) sshStore.importFromSshConfig() - expect(sshConfigHostsToTargetsMock).toHaveBeenCalledWith([], new Set(['production'])) + expect(sshConfigHostsToTargetsMock).toHaveBeenCalledWith([{ host: 'a' }], new Set()) + }) + + // PRIMARY regression (#4684 item #1): a rotated port must take effect on + // re-import instead of silently keeping the stale value. + it('updates an existing config-sourced target when the port changed', () => { + mockStore.addSshTarget({ + id: 'ssh-1', + label: 'cluster', + configHost: 'cluster', + host: '10.0.0.5', + port: 2200, + username: 'dev', + source: 'ssh-config' + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' }) + ]) + + const result = sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).toHaveBeenCalledWith( + 'ssh-1', + expect.objectContaining({ port: 2222, source: 'ssh-config' }) + ) + // Only the seed insert — no duplicate target created. + expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1) + expect(result).toHaveLength(1) + expect(result[0].port).toBe(2222) + }) + + it('refreshes host, username, and jump host on sync, not just port', () => { + mockStore.addSshTarget({ + id: 'ssh-1', + label: 'box', + configHost: 'box', + host: 'old.example.com', + port: 22, + username: 'old', + source: 'ssh-config' + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'box' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ + configHost: 'box', + host: 'new.example.com', + port: 2200, + username: 'newuser', + jumpHost: 'bastion' + }) + ]) + + sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).toHaveBeenCalledWith( + 'ssh-1', + expect.objectContaining({ + host: 'new.example.com', + port: 2200, + username: 'newuser', + jumpHost: 'bastion' + }) + ) + }) + + it('never overwrites a manual target that owns the alias', () => { + mockStore.addSshTarget({ + id: 'ssh-m', + label: 'cluster', + configHost: 'cluster', + host: 'manual.example.com', + port: 22, + username: 'me', + source: 'manual' + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ configHost: 'cluster', host: '10.0.0.9', port: 2222, username: 'dev' }) + ]) + + const result = sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).not.toHaveBeenCalled() + // Only the manual seed insert — the config alias is not duplicated. + expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1) + expect(result).toEqual([]) + }) + + it('adopts a legacy unsourced target into config-sync', () => { + mockStore.addSshTarget({ + id: 'ssh-legacy', + label: 'cluster', + configHost: 'cluster', + host: '10.0.0.5', + port: 2200, + username: 'dev' + // no source — predates the field + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' }) + ]) + + const result = sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).toHaveBeenCalledWith( + 'ssh-legacy', + expect.objectContaining({ port: 2222, source: 'ssh-config' }) + ) + expect(result[0].source).toBe('ssh-config') + }) + + it('does not rewrite an unchanged config-sourced target', () => { + mockStore.addSshTarget({ + id: 'ssh-1', + label: 'cluster', + configHost: 'cluster', + host: 'cluster.example.com', + port: 22, + username: '', + source: 'ssh-config' + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + // Candidate is identical to the persisted target (same default fields). + sshConfigHostsToTargetsMock.mockReturnValue([candidate({ configHost: 'cluster' })]) + + const result = sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).not.toHaveBeenCalled() + expect(result).toEqual([]) }) - it('returns empty array when no new hosts found', () => { + it('returns empty array when nothing changed', () => { loadUserSshConfigMock.mockReturnValue([]) sshConfigHostsToTargetsMock.mockReturnValue([]) diff --git a/src/main/ssh/ssh-connection-store.ts b/src/main/ssh/ssh-connection-store.ts index 50e758c3e797..7d24fc8ebb94 100644 --- a/src/main/ssh/ssh-connection-store.ts +++ b/src/main/ssh/ssh-connection-store.ts @@ -17,6 +17,9 @@ export class SshConnectionStore { const full: SshTarget = { ...target, configHost: target.configHost ?? target.host, + // Why: default to 'manual' so user-created targets are never overwritten + // by a later ~/.ssh/config import (only 'ssh-config' targets are synced). + source: target.source ?? 'manual', id: `ssh-${Date.now()}-${Math.random().toString(36).slice(2, 8)}` } this.store.addSshTarget(full) @@ -32,18 +35,87 @@ export class SshConnectionStore { } /** - * Import hosts from ~/.ssh/config that don't already exist as targets. - * Returns the newly imported targets. + * Sync targets from ~/.ssh/config: insert new hosts, update existing + * config-sourced ones in place (so a rotated port takes effect), never touch + * manual targets. Returns the inserted and updated targets. */ importFromSshConfig(): SshTarget[] { - const existingLabels = new Set(this.store.getSshTargets().map((t) => t.configHost ?? t.label)) const configHosts = loadUserSshConfig() - const newTargets = sshConfigHostsToTargets(configHosts, existingLabels) + const existingTargets = this.store.getSshTargets() + // Map config-managed targets (and legacy unsourced ones) by their config + // alias so a repeat import reconciles instead of duplicating. Manual targets + // are excluded — their alias stays reserved and untouched. + const syncableByAlias = new Map() + const manualAliases = new Set() + for (const existing of existingTargets) { + const alias = existing.configHost ?? existing.label + if (existing.source === 'manual') { + manualAliases.add(alias) + continue + } + if (alias && !syncableByAlias.has(alias)) { + syncableByAlias.set(alias, existing) + } + } + + // Pass an empty exclusion set so the parser returns a candidate for every + // config host (within-config de-duplication still applies); reconciliation + // against existing targets happens here. + const candidates = sshConfigHostsToTargets(configHosts, new Set()) + const changed: SshTarget[] = [] + // Guard against ever processing the same alias twice in one pass, so a + // duplicate candidate can never produce a duplicate target — independent of + // the parser's own within-config de-duplication. + const processedAliases = new Set() - for (const target of newTargets) { - this.store.addSshTarget(target) + for (const candidate of candidates) { + const alias = candidate.configHost ?? candidate.label + if (manualAliases.has(alias)) { + // A manual target owns this alias — never clobber it. + continue + } + if (processedAliases.has(alias)) { + continue + } + processedAliases.add(alias) + const existing = syncableByAlias.get(alias) + if (existing) { + const nextFields = { + configHost: candidate.configHost, + host: candidate.host, + port: candidate.port, + username: candidate.username, + identityFile: candidate.identityFile, + identityAgent: candidate.identityAgent, + identitiesOnly: candidate.identitiesOnly, + proxyCommand: candidate.proxyCommand, + jumpHost: candidate.jumpHost + } + // Skip the write (and the "synced" report) when nothing changed, so a + // repeat sync on every pane open is a no-op. A legacy target with no + // `source` is always rewritten once to stamp it as config-managed. + const isDirty = + existing.source !== 'ssh-config' || + (Object.keys(nextFields) as (keyof typeof nextFields)[]).some( + (key) => existing[key] !== nextFields[key] + ) + if (!isDirty) { + continue + } + const updated = this.store.updateSshTarget(existing.id, { + ...nextFields, + source: 'ssh-config' + }) + if (updated) { + changed.push(updated) + } + } else { + const inserted: SshTarget = { ...candidate, source: 'ssh-config' } + this.store.addSshTarget(inserted) + changed.push(inserted) + } } - return newTargets + return changed } } diff --git a/src/renderer/src/components/settings/SshPane.tsx b/src/renderer/src/components/settings/SshPane.tsx index 406769712004..72e26e5567cf 100644 --- a/src/renderer/src/components/settings/SshPane.tsx +++ b/src/renderer/src/components/settings/SshPane.tsx @@ -56,7 +56,20 @@ export function SshPane(_props: SshPaneProps): React.JSX.Element { useEffect(() => { const abortController = new AbortController() - void loadTargets({ signal: abortController.signal }) + // Why: auto-sync ~/.ssh/config when the Manage pane opens so rotated ports + // and newly added hosts appear without a manual Import click. Best-effort — + // a sync failure must not block listing the already-known targets. + void (async () => { + try { + await window.api.ssh.importConfig() + } catch { + // Surfaced on demand via the explicit Import button; ignore here. + } + if (abortController.signal.aborted) { + return + } + await loadTargets({ signal: abortController.signal }) + })() return () => abortController.abort() }, [loadTargets]) @@ -80,6 +93,10 @@ export function SshPane(_props: SshPaneProps): React.JSX.Element { return } + const identityFile = form.identityFile.trim() || undefined + const proxyCommand = form.proxyCommand.trim() || undefined + const jumpHost = form.jumpHost.trim() || undefined + const target = { label: form.label.trim() || (username ? `${username}@${host}` : configHost), configHost, @@ -87,14 +104,23 @@ export function SshPane(_props: SshPaneProps): React.JSX.Element { port, username, relayGracePeriodSeconds: graceSeconds, - ...(form.identityFile.trim() ? { identityFile: form.identityFile.trim() } : {}), - ...(form.proxyCommand.trim() ? { proxyCommand: form.proxyCommand.trim() } : {}), - ...(form.jumpHost.trim() ? { jumpHost: form.jumpHost.trim() } : {}) + ...(identityFile ? { identityFile } : {}), + ...(proxyCommand ? { proxyCommand } : {}), + ...(jumpHost ? { jumpHost } : {}) } try { await (editingId - ? window.api.ssh.updateTarget({ id: editingId, updates: target }) + ? // Why: an explicit edit takes ownership of the target, so mark it + // `manual` — otherwise the next ~/.ssh/config sync would silently + // revert the user's change back to the config value. Carry the + // optional fields even when cleared (undefined) so removing e.g. a + // config-derived ProxyCommand actually deletes it — updateTarget + // merges partially, so an omitted key would keep the stale value. + window.api.ssh.updateTarget({ + id: editingId, + updates: { ...target, identityFile, proxyCommand, jumpHost, source: 'manual' } + }) : window.api.ssh.addTarget({ target })) recordFeatureInteraction('ssh') if (!mountedRef.current) { @@ -220,13 +246,13 @@ export function SshPane(_props: SshPaneProps): React.JSX.Element { const handleImport = async (): Promise => { try { - const imported = (await window.api.ssh.importConfig()) as SshTarget[] + const synced = (await window.api.ssh.importConfig()) as SshTarget[] recordFeatureInteraction('ssh') if (mountedRef.current) { - if (imported.length === 0) { - toast('No new hosts found in ~/.ssh/config') + if (synced.length === 0) { + toast('~/.ssh/config already in sync') } else { - toast.success(`Imported ${imported.length} host${imported.length > 1 ? 's' : ''}`) + toast.success(`Synced ${synced.length} server${synced.length > 1 ? 's' : ''}`) } } await loadTargets() diff --git a/src/shared/ssh-types.ts b/src/shared/ssh-types.ts index b1d4868a10f1..c5f0957a6783 100644 --- a/src/shared/ssh-types.ts +++ b/src/shared/ssh-types.ts @@ -23,6 +23,13 @@ export type SshTarget = { proxyCommand?: string /** Jump host (ProxyJump), if any. */ jumpHost?: string + /** Where this target came from. `ssh-config` targets are kept in sync with + * `~/.ssh/config` on import — their config-derived fields (host, port, + * username, jump host, identity, proxy) are refreshed on each import. + * `manual` targets are never overwritten by import. Legacy persisted targets + * predate this field (undefined) and are adopted into config-sync on next + * import. */ + source?: 'ssh-config' | 'manual' /** Grace period in seconds before relay shuts down after disconnect. * 0 disables expiry. Default: 10800 (3 hours). Max: 604800 (7 days). */ relayGracePeriodSeconds?: number From dbf4925be62b8fe5828ebe9cf3b5d1d40f22637d Mon Sep 17 00:00:00 2001 From: Jinwoo-H Date: Mon, 8 Jun 2026 14:51:42 -0700 Subject: [PATCH 2/2] fix(ssh): preserve legacy manual targets during config sync Co-authored-by: Orca --- src/main/ssh/ssh-connection-store.test.ts | 22 ++++++++++++++++++++++ src/main/ssh/ssh-connection-store.ts | 22 ++++++++++++++++++---- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/src/main/ssh/ssh-connection-store.test.ts b/src/main/ssh/ssh-connection-store.test.ts index 0e462eb4f387..4d9215e05f8e 100644 --- a/src/main/ssh/ssh-connection-store.test.ts +++ b/src/main/ssh/ssh-connection-store.test.ts @@ -257,6 +257,28 @@ describe('SshConnectionStore', () => { expect(result[0].source).toBe('ssh-config') }) + it('does not overwrite a legacy unsourced manual target with the same alias', () => { + mockStore.addSshTarget({ + id: 'ssh-legacy-manual', + label: 'cluster', + configHost: 'cluster', + host: 'cluster', + port: 2200, + username: 'me' + // no source — predates the field, but does not look like a config import + }) + loadUserSshConfigMock.mockReturnValue([{ host: 'cluster' }]) + sshConfigHostsToTargetsMock.mockReturnValue([ + candidate({ configHost: 'cluster', host: '10.0.0.5', port: 2222, username: 'dev' }) + ]) + + const result = sshStore.importFromSshConfig() + + expect(mockStore.updateSshTarget).not.toHaveBeenCalled() + expect(mockStore.addSshTarget).toHaveBeenCalledTimes(1) + expect(result).toEqual([]) + }) + it('does not rewrite an unchanged config-sourced target', () => { mockStore.addSshTarget({ id: 'ssh-1', diff --git a/src/main/ssh/ssh-connection-store.ts b/src/main/ssh/ssh-connection-store.ts index 7d24fc8ebb94..18c1b9afde0a 100644 --- a/src/main/ssh/ssh-connection-store.ts +++ b/src/main/ssh/ssh-connection-store.ts @@ -42,14 +42,18 @@ export class SshConnectionStore { importFromSshConfig(): SshTarget[] { const configHosts = loadUserSshConfig() const existingTargets = this.store.getSshTargets() - // Map config-managed targets (and legacy unsourced ones) by their config - // alias so a repeat import reconciles instead of duplicating. Manual targets - // are excluded — their alias stays reserved and untouched. + // Map config-managed targets (and legacy targets that strongly look like + // prior imports) by their config alias so a repeat import reconciles instead + // of duplicating. Manual targets are excluded — their alias stays reserved + // and untouched. const syncableByAlias = new Map() const manualAliases = new Set() for (const existing of existingTargets) { const alias = existing.configHost ?? existing.label - if (existing.source === 'manual') { + if ( + existing.source === 'manual' || + (existing.source === undefined && !isLegacyConfigImportTarget(existing)) + ) { manualAliases.add(alias) continue } @@ -119,3 +123,13 @@ export class SshConnectionStore { return changed } } + +function isLegacyConfigImportTarget(target: SshTarget): boolean { + const alias = target.configHost ?? target.label + // Why: legacy manual and imported targets both lack `source`. Only adopt the + // old import shape, where the SSH alias was kept as label/configHost while + // host stored the resolved HostName; otherwise preserve the user's target. + return Boolean( + alias && target.label === alias && target.configHost === alias && target.host !== alias + ) +}