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: Dir() returns incorrect result if the path contains a trailing slash #13199

Closed
0xmohit opened this issue Nov 10, 2015 · 5 comments

Comments

@0xmohit
Copy link
Contributor

0xmohit commented Nov 10, 2015

filepath.Dir returns incorrect result if the specified path contains a trailing slash. For example, if the input were /home/, then filepath.Dir and filepath.Base return /home and home respectively. Playground link.

Following is the output of dirname and basename from GNU coreutils:

$ dirname /home
/
$ basename /home
home
$ dirname /home/
/
$ basename /home/
home

When the input doesn't contain a trailing slash, then the functions yield expected output.

@adg
Copy link
Contributor

adg commented Nov 10, 2015

There does seem to be an inconsistency between filepath.Dir and filepath.Base. IMO Dir should not include the component returned by Base.

@gopherbot
Copy link
Contributor

CL https://golang.org/cl/16791 mentions this issue.

@rsc
Copy link
Contributor

rsc commented Nov 25, 2015

The docs for Dir say "Dir returns all but the last element of path, typically the path's directory." They don't guarantee to get the path's directory. See my longer comment on CL 16791, but I don't think we should change this.

@rsc rsc closed this as completed Nov 25, 2015
@0xmohit
Copy link
Contributor Author

0xmohit commented Nov 26, 2015

This was specifically regarding the oddity in the current implementation (the second case in the playground link above):

input path: /home/, dir: /home, base: home

Whether Dir returns /home or / in this case, Base should adjust itself. To elaborate, one would expect that

filepath.Clean(p) == filepath.Join(filepath.Dir(p), filepath.Base(p))

@rsc
Copy link
Contributor

rsc commented Nov 26, 2015

@0xmohit that's not guaranteed by the docs. The docs state different behaviors for Dir and Base, and I don't feel at liberty to contradict the established behavior and docs. If you need that guarantee, use filepath.Split.

@golang golang locked and limited conversation to collaborators Nov 27, 2016
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

4 participants