]> Cypherpunks repositories - gostls13.git/commitdiff
os: if dirFS.Open fails, undo use of backslashes in error message
authorIan Lance Taylor <iant@golang.org>
Tue, 4 Oct 2022 03:08:11 +0000 (20:08 -0700)
committerGopher Robot <gobot@golang.org>
Thu, 6 Oct 2022 02:55:59 +0000 (02:55 +0000)
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>
src/os/file.go
src/os/os_test.go

index 78677c2f8fdfd88cec1cd55c1780d1b9a8162879..e8d2de1b3cb5cf92ace4039922f3d8a98d92bb37 100644 (file)
@@ -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
index 4c64afaef06081814b17eef96e28b7b39fa9404e..ff745983620274655b4762308ccff93d02b5a84c 100644 (file)
@@ -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`)
        }