diff --git a/src/renderer/src/components/editor/DiffSectionItem.tsx b/src/renderer/src/components/editor/DiffSectionItem.tsx index 0d897fdd9..b3cdbbd11 100644 --- a/src/renderer/src/components/editor/DiffSectionItem.tsx +++ b/src/renderer/src/components/editor/DiffSectionItem.tsx @@ -3,6 +3,7 @@ import { LazySection } from './LazySection' import { ChevronDown, ChevronRight, ExternalLink } from 'lucide-react' import { DiffEditor, type DiffOnMount } from '@monaco-editor/react' import type { editor as monacoEditor } from 'monaco-editor' +import { monaco } from '@/lib/monaco-setup' import { joinPath } from '@/lib/path' import { detectLanguage } from '@/lib/language-detect' import { useAppStore } from '@/store' @@ -126,6 +127,10 @@ export function DiffSectionItem({ ) const language = detectLanguage(section.path) const isEditable = section.area === 'unstaged' + const modelPathBase = useMemo( + () => `diff-section:${encodeURIComponent(worktreeId)}:${encodeURIComponent(section.key)}`, + [section.key, worktreeId] + ) const editorFontSize = computeEditorFontSize( settings?.terminalFontSize ?? 13, editorFontZoomLevel @@ -136,6 +141,31 @@ export function DiffSectionItem({ const lineNumberOptionsSubRef = useRef<{ dispose: () => void } | null>(null) const [popover, setPopover] = useState<{ lineNumber: number; top: number } | null>(null) + const disposeDiffModels = React.useCallback(() => { + window.setTimeout(() => { + const originalModel = monaco.editor.getModel(monaco.Uri.parse(`${modelPathBase}:original`)) + const modifiedModel = monaco.editor.getModel(monaco.Uri.parse(`${modelPathBase}:modified`)) + if (!originalModel?.isAttachedToEditor()) { + originalModel?.dispose() + } + if (!modifiedModel?.isAttachedToEditor()) { + modifiedModel?.dispose() + } + }, 0) + }, [modelPathBase]) + + useEffect(() => { + if (section.collapsed) { + disposeDiffModels() + } + }, [disposeDiffModels, section.collapsed]) + + useEffect(() => { + return () => { + disposeDiffModels() + } + }, [disposeDiffModels]) + useDiffCommentDecorator({ editor: modifiedEditor, filePath: section.path, @@ -397,6 +427,13 @@ export function DiffSectionItem({ modified={section.modifiedContent} theme={isDark ? 'vs-dark' : 'vs'} onMount={handleMount} + // Why: @monaco-editor/react disposes diff models before the diff + // widget during unmount, which can throw from Monaco. Keep them + // through widget teardown and dispose them on the next tick above. + originalModelPath={`${modelPathBase}:original`} + modifiedModelPath={`${modelPathBase}:modified`} + keepCurrentOriginalModel + keepCurrentModifiedModel options={{ readOnly: !isEditable, originalEditable: false, diff --git a/src/renderer/src/hooks/useEditorExternalWatch.ts b/src/renderer/src/hooks/useEditorExternalWatch.ts index 5a2bc9fa4..416ef1913 100644 --- a/src/renderer/src/hooks/useEditorExternalWatch.ts +++ b/src/renderer/src/hooks/useEditorExternalWatch.ts @@ -28,6 +28,15 @@ import type { OpenFile } from '@/store/slices/editor' const EXTERNAL_RELOAD_DEBOUNCE_MS = 75 const pendingExternalReloadTimers = new Map() +function warnExternalWatchFailure(target: WatchedTarget, err: unknown): void { + console.warn('[filesystem-watch] failed to watch worktree', { + worktreeId: target.worktreeId, + worktreePath: target.worktreePath, + connectionId: target.connectionId, + error: err instanceof Error ? err.message : String(err) + }) +} + function scheduleDebouncedExternalReload(notification: { worktreeId: string worktreePath: string @@ -147,10 +156,17 @@ export function useEditorExternalWatch(): void { }) } for (const target of added) { - void window.api.fs.watchWorktree({ - worktreePath: target.worktreePath, - connectionId: target.connectionId - }) + void window.api.fs + .watchWorktree({ + worktreePath: target.worktreePath, + connectionId: target.connectionId + }) + .catch((err) => { + // Why: remote SSH providers can disappear while tabs still reference + // the worktree. Watching should degrade to a diagnostic, not an + // uncaught renderer promise that looks like the terminal froze. + warnExternalWatchFailure(target, err) + }) } targetsRef.current = nextTargets // Why: this effect is intentionally differential — it does not unwatch on