@@ -187,10 +187,38 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
187
187
if opts .AcceptFile == nil {
188
188
opts .AcceptFile = DefaultFileAcceptor
189
189
}
190
- if opts .FS == nil {
191
- opts .FS = os .DirFS (dir )
192
- dir = "."
193
- }
190
+ // fmt.Printf("BEFORE: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
191
+ // if runtime.GOOS == "windows" {
192
+ // // Go running on windows does not support os.DirFS properly
193
+ // // See: https://github.com/golang/go/issues/44279
194
+ // fmt.Println(" windows") //nolint:forbidigo
195
+ // opts.FS = &osFilesystem{}
196
+ // dir = filepath.Join(filepath.VolumeName(dir), dir)
197
+ // fmt.Printf("MIDDLE2: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
198
+ // }
199
+ // if opts.FS == nil {
200
+ // fmt.Println("opts.FS == nil") //nolint:forbidigo
201
+ // opts.FS = os.DirFS(dir)
202
+ // fmt.Printf("MIDDLE1: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
203
+ // } else {
204
+ // var err error
205
+ // fmt.Printf("MIDDLE2A: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
206
+ // opts.FS, err = fs.Sub(opts.FS, dir)
207
+ // if err != nil {
208
+ // return nil, fmt.Errorf("fs.Sub of %v and %v failed: %w", opts.FS, dir, err)
209
+ // }
210
+ // // if runtime.GOOS == "windows" {
211
+ // // dir = ""
212
+ // // } else {
213
+ // // dir = "."
214
+ // // }
215
+ // fmt.Printf("MIDDLE2B: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
216
+ // }
217
+ // fmt.Printf("AFTER: dir=%v opts.FS=%#v\n", dir, opts.FS) //nolint:forbidigo
218
+
219
+ // dir + sub: "C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001\\a\\b\\c"
220
+ // BEFORE: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"
221
+ // AFTER: dir="a\\b\\c" opts.FS="C:\\Users\\RUNNER~1\\AppData\\Local\\Temp\\TestMergeDir_SubFS959700692\\001"
194
222
195
223
sorted := & outFile {}
196
224
var setup sync.Once
@@ -278,15 +306,39 @@ func MergeDir(dir string, conditions Conditions, opts *MergeDirOptions) ([]*File
278
306
return convertToFiles (sorted , conditions )
279
307
}
280
308
309
+ // osFilesystem is an io/fs.FS which defers to the os package for opening files
310
+ // See: https://github.com/golang/go/issues/44279
311
+ type osFilesystem struct {}
312
+
313
+ func (* osFilesystem ) Open (name string ) (fs.File , error ) {
314
+ fmt .Printf ("osFilesystem.Open(%v)\n " , name ) //nolint:forbidigo
315
+ return os .Open (name )
316
+ }
317
+
318
+ var _ fs.FS = new (osFilesystem )
319
+
281
320
func walkDir (fsys fs.FS , dir string , discoveredPaths chan string ) error {
282
- reader , ok := fsys .(fs.ReadDirFS )
283
- if ! ok {
284
- return fmt .Errorf ("unexpected %T wanted fs.ReadDirFS" , fsys )
285
- }
321
+ fmt .Printf ("walkDir: dir=%v fsys=%#v\n " , dir , fsys ) //nolint:forbidigo
286
322
287
- items , err := reader .ReadDir (dir )
323
+ var items []fs.DirEntry
324
+ var err error
325
+
326
+ if fsys != nil {
327
+ fmt .Printf ("walkDir: A\n " ) //nolint:forbidigo
328
+ if rr , ok := fsys .(fs.ReadDirFS ); ok {
329
+ fmt .Printf ("walkDir: B\n " ) //nolint:forbidigo
330
+ items , err = rr .ReadDir (dir )
331
+ }
332
+ }
333
+ if err != nil {
334
+ return fmt .Errorf ("fs.readdir %s failed: %w" , dir , err )
335
+ }
336
+ if len (items ) == 0 {
337
+ fmt .Printf ("walkDir: C\n " ) //nolint:forbidigo
338
+ items , err = os .ReadDir (dir )
339
+ }
288
340
if err != nil {
289
- return fmt .Errorf ("listing %s failed: %w" , dir , err )
341
+ return fmt .Errorf ("os.readdir %s failed: %w" , dir , err )
290
342
}
291
343
292
344
for i := range items {
@@ -356,7 +408,14 @@ func readValidateOptsFromFile(path string, opts *MergeDirOptions) *ValidateOpts
356
408
if opts .ValidateOptsExtension != "" {
357
409
where := strings .TrimSuffix (path , filepath .Ext (path )) + opts .ValidateOptsExtension
358
410
359
- fd , err := opts .FS .Open (where )
411
+ var fd fs.File
412
+ var err error
413
+
414
+ if opts .FS != nil {
415
+ fd , err = opts .FS .Open (where )
416
+ } else {
417
+ fd , err = os .Open (where )
418
+ }
360
419
if err != nil {
361
420
return nil
362
421
}
@@ -369,12 +428,18 @@ func readValidateOptsFromFile(path string, opts *MergeDirOptions) *ValidateOpts
369
428
return nil
370
429
}
371
430
372
- func readFile (fs fs.FS , path string , as FileAcceptance , validateOpts * ValidateOpts ) (* File , error ) {
431
+ func readFile (fsys fs.FS , path string , as FileAcceptance , validateOpts * ValidateOpts ) (* File , error ) {
373
432
if as == SkipFile {
374
433
return nil , nil
375
434
}
376
435
377
- fd , err := fs .Open (path )
436
+ var fd fs.File
437
+ var err error
438
+ if fsys != nil {
439
+ fd , err = fsys .Open (path )
440
+ } else {
441
+ fd , err = os .Open (path )
442
+ }
378
443
if err != nil {
379
444
return nil , fmt .Errorf ("opening %s failed: %w" , path , err )
380
445
}
0 commit comments