Skip to content

Commit e92efeb

Browse files
committed
Clear pnpvfs cache on test cleanup
1 parent e4a3c4b commit e92efeb

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-16
lines changed

internal/testutil/harnessutil/harnessutil.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,12 @@ func CompileFilesEx(
212212
pnpApi := pnp.InitPnpApi(fs, currentDirectory)
213213
if pnpApi != nil {
214214
fs = pnpvfs.From(fs)
215+
t.Cleanup(func() {
216+
err := fs.(*pnpvfs.PnpFS).ClearCache()
217+
if err != nil {
218+
t.Errorf("Failed to clear PnP cache: %v", err)
219+
}
220+
})
215221
}
216222

217223
fs = NewOutputRecorderFS(fs)

internal/vfs/pnpvfs/pnpvfs.go

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ import (
1313
"github.com/microsoft/typescript-go/internal/vfs/iovfs"
1414
)
1515

16-
type pnpFS struct {
16+
type PnpFS struct {
1717
fs vfs.FS
1818
cachedZipReadersMap map[string]*zip.ReadCloser
1919
cacheReaderMutex sync.Mutex
2020
}
2121

22-
var _ vfs.FS = (*pnpFS)(nil)
22+
var _ vfs.FS = (*PnpFS)(nil)
2323

24-
func From(fs vfs.FS) *pnpFS {
25-
pnpFS := &pnpFS{
24+
func From(fs vfs.FS) *PnpFS {
25+
pnpFS := &PnpFS{
2626
fs: fs,
2727
cachedZipReadersMap: make(map[string]*zip.ReadCloser),
2828
}
2929

3030
return pnpFS
3131
}
3232

33-
func (pnpFS *pnpFS) DirectoryExists(path string) bool {
33+
func (pnpFS *PnpFS) DirectoryExists(path string) bool {
3434
path, _, _ = resolveVirtual(path)
3535

3636
if strings.HasSuffix(path, ".zip") {
@@ -42,7 +42,7 @@ func (pnpFS *pnpFS) DirectoryExists(path string) bool {
4242
return fs.DirectoryExists(formattedPath)
4343
}
4444

45-
func (pnpFS *pnpFS) FileExists(path string) bool {
45+
func (pnpFS *PnpFS) FileExists(path string) bool {
4646
path, _, _ = resolveVirtual(path)
4747

4848
if strings.HasSuffix(path, ".zip") {
@@ -53,7 +53,7 @@ func (pnpFS *pnpFS) FileExists(path string) bool {
5353
return fs.FileExists(formattedPath)
5454
}
5555

56-
func (pnpFS *pnpFS) GetAccessibleEntries(path string) vfs.Entries {
56+
func (pnpFS *PnpFS) GetAccessibleEntries(path string) vfs.Entries {
5757
path, hash, basePath := resolveVirtual(path)
5858

5959
fs, formattedPath, zipPath := getMatchingFS(pnpFS, path)
@@ -72,48 +72,48 @@ func (pnpFS *pnpFS) GetAccessibleEntries(path string) vfs.Entries {
7272
return entries
7373
}
7474

75-
func (pnpFS *pnpFS) ReadFile(path string) (contents string, ok bool) {
75+
func (pnpFS *PnpFS) ReadFile(path string) (contents string, ok bool) {
7676
path, _, _ = resolveVirtual(path)
7777

7878
fs, formattedPath, _ := getMatchingFS(pnpFS, path)
7979
return fs.ReadFile(formattedPath)
8080
}
8181

82-
func (pnpFS *pnpFS) Chtimes(path string, mtime time.Time, atime time.Time) error {
82+
func (pnpFS *PnpFS) Chtimes(path string, mtime time.Time, atime time.Time) error {
8383
path, _, _ = resolveVirtual(path)
8484

8585
fs, formattedPath, _ := getMatchingFS(pnpFS, path)
8686
return fs.Chtimes(formattedPath, mtime, atime)
8787
}
8888

89-
func (pnpFS *pnpFS) Realpath(path string) string {
89+
func (pnpFS *PnpFS) Realpath(path string) string {
9090
path, hash, basePath := resolveVirtual(path)
9191

9292
fs, formattedPath, zipPath := getMatchingFS(pnpFS, path)
9393
fullPath := zipPath + fs.Realpath(formattedPath)
9494
return makeVirtualPath(basePath, hash, fullPath)
9595
}
9696

97-
func (pnpFS *pnpFS) Remove(path string) error {
97+
func (pnpFS *PnpFS) Remove(path string) error {
9898
path, _, _ = resolveVirtual(path)
9999

100100
fs, formattedPath, _ := getMatchingFS(pnpFS, path)
101101
return fs.Remove(formattedPath)
102102
}
103103

104-
func (pnpFS *pnpFS) Stat(path string) vfs.FileInfo {
104+
func (pnpFS *PnpFS) Stat(path string) vfs.FileInfo {
105105
path, _, _ = resolveVirtual(path)
106106

107107
fs, formattedPath, _ := getMatchingFS(pnpFS, path)
108108
return fs.Stat(formattedPath)
109109
}
110110

111-
func (pnpFS *pnpFS) UseCaseSensitiveFileNames() bool {
111+
func (pnpFS *PnpFS) UseCaseSensitiveFileNames() bool {
112112
// pnp fs is always case sensitive
113113
return true
114114
}
115115

116-
func (pnpFS *pnpFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
116+
func (pnpFS *PnpFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
117117
root, hash, basePath := resolveVirtual(root)
118118

119119
fs, formattedPath, zipPath := getMatchingFS(pnpFS, root)
@@ -123,7 +123,7 @@ func (pnpFS *pnpFS) WalkDir(root string, walkFn vfs.WalkDirFunc) error {
123123
}))
124124
}
125125

126-
func (pnpFS *pnpFS) WriteFile(path string, data string, writeByteOrderMark bool) error {
126+
func (pnpFS *PnpFS) WriteFile(path string, data string, writeByteOrderMark bool) error {
127127
path, _, _ = resolveVirtual(path)
128128

129129
fs, formattedPath, zipPath := getMatchingFS(pnpFS, path)
@@ -142,7 +142,7 @@ func splitZipPath(path string) (string, string) {
142142
return parts[0] + ".zip", "/" + parts[1]
143143
}
144144

145-
func getMatchingFS(pnpFS *pnpFS, path string) (vfs.FS, string, string) {
145+
func getMatchingFS(pnpFS *PnpFS, path string) (vfs.FS, string, string) {
146146
if !tspath.IsZipPath(path) {
147147
return pnpFS.fs, path, ""
148148
}
@@ -233,3 +233,19 @@ func makeVirtualPath(basePath string, hash string, targetPath string) string {
233233

234234
return path.Join(basePath, hash, strconv.Itoa(depth), subPath)
235235
}
236+
237+
func (pnpFS *PnpFS) ClearCache() error {
238+
pnpFS.cacheReaderMutex.Lock()
239+
defer pnpFS.cacheReaderMutex.Unlock()
240+
241+
for _, reader := range pnpFS.cachedZipReadersMap {
242+
err := reader.Close()
243+
if err != nil {
244+
return err
245+
}
246+
}
247+
248+
pnpFS.cachedZipReadersMap = make(map[string]*zip.ReadCloser)
249+
250+
return nil
251+
}

0 commit comments

Comments
 (0)