From 8a95fa1eda11fab170db7eeaf86e519d3d4868ce Mon Sep 17 00:00:00 2001 From: bincxz <16399091+binaricat@users.noreply.github.com> Date: Wed, 15 Jul 2026 00:41:40 +0800 Subject: [PATCH] fix(sftp): sync reconnect host on tab reuse and home-fallback stale paths Update lastConnectedHost when auto-connect reselects an existing tab, and always try the detected home directory when a remembered start path fails. --- application/state/sftp/useSftpConnections.ts | 3 ++- application/state/useSftpState.ts | 12 ++++++++++++ components/SftpSidePanel.tsx | 3 +++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/application/state/sftp/useSftpConnections.ts b/application/state/sftp/useSftpConnections.ts index 88963c9a3..781f92f1f 100644 --- a/application/state/sftp/useSftpConnections.ts +++ b/application/state/sftp/useSftpConnections.ts @@ -627,8 +627,9 @@ export const useSftpConnections = ({ dirCacheRef.current.delete(provisionalCacheKey); } // Fall back to homeDir, then "/", chaining attempts. + // Remembered/reconnect paths can be stale even when shared cache is gone. let fallbackSucceeded = false; - if (sharedHostCache && startPath !== homeDir) { + if (startPath !== homeDir) { try { startPath = homeDir; files = await listRemoteFiles(sftpId, startPath, filenameEncoding); diff --git a/application/state/useSftpState.ts b/application/state/useSftpState.ts index a9966c9f8..5af801dc1 100644 --- a/application/state/useSftpState.ts +++ b/application/state/useSftpState.ts @@ -160,6 +160,15 @@ export const useSftpState = ( right: null, }); + // Keep reconnect metadata in sync when auto-connect reuses an existing tab + // without calling connect() (selectTab alone does not update this ref). + const setLastConnectedHost = useCallback(( + side: "left" | "right", + host: Host | "local" | null, + ) => { + lastConnectedHostRef.current[side] = host; + }, []); + const handleSessionError = useSftpSessionErrors({ getActivePane, leftTabsRef, @@ -418,6 +427,7 @@ export const useSftpState = ( resolveConflict: resolveAnyConflict, getSftpIdForConnection, getConnectionCacheKey, + setLastConnectedHost, reportSessionError: handleSessionError, rejectHostKeyVerification, acceptHostKeyVerification, @@ -480,6 +490,7 @@ export const useSftpState = ( resolveConflict: resolveAnyConflict, getSftpIdForConnection, getConnectionCacheKey, + setLastConnectedHost, reportSessionError: handleSessionError, rejectHostKeyVerification, acceptHostKeyVerification, @@ -556,6 +567,7 @@ export const useSftpState = ( resolveConflict: (...args: Parameters) => methodsRef.current.resolveConflict(...args), getSftpIdForConnection: (...args: Parameters) => methodsRef.current.getSftpIdForConnection(...args), getConnectionCacheKey: (...args: Parameters) => methodsRef.current.getConnectionCacheKey(...args), + setLastConnectedHost: (...args: Parameters) => methodsRef.current.setLastConnectedHost(...args), reportSessionError: (...args: Parameters) => methodsRef.current.reportSessionError(...args), rejectHostKeyVerification: () => methodsRef.current.rejectHostKeyVerification(), acceptHostKeyVerification: () => methodsRef.current.acceptHostKeyVerification(), diff --git a/components/SftpSidePanel.tsx b/components/SftpSidePanel.tsx index 46964e81b..007d19141 100644 --- a/components/SftpSidePanel.tsx +++ b/components/SftpSidePanel.tsx @@ -351,6 +351,9 @@ const SftpSidePanelInner: React.FC = ({ ); if (existingTab) { s.selectTab("left", existingTab.id); + // selectTab does not update reconnect metadata; keep lastConnectedHost + // aligned with the tab we just activated so channel drops rebind correctly. + s.setLastConnectedHost?.("left", activeHost); connectedKeyRef.current = connectionKey; connectedHostObjRef.current = activeHost; // Session memory keys are per terminal session; republish the visible