Skip to content

Commit 54617df

Browse files
Fix panic in overlayfs when saving a file without an overlay (#3849)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: RyanCavanaugh <6685088+RyanCavanaugh@users.noreply.github.com>
1 parent e06d14e commit 54617df

2 files changed

Lines changed: 24 additions & 5 deletions

File tree

internal/project/overlayfs.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,12 +367,14 @@ func (fs *overlayFS) processChanges(changes []FileChange) (FileChangeSummary, ma
367367
}
368368

369369
if events.saved {
370-
if o == nil {
371-
panic("overlay not found for saved file: " + uri)
370+
if o != nil {
371+
o = newOverlay(o.FileName(), o.Content(), o.Version(), o.kind)
372+
o.matchesDiskText = true
373+
newOverlays[path] = o
374+
} else if !events.watchChanged {
375+
// File was saved but never opened via didOpen; treat as a disk change.
376+
result.Changed.Add(uri)
372377
}
373-
o = newOverlay(o.FileName(), o.Content(), o.Version(), o.kind)
374-
o.matchesDiskText = true
375-
newOverlays[path] = o
376378
}
377379

378380
if events.created && o == nil {

internal/project/overlayfs_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,23 @@ func TestProcessChanges(t *testing.T) {
197197
assert.Assert(t, !fs.getFile(testURI1.FileName()).MatchesDiskText())
198198
})
199199

200+
t.Run("save without overlay should not panic", func(t *testing.T) {
201+
t.Parallel()
202+
fs := createOverlayFS()
203+
204+
// Save a file that was never opened (no overlay exists).
205+
// This can happen when an editor sends didSave for a file
206+
// that is not managed by the LSP server (e.g., package.json).
207+
result, _ := fs.processChanges([]FileChange{
208+
{
209+
Kind: FileChangeKindSave,
210+
URI: testURI1,
211+
},
212+
})
213+
// Should be treated as a disk change
214+
assert.Assert(t, result.Changed.Has(testURI1))
215+
})
216+
200217
t.Run("close then open in same batch marks as changed", func(t *testing.T) {
201218
t.Parallel()
202219
fs := createOverlayFS()

0 commit comments

Comments
 (0)