From: Ian Lance Taylor Date: Tue, 4 Oct 2022 03:08:11 +0000 (-0700) Subject: os: if dirFS.Open fails, undo use of backslashes in error message X-Git-Tag: go1.20rc1~722 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=755a2927d8f5bb79952db8fd17bbdec1aed91518;p=gostls13.git os: if dirFS.Open fails, undo use of backslashes in error message This fixes a bug introduced by CL 426094 that caused the golang.org/x/website/internal/web tests to fail. Fixes #56034 Change-Id: Ic64967c6d440ad260b7283a18972b20023320ab6 Reviewed-on: https://go-review.googlesource.com/c/go/+/437976 Auto-Submit: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Bryan Mills TryBot-Result: Gopher Robot Reviewed-by: Ian Lance Taylor --- diff --git a/src/os/file.go b/src/os/file.go index 78677c2f8f..e8d2de1b3c 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -644,6 +644,13 @@ 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 } return f, nil diff --git a/src/os/os_test.go b/src/os/os_test.go index 4c64afaef0..ff74598362 100644 --- a/src/os/os_test.go +++ b/src/os/os_test.go @@ -2712,13 +2712,25 @@ func TestDirFS(t *testing.T) { t.Fatal(err) } } - if err := fstest.TestFS(DirFS("./testdata/dirfs"), "a", "b", "dir/x"); err != nil { + fs := DirFS("./testdata/dirfs") + if err := fstest.TestFS(fs, "a", "b", "dir/x"); err != nil { t.Fatal(err) } + // Test that the error message does not contain a backslash. + const nonesuch = "dir/nonesuch" + _, err := fs.Open(nonesuch) + if err == nil { + t.Error("fs.Open of nonexistent file succeeded") + } else { + if !strings.Contains(err.Error(), nonesuch) { + t.Errorf("error %q does not contain %q", err, nonesuch) + } + } + // Test that Open does not accept backslash as separator. d := DirFS(".") - _, err := d.Open(`testdata\dirfs`) + _, err = d.Open(`testdata\dirfs`) if err == nil { t.Fatalf(`Open testdata\dirfs succeeded`) }