golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
122.88k stars 17.52k forks source link

path/filepath: Rel() fails on Windows when only one parameter has `\\?\` prefix #68847

Open dmcardle opened 1 month ago

dmcardle commented 1 month ago

Go version

go version devel go1.24-47cdc43289 Mon Aug 12 13:09:41 2024 -0400 windows/amd64

Output of go env in your module/workspace:

PS C:\Users\Dan\REPOS\go\src> ..\bin\go.exe env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Dan\AppData\Local\go-build
set GOENV=C:\Users\Dan\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\Dan\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\Dan\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\Dan\REPOS\go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLCHAIN=auto
set GOTOOLDIR=C:\Users\Dan\REPOS\go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=devel go1.24-47cdc43289 Mon Aug 12 13:09:41 2024 -0400
set GODEBUG=
set GOTELEMETRY=local
set GOTELEMETRYDIR=C:\Users\Dan\AppData\Roaming\go\telemetry
set GCCGO=gccgo
set GOAMD64=v1
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=0
set GOMOD=C:\Users\Dan\REPOS\go\src\go.mod
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 -fno-caret-diagnostics -Qunused-arguments -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=C:\Users\Dan\AppData\Local\Temp\go-build933240645=/tmp/go-build -gno-record-gcc-switches

What did you do?

What did you see happen?

--- FAIL: TestRelWindows (0.00s)
    path_windows_test.go:734: Rel("C:\\foo", "\\\\?\\C:\\foo\\bar") = "", Rel: can't make \\?\C:\foo\bar relative to C:\foo; want "bar"
    path_windows_test.go:734: Rel("C:/foo", "//?/C:/foo/bar") = "", Rel: can't make //?/C:/foo/bar relative to C:/foo; want "bar"
    path_windows_test.go:734: Rel("\\\\?\\C:\\foo", "C:\\foo\\bar") = "", Rel: can't make C:\foo\bar relative to \\?\C:\foo; want "bar"
    path_windows_test.go:734: Rel("//?/C:/foo", "C:/foo/bar") = "", Rel: can't make C:/foo/bar relative to //?/C:/foo; want "bar"
FAIL
FAIL    path/filepath   1.568s

What did you expect to see?

The demo CL (https://go-review.googlesource.com/c/go/+/604875/3) contains several expectations, but I'll pick one. I'm particularly surprised that the relative path from basepath \\?\C:\foo to targpath \\?\C:\foo\bar is an error instead of bar.

I'm a little out of my depth, but this Microsoft documentation (https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file#win32-file-namespaces) seems to indicate that \\?\ is just a way of turning off string parsing in Windows APIs.

I think it should be possible to work around this limitation, for instance by prefixing \\?\ onto the parameters when they are already absolute paths.

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

gopherbot commented 1 month ago

Change https://go.dev/cl/604875 mentions this issue: path/filepath: Add test to demonstrate Rel() quirk on Windows

ianlancetaylor commented 1 month ago

CC @neild @golang/windows

alexbrainman commented 4 weeks ago

I agree with @dmcardle that \\?\ prefix should be ignored when comparing files in filepath.Rel.

All tests in https://go-review.googlesource.com/c/go/+/604875 should pass.

Alex