Skip to content

path.Base and path.Split do not properly split the file name on Windows as documented #49745

@heaths

Description

@heaths

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

go version go1.17.3 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
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\heath\AppData\Local\go-build
set GOENV=C:\Users\heath\AppData\Roaming\go\env
set GOEXE=.exe
set GOEXPERIMENT=
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\heath\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\heath\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Program Files\Go
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Program Files\Go\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.17.3
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=NUL
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\heath\AppData\Local\Temp\go-build4257983573=/tmp/go-build -gno-record-gcc-switches

What did you do?

Using path.Base() and path.Split() on Windows, I found they do not return just the file name (or directory and filename separated) as documented. They work as documented on WSL2 (Ubunto 18.04, however).

package main

import (
    "fmt"
    "os"
    "path"
)

func main() {
    fmt.Println("base =", path.Base(os.Args[0]))

    dir, file := path.Split(os.Args[0])
    fmt.Printf("dir = %q, file = %q\n", dir, file)
}

What did you expect to see?

I expected the file name itself to be returned from path.Base, as well as the directory and file name in dir and file parameters from path.Split.

What did you see instead?

On Windows, the entire path was found in place of the file as shown in the output from the sample program below:

base = C:\Users\heath\AppData\Local\Temp\go-build2408382784\b001\exe\main.exe
dir = "", file = "C:\\Users\\heath\\AppData\\Local\\Temp\\go-build2408382784\\b001\\exe\\main.exe"

On Ubuntu the content below is correct according to documentation and is what I expected:

base = main
dir = "/tmp/go-build2295857948/b001/exe/", file = "main"

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions