Description
Go version
1.22.4 windows/amd64
Output of go env
in your module/workspace:
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Hakkin\AppData\Local\go-build
set GOENV=C:\Users\Hakkin\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Hakkin\Desktop\Programs\Misc\Projects\Go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Hakkin\Desktop\Programs\Misc\Projects\Go
set GOPRIVATE=
set GOPROXY=
set GOROOT=C:\Program Files\Go
set GOSUMDB=
set GOTMPDIR=
set GOTOOLCHAIN=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.22.4
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
set GOWORK=
set CGO_CFLAGS=-O2 -g
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-O2 -g
set CGO_FFLAGS=-O2 -g
set CGO_LDFLAGS=-O2 -g
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Hakkin\AppData\Local\Temp\go-build2088173783=/tmp/go-build -gno-record-gcc-switches
What did you do?
Attempted to list a device path (\\.\C:
) in Go 1.22 on Windows.
Code, must be ran under admin:
package main
import (
"log"
"os"
)
func main() {
device_path := `\\.\C:`
f, err := os.Open(device_path)
if err != nil {
log.Fatalf("error opening path: %s", err)
}
entries, err := f.ReadDir(100)
if err != nil {
log.Fatalf("error reading directory: %s", err)
}
log.Printf("found %d entries in directory", len(entries))
}
Note this also happens for other device paths, not just \\.\C:
. Other common device paths are things like \\?\Volume{...}
, \\?\GLOBALROOT\Device\HarddiskVolumeShadowCopy...
, etc. This issue was encountered at kopia/kopia#3842 when dealing with Windows Shadow Copy Volumes in 1.22.
What did you see happen?
# go version
go version go1.22.4 windows/amd64
# go run main.go
2024/06/04 18:18:10 error reading directory: GetFileInformationByHandleEx \\.\C:: The parameter is incorrect.
What did you expect to see?
This code works as expected in 1.21.10, thus it seems to be a regression.
# go version
go version go1.21.10 windows/amd64
# go run main.go
2024/06/04 18:22:09 found 22 entries in directory
In 1.22, you can restore the previous behavior by appending a backslash \
to the path:
...
device_path := `\\.\C:\` // <- Trailing backslash
...
# go version
go version go1.22.4 windows/amd64
# go run main.go
2024/06/04 18:49:36 found 22 entries in directory
but these kinds of paths with trailing backslashes are mangled by functions like filepath.Clean
and have the backslash stripped. filepath.Clean
has exceptions for root windows paths like C:\
, but these exceptions aren't applied to device paths.