diff --git a/internal/project/snapshot_test.go b/internal/project/snapshot_test.go index a075039b33..ded5e16b5e 100644 --- a/internal/project/snapshot_test.go +++ b/internal/project/snapshot_test.go @@ -101,4 +101,23 @@ func TestSnapshot(t *testing.T) { assert.Check(t, snapshotAfter.fs.diskFiles["/home/projects/ts/p1/a.ts"] == nil) assert.Check(t, snapshotAfter.fs.diskFiles["/home/projects/ts/p2/b.ts"] != nil) }) + + t.Run("GetFile returns nil for non-existent files", func(t *testing.T) { + t.Parallel() + files := map[string]any{ + "/home/projects/TS/p1/tsconfig.json": "{}", + "/home/projects/TS/p1/index.ts": "console.log('Hello, world!');", + } + session := setup(files) + session.DidOpenFile(context.Background(), "file:///home/projects/TS/p1/index.ts", 1, files["/home/projects/TS/p1/index.ts"].(string), lsproto.LanguageKindTypeScript) + snapshot, release := session.Snapshot() + defer release() + + handle := snapshot.GetFile("/home/projects/TS/p1/nonexistent.ts") + assert.Check(t, handle == nil, "GetFile should return nil for non-existent file") + + // Test that ReadFile returns false for non-existent file + _, ok := snapshot.ReadFile("/home/projects/TS/p1/nonexistent.ts") + assert.Check(t, !ok, "ReadFile should return false for non-existent file") + }) } diff --git a/internal/project/snapshotfs.go b/internal/project/snapshotfs.go index 1b45acf0a2..441299e318 100644 --- a/internal/project/snapshotfs.go +++ b/internal/project/snapshotfs.go @@ -30,7 +30,7 @@ type snapshotFS struct { readFiles collections.SyncMap[tspath.Path, memoizedDiskFile] } -type memoizedDiskFile func() *diskFile +type memoizedDiskFile func() FileHandle func (s *snapshotFS) FS() vfs.FS { return s.fs @@ -43,16 +43,14 @@ func (s *snapshotFS) GetFile(fileName string) FileHandle { if file, ok := s.diskFiles[s.toPath(fileName)]; ok { return file } - newEntry := memoizedDiskFile(sync.OnceValue(func() *diskFile { + newEntry := memoizedDiskFile(sync.OnceValue(func() FileHandle { if contents, ok := s.fs.ReadFile(fileName); ok { return newDiskFile(fileName, contents) } return nil })) - if entry, ok := s.readFiles.LoadOrStore(s.toPath(fileName), newEntry); ok { - return entry() - } - return nil + entry, _ := s.readFiles.LoadOrStore(s.toPath(fileName), newEntry) + return entry() } type snapshotFSBuilder struct {