Skip to content

os: dirFS.ReadFile returns inconsistent PathError.Path #64366

@rogpeppe

Description

@rogpeppe

Go version

go version devel go1.22-3188758653 Fri Nov 10 21:25:30 2023 +0000 linux/amd64

Reproducibility

  • Does this issue reproduce with the latest release?

What operating system and processor architecture are you using (go env)?

GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/home/rogpeppe/.cache/go-build'
GOENV='/home/rogpeppe/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/home/rogpeppe/src/go/pkg/mod'
GONOPROXY='github.com/cue-unity'
GONOSUMDB='github.com/cue-unity'
GOOS='linux'
GOPATH='/home/rogpeppe/src/go'
GOPRIVATE='github.com/cue-unity'
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/home/rogpeppe/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/home/rogpeppe/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='devel go1.22-3188758653 Fri Nov 10 21:25:30 2023 +0000'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/home/rogpeppe/go/src/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build683023569=/tmp/go-build -gno-record-gcc-switches'

What did you do?

https://go.dev/play/p/5vKYCOeV5gd

package main

import (
	"errors"
	"fmt"
	"io/fs"
	"os"
)

func main() {
	fsys := os.DirFS("/")
	_, err := fs.ReadFile(fsys, "non-existent")
	fmt.Printf("ReadFile path %q\n", errorPath(err))
	_, err = fs.ReadFile(struct{ fs.FS }{fsys}, "non-existent")
	fmt.Printf("ReadFile without shortcut: path %q\n", errorPath(err))
}

func errorPath(err error) string {
	var perr *fs.PathError
	if !errors.As(err, &perr) {
		return ""
	}
	return perr.Path
}

What did you expect to see?

ReadFile path "non-existent"
ReadFile without shortcut: path "non-existent"

What did you see instead?

ReadFile path "/non-existent"
ReadFile without shortcut: path "non-existent"

The PathError returned from fs.ReadFile is reflecting the path of the underlying os implementation rather than the fs.FS name. It should be the same regardless of whether the FS implementation implements ReadFile or not.

I think the dirFS.ReadFile method should probably inspect the returned error and transform paths when it's a PathError.

Also, the io/fs documentation could be clearer about the requirements for error returns throughout the API.

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixPendingIssues that have a fix which has not yet been reviewed or submitted.FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions