Skip to content

Commit b09de27

Browse files
committed
Dont add "/" to path when walking windows os
1 parent d8ca80e commit b09de27

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

internal/vfs/internal/internal.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ func (vfs *Common) WalkDir(root string, walkFn fs.WalkDirFunc) error {
132132
if path == "." {
133133
path = ""
134134
}
135-
return walkFn(rootName+path, d, err)
135+
walkPath := rootName + path
136+
if rest, ok := strings.CutPrefix(walkPath, "/"); ok && tspath.PathIsAbsolute(rest) {
137+
walkPath = rest
138+
}
139+
return walkFn(walkPath, d, err)
136140
})
137141
}
138142

internal/vfs/vfstest/vfstest_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,20 @@ func TestFromMap(t *testing.T) {
332332
content, ok = fs.ReadFile("/mapfile")
333333
assert.Assert(t, ok)
334334
assert.Equal(t, content, "hello, world")
335+
336+
var files []string
337+
err := fs.WalkDir("/", func(path string, d vfs.DirEntry, err error) error {
338+
if err != nil {
339+
return err
340+
}
341+
if !d.IsDir() {
342+
files = append(files, path)
343+
}
344+
return nil
345+
})
346+
assert.NilError(t, err)
347+
slices.Sort(files)
348+
assert.DeepEqual(t, files, []string{"/bytes", "/mapfile", "/string"})
335349
})
336350

337351
t.Run("Windows", func(t *testing.T) {
@@ -356,6 +370,20 @@ func TestFromMap(t *testing.T) {
356370
content, ok = fs.ReadFile("e:/mapfile")
357371
assert.Assert(t, ok)
358372
assert.Equal(t, content, "hello, world")
373+
374+
var files []string
375+
err := fs.WalkDir("/", func(path string, d vfs.DirEntry, err error) error {
376+
if err != nil {
377+
return err
378+
}
379+
if !d.IsDir() {
380+
files = append(files, path)
381+
}
382+
return nil
383+
})
384+
assert.NilError(t, err)
385+
slices.Sort(files)
386+
assert.DeepEqual(t, files, []string{"c:/string", "d:/bytes", "e:/mapfile"})
359387
})
360388

361389
t.Run("Mixed", func(t *testing.T) {

0 commit comments

Comments
 (0)