go: os: ReadDir on regular file returns ERROR_PATH_NOT_FOUND instead of ENOTDIR

What version of Go are you using (go version)?

$ go version
go version go1.16.5 windows/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\moo\AppData\Local\go-build
set GOENV=C:\Users\moo\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\moo\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\moo\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\moo\.gimme\versions\go1.16.5.windows.amd64
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\moo\.gimme\versions\go1.16.5.windows.amd64\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.5
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=C:\Users\moo\oss\getenvoy\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=C:\Users\moo\AppData\Local\Temp\go-build3391750208=/tmp/go-build -gno-record-gcc-switches

What did you do?

	files, err := os.ReadDir(filePath)

What did you expect to see?

err should be ENOTDIR

What did you see instead?

os.IsNotExist(err) matches because it was ERROR_PATH_NOT_FOUND

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

Here’s the unit test, and on windows GitHub actions runner, it fails like this, but only on windows (doesn’t fail on linux or macOS). Feel free too do whatever you want, just I owed a unit test from earlier. If you think this is ok, just close the issue.

--- FAIL: TestDirError (0.00s)
    dir_test.go:25: didn't want os.IsNotExist() on os.ReadDir(C:\Users\RUNNER~1\AppData\Local\Temp\TestDirError275160222\001/foo)
func TestDirError(t *testing.T) {
	dir := t.TempDir()
	fileNotDir := path.Join(dir, "foo")
	if os.WriteFile(fileNotDir, []byte{}, 0600) != nil {
		t.Fatalf("cannot write %s", fileNotDir)
	}
	_, err := os.ReadDir(fileNotDir)
	if err == nil {
		t.Fatalf("wanted an error on os.ReadDir(%s)", fileNotDir)
	}
	pErr, ok := err.(*os.PathError)
	if !ok {
		t.Fatalf("wanted an os.PathError on os.ReadDir(%s)", fileNotDir)
	}
	if os.IsNotExist(err) {
		t.Fatalf("didn't want os.IsNotExist() on os.ReadDir(%s)", fileNotDir)
	}
	if pErr.Err != syscall.ENOTDIR {
		t.Fatalf("want syscall.ENOTDIR, have %s, on os.ReadDir(%s)", pErr.Err, fileNotDir)
	}
}

To put it concretely, I believe it is an experience fail to wrap errors only on windows for the same operation. Consistency is important and reduces time loss. It is true someone can say the user is wrong, but there is probably also some value in being direct and consistent in core APIs including error depth. I think this is the key issue and if someone else also thinks through this and decides it is better to close it anyway, go for it, just I would like consistency to be considered. that alone could make the efforts here worth something above zero.

Using == for type error is not recommended. Using something like below provides details on what is happening:

// Testing ENOTDIR first provides the detailed cause of the failure.
if !errors.Is(pErr.Err, syscall.ENOTDIR) {
		t.Fatalf("want syscall.ENOTDIR, have %s, on os.ReadDir(%s)", pErr.Err, fileNotDir)
}
if os.IsNotExist(err) {
		t.Fatalf("didn't want os.IsNotExist() on os.ReadDir(%s)", fileNotDir)
}

IsNotExist(err) reports true as the directory is not found which is ERROR_PATH_NOT_FOUND on windows. It seems that issue can be closed.

What did you do?

	files, err := os.ReadDir(filePath)

That is not enough to reproduce this issue.

@codefromthecrypt can you, please, provide complete program with steps to reproduce?

Thank you.

Alex