diff options
| author | Ian Lance Taylor <iant@golang.org> | 2022-10-03 20:08:11 -0700 |
|---|---|---|
| committer | Gopher Robot <gobot@golang.org> | 2022-10-06 02:55:59 +0000 |
| commit | 755a2927d8f5bb79952db8fd17bbdec1aed91518 (patch) | |
| tree | c9d085c58282c3b66018a8dd03dfd50fe6b605f6 /src | |
| parent | c433cf189354f0b3a5e50716df57a94af5f22718 (diff) | |
| download | go-755a2927d8f5bb79952db8fd17bbdec1aed91518.tar.xz | |
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 <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@google.com>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@google.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/os/file.go | 7 | ||||
| -rw-r--r-- | src/os/os_test.go | 16 |
2 files changed, 21 insertions, 2 deletions
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`) } |
