Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions internal/project/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
}
10 changes: 4 additions & 6 deletions internal/project/snapshotfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down