Skip to content

Commit b192188

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

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

merge.go

Lines changed: 43 additions & 0 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,39 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
187188
if opts.AcceptFile == nil {
188189
opts.AcceptFile = DefaultFileAcceptor
189190
}
191+
fmt.Printf("BEFORE: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
192+
// if runtime.GOOS == "windows" {
193+
// // Go running on windows does not support os.DirFS properly
194+
// // See: https://github.com/golang/go/issues/44279
195+
// fmt.Println(" windows") //nolint:forbidigo
196+
// opts.FS = &osFilesystem{}
197+
// dir = filepath.Join(filepath.VolumeName(dir), dir)
198+
// fmt.Printf("MIDDLE2: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
199+
// }
190200
if opts.FS == nil {
201+
fmt.Println(" other") //nolint:forbidigo
191202
opts.FS = os.DirFS(dir)
192203
dir = "."
204+
fmt.Printf("MIDDLE1: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
205+
} else {
206+
var err error
207+
fmt.Printf("MIDDLE2A: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
208+
opts.FS, err = fs.Sub(opts.FS, dir)
209+
if err != nil {
210+
return nil, fmt.Errorf("fs.Sub of %v and %v failed: %w", opts.FS, dir, err)
211+
}
212+
if runtime.GOOS == "windows" {
213+
dir = ""
214+
} else {
215+
dir = "."
216+
}
217+
fmt.Printf("MIDDLE2B: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
193218
}
219+
fmt.Printf("AFTER: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
220+
221+
// dir + sub: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001\\a\\b\\c"
222+
// BEFORE: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"
223+
// AFTER: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"
194224

195225
sorted := &outFile{}
196226
var setup sync.Once
@@ -278,7 +308,20 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
278308
return convertToFiles(sorted, conditions)
279309
}
280310

311+
// osFilesystem is an io/fs.FS which defers to the os package for opening files
312+
// See: https://github.com/golang/go/issues/44279
313+
type osFilesystem struct{}
314+
315+
func (*osFilesystem) Open(name string) (fs.File, error) {
316+
fmt.Printf("osFilesystem.Open(%v)\n", name) //nolint:forbidigo
317+
return os.Open(name)
318+
}
319+
320+
var _ fs.FS = new(osFilesystem)
321+
281322
func walkDir(fsys fs.FS, dir string, discoveredPaths chan string) error {
323+
fmt.Printf("walkDir: dir=%v fsys=%#v\n", dir, fsys) //nolint:forbidigo
324+
282325
reader, ok := fsys.(fs.ReadDirFS)
283326
if !ok {
284327
return fmt.Errorf("unexpected %T wanted fs.ReadDirFS", fsys)

merge_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,9 +585,10 @@ func TestMergeDir__DefaultFileAcceptor(t *testing.T) {
585585
require.Equal(t, SkipFile, output)
586586
}
587587

588-
func TestMergeDir_SubFS(t *testing.T) {
588+
func TestMergeDir_WithFS(t *testing.T) {
589589
dir := t.TempDir()
590590
sub := filepath.Join("a", "b", "c")
591+
fmt.Printf("dir + sub: %v\n", filepath.Join(dir, sub)) //nolint:forbidigo
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)