Skip to content

fix(editor): stop a failed diff load from being saved over the file - #14509

Closed
nwparker wants to merge 2 commits into
mainfrom
nwparker/fix-diff-error-pane-save
Closed

nwparker wants to merge 2 commits into
mainfrom
nwparker/fix-diff-error-pane-save

Conversation

@nwparker

@nwparker nwparker commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor
Files Added Deleted Net
Test 4 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​580 $\color{#cf222e}{\Huge{\mathbf{−}}}$​3 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​577
Prod 6 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​88 $\color{#cf222e}{\Huge{\mathbf{−}}}$​34 $\color{#1a7f37}{\Huge{\mathbf{+}}}$​54

The bug

When a diff RPC fails, useEditorPanelContentState fabricates a result so the pane has something to render:

{ kind: 'text', originalContent: '', modifiedContent: `Error loading diff: ${String(err)}` }

For an unstaged diff that pane is editable. Cmd+S calls onSave(modifiedEditor.getValue()), and attemptEditorFileSave falls back to the displayed body when no draft exists (editor-autosave-controller.ts). The error sentence gets written over the user's file.

Reachable from any diff-load failure — SSH drop, git error, permission denied, or the diff_too_large added in #14160.

Why the existing guard misses it

largeDiffSaveContentAvailable already exists for exactly this hazard, but it only gates the LargeDiffFallback button, which renders when largeDiffRenderLimit.limited is true. On the error path that field is absent, so renderLimit.limited is false, the full DiffEditor mounts, and installEditorSaveShortcut is installed — gated only by editable. The guard never runs.

The fix

Mark the fabricated result and hold the pane read-only.

Read-only rather than just gating onSave, because a draft is the other route: typing into the pane mints one via onContentChange, and a draft is saveable even when the fallback body is not. editable={false} closes both — installEditorSaveShortcut sits inside if (editable).

loadError mirrors the field FileContent already carries for the plain-file path, and lives on the renderer-local DiffContent (GitDiffResult & {...}), so nothing on the wire changes.

Scope

4 files, +138/−6 — most of it the new test.

File Change
editor-panel-content-types.ts loadError?: boolean on DiffContent
useEditorPanelContentState.ts set it on the fabricated result
EditorContent.tsx diffEditable gates editable / onContentChange / onSave
EditorContent.diff-load-error.test.tsx new

Testing

  • Mutation-verified: reverting diffEditable to plain isEditable fails exactly the new read-only case, and only that case.
  • 23094 renderer tests pass; web and node typechecks clean.

Not in scope

The error pane's presentation is unchanged — it still shows the message inside a diff view rather than the dedicated FileLoadErrorView the plain-file path uses. That is a UX improvement worth doing separately; this PR only removes the data-loss path.

Made with Orca 🐋

@coderabbitai

coderabbitai Bot commented Aug 14, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a884d5ff-dd9b-4160-a7d1-d20d23628404

📥 Commits

Reviewing files that changed from the base of the PR and between 71bbab7 and 56c307d.

📒 Files selected for processing (10)
  • src/renderer/src/components/editor/DiffViewer.editable-rewiring.test.tsx
  • src/renderer/src/components/editor/DiffViewer.tsx
  • src/renderer/src/components/editor/EditorContent.diff-load-error.test.tsx
  • src/renderer/src/components/editor/EditorContent.tsx
  • src/renderer/src/components/editor/editor-panel-content-types.ts
  • src/renderer/src/components/editor/useDiffViewerEditingWiring.ts
  • src/renderer/src/components/editor/useEditorPanelContentState.diff-load-error.test.tsx
  • src/renderer/src/components/editor/useEditorPanelDiffContentLoader.ts
  • src/renderer/src/components/editor/useEditorPanelExternalContentEvents.test.tsx
  • src/renderer/src/components/editor/useEditorPanelExternalContentEvents.ts
🚧 Files skipped from review as they are similar to previous changes (2)
  • src/renderer/src/components/editor/editor-panel-content-types.ts
  • src/renderer/src/components/editor/EditorContent.tsx

Included review availability: Your plan includes up to 10 reviews per rolling hour; 6 remain after this review.


📝 Walkthrough

Walkthrough

Diff content now records load failures with loadError. Failed diffs are read-only unless an existing edit buffer is present. Saved or recovered content clears the error state. DiffViewer editing shortcuts and content listeners now use a dedicated hook that rewires them when editability changes without remounting or stealing focus. Tests cover load failures, recovery, saved content, edit preservation, and wiring cleanup.

Merge Risk: ⚪ Minimal · up to 56c30

The change prevents a failed diff-load message from being saved over the user's file by making the error pane read-only; no actionable merge-blocking risk remains after normal checks and review.

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The description explains the bug, fix, scope, and testing, but it omits required template sections such as Linked Issue, Visual Proof, and the checklist. Add the required template sections, provide a linked issue, include visual proof or N/A with a reason, and complete the testing and review checklists.
Docstring Coverage ⚠️ Warning Docstring coverage is 20.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly states that failed diff-load content must not be saved over the file.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches 💡 1
📝 Generate docstrings 💡
  • Create stacked PR
  • Commit on current branch

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

nwparker and others added 2 commits August 16, 2026 21:22
When a diff RPC fails, useEditorPanelContentState fabricates a result whose
modifiedContent is \`Error loading diff: \${err}\` so the pane has something to
show. For an unstaged diff that pane is editable, so Cmd+S calls onSave with the
editor body — and attemptEditorFileSave falls back to that body when no draft
exists (editor-autosave-controller). The error sentence gets written over the
user's file.

Reachable from any diff-load failure: SSH drop, git error, permission denied, or
the diff_too_large introduced in #14160.

The existing largeDiffSaveContentAvailable guard does not cover this. It only
gates the LargeDiffFallback button, which renders when largeDiffRenderLimit is
limited; on the error path that field is absent, so the full editable DiffEditor
mounts and installs the save shortcut instead.

Marks the fabricated result and holds the pane read-only, which blocks the save
shortcut and prevents a draft being minted — a draft would otherwise be saveable
even with onSave gated. loadError mirrors the field FileContent already carries
for the plain-file path, and lives on the renderer-local DiffContent, so nothing
on the wire changes.

Test: mutation-verified — dropping the guard fails exactly the new read-only
case. 23094 renderer tests pass; web and node typechecks clean.

Co-authored-by: Orca <[email protected]>
…covers

Two problems with gating the diff pane on the fetch outcome alone:

The pane renders `modifiedDiffBuffer ?? dc.modifiedContent`, so when a draft
exists it shows the user's own text, not the error message. Holding that
read-only froze a dirty tab with a dead Cmd+S and no explanation. Gate on what
is actually rendered instead — the error body can only reach the editor when
there is no draft.

This also made a diff pane's `editable` dynamic for the first time, and
DiffViewer wired editing only in onMount, which @monaco-editor/react pins to
the first render. A pane recovering from a failed load became typable with no
save shortcut and no change subscription: keystrokes minted no draft, the tab
never went dirty, and the text was lost on the next remount. Move that wiring
into an effect keyed on `[editable, modifiedEditor]`. Remounting would have
worked too, but the mount-time focus grab would steal keystrokes, since the
recovery is an unattended background refetch.

Also drops loadError when a save replaces the body, so the pane does not stay
read-only while showing correct on-disk content.
@nwparker
nwparker force-pushed the nwparker/fix-diff-error-pane-save branch from f0de825 to 56c307d Compare August 17, 2026 04:23
@coderabbitai

coderabbitai Bot commented Aug 17, 2026

Copy link
Copy Markdown
Contributor

Note

GitHub couldn't provide a complete incremental comparison for this pull request, so CodeRabbit is performing a full review instead. This review may take a little longer.

nwparker added a commit that referenced this pull request Oct 5, 2026
Adapted from #14509 to the current split editor surface.

Co-authored-by: Orca <[email protected]>
@OrcaWin

OrcaWin commented Oct 26, 2026

Copy link
Copy Markdown
Collaborator

Closing as superseded by the still-open #25407, which explicitly adapts this fix to the current editor modules.

I compared all six production files in this PR with the replacement. #25407 carries the renderer-local loadError flag at the failed-diff boundary; keeps an unstaged error body read-only unless a genuine draft exists (including an empty draft); gates both editing and Save; removes the flag after a genuine save; and rewires Save/Find/change subscriptions when the mounted editor's editability changes, with cleanup and without a recovery-time focus grab. Its tests retain error/draft/reload and editing-callback controls and add an actual-file preservation/recovery E2E.

The hook extraction in this version is replaced by the same effect lifetime directly in DiffViewer; the consumer moved from EditorContent to EditorDiffFileSurface. Those are implementation differences, not remaining unique behavior. #25407 remains open as the implementation to pursue; this closure does not claim the failed-diff data-loss fix has shipped.

@OrcaWin OrcaWin closed this Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants