-
Notifications
You must be signed in to change notification settings - Fork 18k
path/filepath: unexpected behavior of filepath.Join on Windows #26953
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
Comments
I think this is the intended behaviour. The reason is that Edit: Yep. The source code of filepath.Join confirms this: if len(elem[0]) == 2 && elem[0][1] == ':' {
// First element is drive letter without terminating slash.
// Keep path relative to current directory on that drive.
return Clean(elem[0] + strings.Join(elem[1:], string(Separator)))
} |
@FMNSSun , thanks for looking into it. parts := []string {"C:", "","Program Files", "Microsoft Office 15", "ClientX64" } |
TL/DR: I don't have to run the snippet because the source code is clear enough to see what's going on: The problem is that
which is actually not the case with In the case of windows the code simply adds the drive letter + colon + return Clean(elem[0] + strings.Join(elem[1:], string(Separator))) should perhaps invoke Edit: Another solution might be using |
I agree to that. I modified existing test:
and I see:
So it is a bug. /cc @mattn who wrote a lot of that code. Alex |
Change https://golang.org/cl/129758 mentions this issue: |
I am missing this one: |
@B-Art |
If anyone wants to verify change https://go-review.googlesource.com/c/go/+/129758 before I submit it. Thank you. Alex |
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.10.2 windows/amd64
Does this issue reproduce with the latest release?
Yes
What operating system and processor architecture are you using (
go env
)?set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\Jerry\AppData\Local\go-build
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Work\MyProjects\workspace\go
set GORACE=
set GOROOT=C:\Go
set GOTMPDIR=
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set GCCGO=gccgo
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
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 -fmessage-length=0 -fdebug-prefix-map=C:\Users\Jerry\AppData\Local\Temp\go-build770456046=/tmp/go-build -gno-record-gcc-switches
What did you do?
If possible, provide a recipe for reproducing the error.
A complete runnable program is good.
A link on play.golang.org is best.
filepath.Join seems does not work well in Windows, please see the following snippet
package main
import (
"os"
"fmt"
"path/filepath"
)
func main() {
parts := []string {"C:", "Windows", "WinSXS" }
fmt.Println(filepath.Join(parts...))
parts = []string {fmt.Sprintf("C:%c", os.PathSeparator), "Windows", "WinSXS" }
fmt.Println(filepath.Join(parts...))
parts = []string {"C:", "","Program Files", "Microsoft Office 15", "ClientX64" }
fmt.Println(filepath.Join(parts...))
}
filepath.Join is expected to process the path separator properly, it works well for the other parts except the drive letter without the terminating slash. but if there was an empty string in the other parts, it works.
What did you expect to see?
It should output something like the following,
C:\Windows\WinSXS
C:\Windows\WinSXS
C:\Program Files\Microsoft Office 15\ClientX64
What did you see instead?
C:Windows\WinSXS
C:\Windows\WinSXS
C:\Program Files\Microsoft Office 15\ClientX64
The text was updated successfully, but these errors were encountered: