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/filepath: Glob fails if it has a wildcard and ends with / #33617

Open
BlackHole1 opened this issue Aug 13, 2019 · 12 comments
Open

path/filepath: Glob fails if it has a wildcard and ends with / #33617

BlackHole1 opened this issue Aug 13, 2019 · 12 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@BlackHole1
Copy link

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

$ go version
go version go1.12 darwin/amd64

Does this issue reproduce with the latest release?

Yes

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

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/black-hole/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/black-hole/.gvm/pkgsets/go1.12/global"
GOPROXY=""
GORACE=""
GOROOT="/Users/black-hole/.gvm/gos/go1.12"
GOTMPDIR=""
GOTOOLDIR="/Users/black-hole/.gvm/gos/go1.12/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/82/jkr30dwj24ncf6ng3tdv9kd40000gn/T/go-build925523187=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

https://play.golang.org/p/hOALcoEmrWz

What did you expect to see?

Accurate return path

What did you see instead?

Cannot match when there is a wildcard in the path and the last character is /

Other

image

@BlackHole1 BlackHole1 changed the title path/filePath: If the function Glob has a wildcard and the last string is /, it does not capture correctly. path/filePath: If the function Glob has a wildcard and the last string is /, it does not capture correctly Aug 13, 2019
@agnivade agnivade added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Aug 13, 2019
@agnivade agnivade added this to the Unplanned milestone Aug 13, 2019
@jamdagni86
Copy link
Contributor

The function first uses filepath.Split to get the parent directory of the given path. When a trailing slash is present, filepath.Split returns the path as it is as it can't split it further. The fix is to clean the path before calling filepath.Split

dir, file := Split(pattern)
volumeLen := 0
if runtime.GOOS == "windows" {
volumeLen, dir = cleanGlobPathWindows(dir)
} else {
dir = cleanGlobPath(dir)
}

@ianlancetaylor ianlancetaylor changed the title path/filePath: If the function Glob has a wildcard and the last string is /, it does not capture correctly path/filepath: Glob fails if it has a wildcard and ends with / Aug 16, 2019
@ianlancetaylor ianlancetaylor modified the milestones: Unplanned, Go1.14 Aug 16, 2019
@odeke-em
Copy link
Member

odeke-em commented Oct 7, 2019

Hello @BlackHole1, thank you for the report!
@jamdagni86 would you like to send a CL as you had recommended above, and a test following up too, for Go1.14?

@rsc rsc modified the milestones: Go1.14, Backlog Oct 9, 2019
jamdagni86 added a commit to jamdagni86/go that referenced this issue Nov 19, 2019
The Glob tries to get the parent directory of the given directory.
When a trailing slash is present, filepath.Split returns the path as it is as it can not split it further.
This fix cleans the path first by remoivng any trailing slash before getting the parent directory.

Fixes golang#33617
@gopherbot
Copy link

Change https://golang.org/cl/207837 mentions this issue: path/filepath: Glob fails if it has a wildcard and ends with /

@ianlancetaylor
Copy link
Contributor

I don't understand this issue report. The docs for filepath.Glob say that it uses the rules for filepath.Match. The docs for filepath.Match do not say that a pattern with a trailing slash matches the same name without the trailing slash.

What is the actual bug here? Where do the functions not act as documented?

@BlackHole1
Copy link
Author

BlackHole1 commented Nov 20, 2019

With or without the slash in the trailing, I think the matching result should be the same, but it is not the case.

@ianlancetaylor
Copy link
Contributor

The documentation for Glob says that it follows the same as rules as Match. The documentation for Match does not say anything about a trailing slash. Consider https://play.golang.org/p/6j4icmyaYpp.

What do you think Glob("/usr/*/go/") should print?

@BlackHole1
Copy link
Author

I think their output should be the same. The last slash should not affect the match.

The Match design principle is to follow http://man7.org/linux/man-pages/man7/glob.7.html

Although I didn't find the relevant instructions in the above document, I tried several shells, and the results of their matching have nothing to do with the last slash.

@ianlancetaylor
Copy link
Contributor

I have a file /home/iant/hello.go. Using bash:

> echo /home/*/hello.go
/home/iant/hello.go
> echo /home/*/hello.go/
/home/*/hello.go/

So I don't agree with you that the last slash does not affect the match. It does affect it.

@BlackHole1
Copy link
Author

BlackHole1 commented Nov 21, 2019

It seems that if it is a file at the end, it is influential. But when the last is the directory, there is no affect.

> tree ./test
./test
├── a
│   └── x
│       └── 1.txt
└── b
    └── x
        └── 2.txt

4 directories, 2 files
> echo ./test/*/x/
./test/a/x/ ./test/b/x/

> echo ./test/*/x
./test/a/x ./test/b/x
> ls ./test/*/x/
./test/a/x/:
1.txt

./test/b/x/:
2.txt

> ls ./test/*/x
./test/a/x:
1.txt

./test/b/x:
2.txt

@ianlancetaylor
Copy link
Contributor

So it seems that we need to decide whether filepath.Glob should permit a trailing slash to match a directory name.

@BlackHole1
Copy link
Author

BlackHole1 commented Nov 21, 2019

I would like to confirm whether to modify filepath.Glob or filepath.Match?

@ianlancetaylor
Copy link
Contributor

I don't see how it is possible to change filepath.Match for this, as filepath.Match is only text based.

(That is of course an argument for not changing filepath.Glob, which is currently defined in terms of filepath.Match.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants