Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
1 task done
rogpeppe opened this issue Nov 23, 2023 · 3 comments
Closed
1 task done

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

rogpeppe opened this issue Nov 23, 2023 · 3 comments
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@rogpeppe
Copy link
Contributor

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.

@raghvenders

This comment was marked as spam.

@gopherbot
Copy link

Change https://go.dev/cl/544835 mentions this issue: io/fs, os: unify PathError.Path for dirFS.{ReadFile, ReadDir}

@bcmills
Copy link
Contributor

bcmills commented Nov 27, 2023

See previously https://go.dev/cl/446115 (for #56034).

@bcmills bcmills added NeedsFix The path to resolution is known, but the work has not been done. FixPending Issues that have a fix which has not yet been reviewed or submitted. labels Nov 27, 2023
@bcmills bcmills added this to the Backlog milestone Nov 27, 2023
@dmitshur dmitshur modified the milestones: Backlog, Go1.22 Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FixPending Issues that have a fix which has not yet been reviewed or submitted. NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants