@@ -6,6 +6,7 @@ package cache
6
6
7
7
import (
8
8
"context"
9
+ "fmt"
9
10
"strconv"
10
11
"strings"
11
12
"sync"
@@ -328,11 +329,6 @@ func (s *Session) DidModifyFiles(ctx context.Context, changes []source.FileModif
328
329
if c .Action == source .InvalidateMetadata {
329
330
forceReloadMetadata = true
330
331
}
331
- // Do nothing if the file is open in the editor and we receive
332
- // an on-disk action. The editor is the source of truth.
333
- if s .isOpen (c .URI ) && c .OnDisk {
334
- continue
335
- }
336
332
// Look through all of the session's views, invalidating the file for
337
333
// all of the views to which it is known.
338
334
for _ , view := range s .views {
@@ -379,13 +375,20 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
379
375
defer s .overlayMu .Unlock ()
380
376
381
377
for _ , c := range changes {
382
- // Don't update overlays for on-disk changes or metadata invalidations.
383
- if c .OnDisk || c . Action == source .InvalidateMetadata {
378
+ // Don't update overlays for metadata invalidations.
379
+ if c .Action == source .InvalidateMetadata {
384
380
continue
385
381
}
386
382
387
383
o , ok := s .overlays [c .URI ]
388
384
385
+ // If the file is not opened in an overlay and the change is on disk,
386
+ // there's no need to update an overlay. If there is an overlay, we
387
+ // may need to update the overlay's saved value.
388
+ if ! ok && c .OnDisk {
389
+ continue
390
+ }
391
+
389
392
// Determine the file kind on open, otherwise, assume it has been cached.
390
393
var kind source.FileKind
391
394
switch c .Action {
@@ -408,35 +411,46 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
408
411
}
409
412
410
413
// If the file is on disk, check if its content is the same as in the
411
- // overlay. Saves don't necessarily come with the file's content.
414
+ // overlay. Saves and on-disk file changes don't come with the file's
415
+ // content.
412
416
text := c .Text
413
- if text == nil && c .Action == source .Save {
417
+ if text == nil && (c .Action == source .Save || c .OnDisk ) {
418
+ if ! ok {
419
+ return nil , fmt .Errorf ("no known content for overlay for %s" , c .Action )
420
+ }
414
421
text = o .text
415
422
}
423
+ // On-disk changes don't come with versions.
424
+ version := c .Version
425
+ if c .OnDisk {
426
+ version = o .version
427
+ }
416
428
hash := hashContents (text )
417
429
var sameContentOnDisk bool
418
430
switch c .Action {
419
- case source .Open :
420
- fh , err := s .cache .getFile (ctx , c .URI )
421
- if err != nil {
422
- return nil , err
423
- }
424
- _ , readErr := fh .Read ()
425
- sameContentOnDisk = (readErr == nil && fh .Identity ().Identifier == hash )
431
+ case source .Delete :
432
+ // Do nothing. sameContentOnDisk should be false.
426
433
case source .Save :
427
434
// Make sure the version and content (if present) is the same.
428
- if o .version != c . Version {
435
+ if o .version != version {
429
436
return nil , errors .Errorf ("updateOverlays: saving %s at version %v, currently at %v" , c .URI , c .Version , o .version )
430
437
}
431
438
if c .Text != nil && o .hash != hash {
432
439
return nil , errors .Errorf ("updateOverlays: overlay %s changed on save" , c .URI )
433
440
}
434
441
sameContentOnDisk = true
442
+ default :
443
+ fh , err := s .cache .getFile (ctx , c .URI )
444
+ if err != nil {
445
+ return nil , err
446
+ }
447
+ _ , readErr := fh .Read ()
448
+ sameContentOnDisk = (readErr == nil && fh .Identity ().Identifier == hash )
435
449
}
436
450
o = & overlay {
437
451
session : s ,
438
452
uri : c .URI ,
439
- version : c . Version ,
453
+ version : version ,
440
454
text : text ,
441
455
kind : kind ,
442
456
hash : hash ,
@@ -445,7 +459,8 @@ func (s *Session) updateOverlays(ctx context.Context, changes []source.FileModif
445
459
s .overlays [c .URI ] = o
446
460
}
447
461
448
- // Get the overlays for each change while the session's overlay map is locked.
462
+ // Get the overlays for each change while the session's overlay map is
463
+ // locked.
449
464
overlays := make (map [span.URI ]* overlay )
450
465
for _ , c := range changes {
451
466
if o , ok := s .overlays [c .URI ]; ok {
0 commit comments