Skip to content
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

path.IsAbs() wrong in Windows #42995

Closed
axetroy opened this issue Dec 4, 2020 · 5 comments
Closed

path.IsAbs() wrong in Windows #42995

axetroy opened this issue Dec 4, 2020 · 5 comments

Comments

@axetroy
Copy link

axetroy commented Dec 4, 2020

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

$ go version
go version go1.15.5 windows/amd64

Does this issue reproduce with the latest release?

package main

import (
	"regexp"
	"path"
)

var (
	winAbsolutePathRegegp = regexp.MustCompile(`^(?i)[a-z]:.+`)
)

func main() {
	var s = "D:\\a\\whatchanged\\whatchanged"
	
	println(winAbsolutePathRegegp.MatchString(s))
	
	println(path.IsAbs(s))
}

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\runneradmin\AppData\Local\go-build
set GOENV=C:\Users\runneradmin\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\runneradmin\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\runneradmin\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\hostedtoolcache\windows\go\1.15.5\x64
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\hostedtoolcache\windows\go\1.15.5\x64\pkg\tool\windows_amd64
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\a\whatchanged\whatchanged\go.mod
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\RUNNER~1\AppData\Local\Temp\go-build717683694=/tmp/go-build -gno-record-gcc-switches

What did you do?

Determine whether a windows path is an absolute path

What did you expect to see?

path.IsAbs("D:\\a\\whatchanged\\whatchanged") // should be true

What did you see instead?

path.IsAbs("D:\\a\\whatchanged\\whatchanged") // but got false

here is the CI:

https://github.com/axetroy/whatchanged/runs/1497192324

here is the source code:

https://github.com/axetroy/whatchanged/blob/5050ea45deb73d861df1b2e62f945502a2c78c7d/internal/client/git.go#L41-L43

@mattn
Copy link
Member

mattn commented Dec 4, 2020

Please use path/filepath.Abs.

@axetroy
Copy link
Author

axetroy commented Dec 4, 2020

Please use path/filepath.Abs.

ohoh.

Is there any difference between these two?

I checked the documents of these two packages, and his instructions are the same

IsAbs reports whether the path is absolute.
IsAbs reports whether the path is absolute.

@axetroy
Copy link
Author

axetroy commented Dec 4, 2020

And it doesn’t seem to not handle windows style

// IsAbs reports whether the path is absolute.
func IsAbs(path string) bool {
	return strings.HasPrefix(path, "/")
}
// IsAbs reports whether the path is absolute.
func IsAbs(path string) bool {
	return len(path) > 0 && path[0] == '/'
}

@mattn
Copy link
Member

mattn commented Dec 4, 2020

Please check path_windows.go

// IsAbs reports whether the path is absolute.
func IsAbs(path string) (b bool) {
	if isReservedName(path) {
		return true
	}
	l := volumeNameLen(path)
	if l == 0 {
		return false
	}
	path = path[l:]
	if path == "" {
		return false
	}
	return isSlash(path[0])
}

If this is not a question, please close this. Thank you.

@axetroy
Copy link
Author

axetroy commented Dec 4, 2020

ok. my bad.
Thank you for your patience and have a good day. 👍

@axetroy axetroy closed this as completed Dec 4, 2020
@golang golang locked and limited conversation to collaborators Dec 4, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants