Skip to content

Commit 0eb4851

Browse files
committed
fix: defer to os.Open for windows
See: golang/go#44279
1 parent f5efa42 commit 0eb4851

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

merge.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"io/fs"
2626
"os"
2727
"path/filepath"
28+
"runtime"
2829
"strings"
2930
"sync"
3031

@@ -187,10 +188,24 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
187188
if opts.AcceptFile == nil {
188189
opts.AcceptFile = DefaultFileAcceptor
189190
}
191+
fmt.Printf("BEFORE: dir=%q opts.FS=%#v\n", dir, opts.FS)
190192
if opts.FS == nil {
191-
opts.FS = os.DirFS(dir)
192-
dir = "."
193+
// Go running on windows does not support os.DirFS properly
194+
// See: https://github.com/golang/go/issues/44279
195+
if runtime.GOOS == "windows" {
196+
fmt.Println(" windows")
197+
opts.FS = &osFilesystem{}
198+
} else {
199+
fmt.Println(" other")
200+
opts.FS = os.DirFS(dir)
201+
dir = "."
202+
}
203+
fmt.Printf("MIDDLE1: dir=%q opts.FS=%#v\n", dir, opts.FS)
204+
}
205+
if runtime.GOOS == "windows" {
206+
dir = filepath.Join(filepath.VolumeName(dir), dir)
193207
}
208+
fmt.Printf("AFTER: dir=%q opts.FS=%#v\n", dir, opts.FS)
194209

195210
sorted := &outFile{}
196211
var setup sync.Once
@@ -278,6 +293,16 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
278293
return convertToFiles(sorted, conditions)
279294
}
280295

296+
// osFilesystem is an io/fs.FS which defers to the os package for opening files
297+
// See: https://github.com/golang/go/issues/44279
298+
type osFilesystem struct{}
299+
300+
func (*osFilesystem) Open(name string) (fs.File, error) {
301+
return os.Open(name)
302+
}
303+
304+
var _ fs.FS = new(osFilesystem)
305+
281306
func walkDir(fsys fs.FS, dir string, discoveredPaths chan string) error {
282307
reader, ok := fsys.(fs.ReadDirFS)
283308
if !ok {

merge_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,7 @@ func TestMergeDir__DefaultFileAcceptor(t *testing.T) {
588588
func TestMergeDir_SubFS(t *testing.T) {
589589
dir := t.TempDir()
590590
sub := filepath.Join("a", "b", "c")
591+
fmt.Printf("dir + sub: %q\n", filepath.Join(dir, sub))
591592
require.NoError(t, os.MkdirAll(filepath.Join(dir, sub), 0777))
592593

593594
src, err := os.Open(filepath.Join("test", "testdata", "ppd-debit.ach"))

0 commit comments

Comments
 (0)