Skip to content

Commit d961b12

Browse files
callthingsoffgopherbot
authored andcommitted
io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir}
Fixes #64366 Change-Id: Ie78ab2cb9e11c0766665cd37fd7a26d36a1c24fa GitHub-Last-Rev: 3cb3bb8 GitHub-Pull-Request: #64372 Reviewed-on: https://go-review.googlesource.com/c/go/+/544835 Reviewed-by: Dmitri Shuralyov <[email protected]> Run-TryBot: Jes Cok <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Bryan Mills <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Bryan Mills <[email protected]>
1 parent 0d018b4 commit d961b12

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

src/io/fs/readdir_test.go

Lines changed: 18 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,20 @@ func TestFileInfoToDirEntry(t *testing.T) {
9192
})
9293
}
9394
}
95+
96+
func errorPath(err error) string {
97+
var perr *PathError
98+
if !errors.As(err, &perr) {
99+
return ""
100+
}
101+
return perr.Path
102+
}
103+
104+
func TestReadDirPath(t *testing.T) {
105+
fsys := os.DirFS(t.TempDir())
106+
_, err1 := ReadDir(fsys, "non-existent")
107+
_, err2 := ReadDir(struct{ FS }{fsys}, "non-existent")
108+
if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
109+
t.Fatalf("s1: %s != s2: %s", s1, s2)
110+
}
111+
}

src/io/fs/readfile_test.go

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

77
import (
88
. "io/fs"
9+
"os"
910
"testing"
1011
"testing/fstest"
1112
"time"
@@ -57,3 +58,12 @@ func TestReadFile(t *testing.T) {
5758
t.Fatalf(`ReadFile(sub(.), "hello.txt") = %q, %v, want %q, nil`, data, err, "hello, world")
5859
}
5960
}
61+
62+
func TestReadFilePath(t *testing.T) {
63+
fsys := os.DirFS(t.TempDir())
64+
_, err1 := ReadFile(fsys, "non-existent")
65+
_, err2 := ReadFile(struct{ FS }{fsys}, "non-existent")
66+
if s1, s2 := errorPath(err1), errorPath(err2); s1 != s2 {
67+
t.Fatalf("s1: %s != s2: %s", s1, s2)
68+
}
69+
}

src/os/file.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,15 @@ 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+
if e, ok := err.(*PathError); ok {
696+
// See comment in dirFS.Open.
697+
e.Path = name
698+
}
699+
return nil, err
700+
}
701+
return b, nil
694702
}
695703

696704
// ReadDir reads the named directory, returning all its directory entries sorted
@@ -700,7 +708,15 @@ func (dir dirFS) ReadDir(name string) ([]DirEntry, error) {
700708
if err != nil {
701709
return nil, &PathError{Op: "readdir", Path: name, Err: err}
702710
}
703-
return ReadDir(fullname)
711+
entries, err := ReadDir(fullname)
712+
if err != nil {
713+
if e, ok := err.(*PathError); ok {
714+
// See comment in dirFS.Open.
715+
e.Path = name
716+
}
717+
return nil, err
718+
}
719+
return entries, nil
704720
}
705721

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

0 commit comments

Comments
 (0)