Skip to content

Commit 26137be

Browse files
committed
io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir}
Fixes #64366
1 parent 0c7e5d3 commit 26137be

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

src/io/fs/readdir_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package fs_test
66

77
import (
8+
"errors"
89
. "io/fs"
910
"os"
1011
"testing"
@@ -91,3 +92,19 @@ func TestFileInfoToDirEntry(t *testing.T) {
9192
})
9293
}
9394
}
95+
96+
func TestReadDirPath(t *testing.T) {
97+
errorPath := func(err error) string {
98+
var perr *PathError
99+
if !errors.As(err, &perr) {
100+
return ""
101+
}
102+
return perr.Path
103+
}
104+
fsys := os.DirFS(t.TempDir())
105+
_, err1 := ReadDir(fsys, "non-existent")
106+
_, err2 := ReadDir(struct{ FS }{fsys}, "non-existent")
107+
if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
108+
t.Fatalf("s1: %s != s2: %s", s1, s2)
109+
}
110+
}

src/io/fs/readfile_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
package fs_test
66

77
import (
8+
"errors"
89
. "io/fs"
10+
"os"
911
"testing"
1012
"testing/fstest"
1113
"time"
@@ -57,3 +59,19 @@ func TestReadFile(t *testing.T) {
5759
t.Fatalf(`ReadFile(sub(.), "hello.txt") = %q, %v, want %q, nil`, data, err, "hello, world")
5860
}
5961
}
62+
63+
func TestReadFilePath(t *testing.T) {
64+
errorPath := func(err error) string {
65+
var perr *PathError
66+
if !errors.As(err, &perr) {
67+
return ""
68+
}
69+
return perr.Path
70+
}
71+
fsys := os.DirFS(t.TempDir())
72+
_, err1 := ReadFile(fsys, "non-existent")
73+
_, err2 := ReadFile(struct{ FS }{fsys}, "non-existent")
74+
if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
75+
t.Fatalf("s1: %s != s2: %s", s1, s2)
76+
}
77+
}

src/os/file.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,13 @@ func (dir dirFS) ReadFile(name string) ([]byte, error) {
690690
if err != nil {
691691
return nil, &PathError{Op: "readfile", Path: name, Err: err}
692692
}
693-
return ReadFile(fullname)
693+
b, err := ReadFile(fullname)
694+
if err != nil {
695+
// See comment in dirFS.Open.
696+
err.(*PathError).Path = name
697+
return nil, err
698+
}
699+
return b, nil
694700
}
695701

696702
// ReadDir reads the named directory, returning all its directory entries sorted
@@ -700,7 +706,13 @@ func (dir dirFS) ReadDir(name string) ([]DirEntry, error) {
700706
if err != nil {
701707
return nil, &PathError{Op: "readdir", Path: name, Err: err}
702708
}
703-
return ReadDir(fullname)
709+
entries, err := ReadDir(fullname)
710+
if err != nil {
711+
// See comment in dirFS.Open.
712+
err.(*PathError).Path = name
713+
return nil, err
714+
}
715+
return entries, nil
704716
}
705717

706718
func (dir dirFS) Stat(name string) (fs.FileInfo, error) {

0 commit comments

Comments
 (0)