From daa85d17c25d550f40661d0fd96de0513a87f3bb Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 6 Sep 2024 10:42:36 +0900 Subject: [PATCH] internal/file: bug fix: VirtualFS.Open(".") should always return a new entry Closes #3081 --- internal/file/file_notjs.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/internal/file/file_notjs.go b/internal/file/file_notjs.go index d6f5a8b12..f9428b85b 100644 --- a/internal/file/file_notjs.go +++ b/internal/file/file_notjs.go @@ -29,15 +29,22 @@ import ( ) type VirtualFS struct { - root virtualFSRoot + paths []string } func NewVirtualFS(paths []string) *VirtualFS { fs := &VirtualFS{} - fs.root.addRealPaths(paths) + fs.paths = make([]string, len(paths)) + copy(fs.paths, paths) return fs } +func (v *VirtualFS) newRootFS() *virtualFSRoot { + var root virtualFSRoot + root.addRealPaths(v.paths) + return &root +} + func (v *VirtualFS) Open(name string) (fs.File, error) { if !fs.ValidPath(name) { return nil, &fs.PathError{ @@ -48,15 +55,16 @@ func (v *VirtualFS) Open(name string) (fs.File, error) { } if name == "." { - return &v.root, nil + return v.newRootFS(), nil } // A valid path must not include a token "." or "..", except for "." itself. es := strings.Split(name, "/") - for _, realPath := range v.root.realPaths { + for _, realPath := range v.paths { if filepath.Base(realPath) != es[0] { continue } + // TODO: Does this work on Windows? return os.Open(filepath.Join(append([]string{realPath}, es[1:]...)...)) }