Skip to content

Commit af2a49d

Browse files
committed
Fix fs.Sub problems on Windows
1 parent 1b1a68f commit af2a49d

File tree

3 files changed

+5
-4
lines changed

3 files changed

+5
-4
lines changed

context_fs_go1.16.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func fsFile(c Context, file string, filesystem fs.FS) error {
2828

2929
fi, _ := f.Stat()
3030
if fi.IsDir() {
31-
file = filepath.Join(file, indexPage)
31+
file = filepath.ToSlash(filepath.Join(file, indexPage))
3232
f, err = filesystem.Open(file)
3333
if err != nil {
3434
return ErrNotFound

echo_fs_go1.16.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func StaticDirectoryHandler(fileSystem fs.FS, disablePathUnescaping bool) Handle
6262
}
6363

6464
// fs.FS.Open() already assumes that file names are relative to FS root path and considers name with prefix `/` as invalid
65-
name := filepath.Clean(strings.TrimPrefix(p, "/"))
65+
name := filepath.ToSlash(filepath.Clean(strings.TrimPrefix(p, "/")))
6666
fi, err := fs.Stat(fileSystem, name)
6767
if err != nil {
6868
return ErrNotFound
@@ -113,6 +113,7 @@ func (fs defaultFS) Open(name string) (fs.File, error) {
113113
}
114114

115115
func subFS(currentFs fs.FS, root string) (fs.FS, error) {
116+
root = filepath.ToSlash(filepath.Clean(root)) // note: fs.FS operates only with slashes. `ToSlash` is necessary for Windows
116117
if dFS, ok := currentFs.(*defaultFS); ok {
117118
// we need to make exception for `defaultFS` instances as it interprets root prefix differently from fs.FS to
118119
// allow cases when root is given as `../somepath` which is not valid for fs.FS
@@ -122,5 +123,5 @@ func subFS(currentFs fs.FS, root string) (fs.FS, error) {
122123
fs: os.DirFS(root),
123124
}, nil
124125
}
125-
return fs.Sub(currentFs, filepath.Clean(root))
126+
return fs.Sub(currentFs, root)
126127
}

echo_fs_go1.16_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestEcho_StaticPanic(t *testing.T) {
244244
e.Filesystem = os.DirFS("./")
245245

246246
assert.PanicsWithError(t, tc.expectError, func() {
247-
e.Static("/assets", tc.givenRoot)
247+
e.Static("../assets", tc.givenRoot)
248248
})
249249
})
250250
}

0 commit comments

Comments
 (0)