Skip to content

os: ReadDir on regular file returns non nil slice on Windows #75157

@stevenh

Description

@stevenh

Go version

go version go1.25.0 windows/amd64

Output of go env in your module/workspace:

AR=ar
CC=gcc
CGO_CFLAGS=-O2 -g
CGO_CPPFLAGS=
CGO_CXXFLAGS=-O2 -g
CGO_ENABLED=1
CGO_FFLAGS=-O2 -g
CGO_LDFLAGS=-O2 -g
CXX=g++
GCCGO=gccgo
GO111MODULE=
GOAMD64=v1
GOARCH=amd64
GOAUTH=netrc
GOBIN=
GOCACHE=C:\Users\runneradmin\AppData\Local\go-build
GOCACHEPROG=
GODEBUG=
GOENV=C:\Users\runneradmin\AppData\Roaming\go\env
GOEXE=.exe
GOEXPERIMENT=
GOFIPS140=off
GOFLAGS=
GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\RUNNER~1\AppData\Local\Temp\go-build2877138821=/tmp/go-build -gno-record-gcc-switches
GOHOSTARCH=amd64
GOHOSTOS=windows
GOINSECURE=
GOMOD=D:\a\tracktools\tracktools\go.mod
GOMODCACHE=C:\Users\runneradmin\go\pkg\mod
GONOPROXY=
GONOSUMDB=
GOOS=windows
GOPATH=C:\Users\runneradmin\go
GOPRIVATE=
GOPROXY=https://proxy.golang.org,direct
GOROOT=C:\hostedtoolcache\windows\go\1.25.0\x64
GOSUMDB=sum.golang.org
GOTELEMETRY=local
GOTELEMETRYDIR=C:\Users\runneradmin\AppData\Roaming\go\telemetry
GOTMPDIR=
GOTOOLCHAIN=auto
GOTOOLDIR=C:\hostedtoolcache\windows\go\1.25.0\x64\pkg\tool\windows_amd64
GOVCS=
GOVERSION=go1.25.0
GOWORK=
PKG_CONFIG=pkg-config

What did you do?

Call os.ReadDir on a file entry, Linux and Mac both return nil, syscall.ENOTDIR but Windows returns []fs.DirEntry{}, syscall.ENOTDIR.

In previous versions of go (v1.22) all platforms returned []fs.DirEntry{}, syscall.ENOTDIR

package main

import (
	"io/fs"
	"os"
	"syscall"
	"testing"
	"time"

	"github.com/stretchr/testify/require"
)

func Test_ReadDir(t *testing.T) {
	tf, err := os.CreateTemp(t.TempDir(), "os-test")
	require.NoError(t, err)

	name := tf.Name()
	require.NoError(t, tf.Close())

	dirs, err := os.ReadDir(name)
	require.ErrorIs(t, err, syscall.ENOTDIR)
	var expectedDirs []fs.DirEntry
	require.Equal(t, expectedDirs, dirs)
}

Result is:

    osfs_test.go:23: 
        	Error Trace:	osfs_test.go:23
        	Error:      	Not equal: 
        	            	expected: []fs.DirEntry(nil)
        	            	actual  : []fs.DirEntry{}
        	            	
        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1,2 +1,3 @@
        	            	-([]fs.DirEntry) <nil>
        	            	+([]fs.DirEntry) {
        	            	+}
        	            	 
        	Test:       	Test_ReadDir

What did you see happen?

Windows returned []fs.DirEntry{}, syscall.ENOTDIR

What did you expect to see?

Consistent behaviour between all operating systems, Linux and Mac both return nil, syscall.ENOTDIR but Windows returns []fs.DirEntry{}, syscall.ENOTDIR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.NeedsFixThe path to resolution is known, but the work has not been done.OS-Windows

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions