aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIan Lance Taylor <iant@golang.org>2022-10-27 17:29:51 -0700
committerGopher Robot <gobot@golang.org>2022-10-28 19:25:47 +0000
commit42f334d677d5d50ba7ddca08a30451b33e78e61b (patch)
tree996f41034f47ebdbd117d11400403f8482b5817f /src
parente59d873ff906550ace73b86bdb74b68ebe482a10 (diff)
downloadgo-42f334d677d5d50ba7ddca08a30451b33e78e61b.tar.xz
os: don't include DirFS argument in DirFS errors
Otherwise we wind up mixing GOOS paths with slash separated paths. Change-Id: I63dd733cbdb0668effbc030cfd58945008732d9e Reviewed-on: https://go-review.googlesource.com/c/go/+/446115 Reviewed-by: Ian Lance Taylor <iant@google.com> Run-TryBot: Ian Lance Taylor <iant@google.com> Reviewed-by: Heschi Kreinick <heschi@google.com> Reviewed-by: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Auto-Submit: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src')
-rw-r--r--src/os/file.go17
-rw-r--r--src/os/os_test.go6
2 files changed, 14 insertions, 9 deletions
diff --git a/src/os/file.go b/src/os/file.go
index e8d2de1b3c..c46c9030b9 100644
--- a/src/os/file.go
+++ b/src/os/file.go
@@ -625,6 +625,7 @@ func DirFS(dir string) fs.FS {
return dirFS(dir)
}
+// containsAny reports whether any bytes in chars are within s.
func containsAny(s, chars string) bool {
for i := 0; i < len(s); i++ {
for j := 0; j < len(chars); j++ {
@@ -644,14 +645,12 @@ func (dir dirFS) Open(name string) (fs.File, error) {
}
f, err := Open(dir.join(name))
if err != nil {
- if runtime.GOOS == "windows" {
- // Undo the backslash conversion done by dir.join.
- perr := err.(*PathError)
- if containsAny(perr.Path, `\`) {
- perr.Path = string(dir) + "/" + name
- }
- }
- return nil, err // nil fs.File
+ // DirFS takes a string appropriate for GOOS,
+ // while the name argument here is always slash separated.
+ // dir.join will have mixed the two; undo that for
+ // error reporting.
+ err.(*PathError).Path = name
+ return nil, err
}
return f, nil
}
@@ -662,6 +661,8 @@ func (dir dirFS) Stat(name string) (fs.FileInfo, error) {
}
f, err := Stat(dir.join(name))
if err != nil {
+ // See comment in dirFS.Open.
+ err.(*PathError).Path = name
return nil, err
}
return f, nil
diff --git a/src/os/os_test.go b/src/os/os_test.go
index 550b7db5a3..52fac4f63c 100644
--- a/src/os/os_test.go
+++ b/src/os/os_test.go
@@ -2717,7 +2717,8 @@ func TestDirFS(t *testing.T) {
t.Fatal(err)
}
- // Test that the error message does not contain a backslash.
+ // Test that the error message does not contain a backslash,
+ // and does not contain the DirFS argument.
const nonesuch = "dir/nonesuch"
_, err := fs.Open(nonesuch)
if err == nil {
@@ -2726,6 +2727,9 @@ func TestDirFS(t *testing.T) {
if !strings.Contains(err.Error(), nonesuch) {
t.Errorf("error %q does not contain %q", err, nonesuch)
}
+ if strings.Contains(err.Error(), "testdata") {
+ t.Errorf("error %q contains %q", err, "testdata")
+ }
}
// Test that Open does not accept backslash as separator.