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: path.Clean should not remove the last slash of a path #59904

Closed
stephane-archer opened this issue Apr 30, 2023 · 9 comments
Closed

path: path.Clean should not remove the last slash of a path #59904

stephane-archer opened this issue Apr 30, 2023 · 9 comments

Comments

@stephane-archer
Copy link

dirpath := "./dir/"
dirpathclean:=path.Clean(dirpath)
if path.Dir(dirpath) != path.Dir(dirpathclean) {
    t.Fatal("WTF")
}

path.Clean should not remove the last slash of a path otherwise the rest of the path library doesn't have the appropriate behavior

@seankhliao
Copy link
Member

Working as intended.

If clean was a no-op, it wouldn't need to exist.
Canonical names do not have trailing slashes.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Apr 30, 2023
@stephane-archer
Copy link
Author

@seankhliao How do you know if the path represents a path to a directory or to a file? trailing slashes have that meaning, right? if you said that ./dir/ is the same as ./dir what about ./file.jpg/ and ./file.jpg?

@seankhliao
Copy link
Member

nothing stops a directory from being called file.jpeg
a plain path does not preserve that information

@stephane-archer
Copy link
Author

@seankhliao don't you think trailing slashes have a meaning? files will not have trailing slashes right?

@ianlancetaylor
Copy link
Contributor

@stephane-archer At this time we aren't going to change the behavior of path.Clean. It is behaving as documented and intended. Thanks.

@stephane-archer
Copy link
Author

@ianlancetaylor It's strange that path.Dir expects trailing slashes to work properly and path.Clean removes them. I can't get the logic, would you explain?

@ianlancetaylor
Copy link
Contributor

It seems to me that your point about path.Dir doesn't hold if we assume that paths should normally not have trailing slashes. It's true that path.Dir("a/") returns "a", but that's only because of the unexpected trailing slash. path.Dir is not supposed to return a directory if the path refers to a directory; it's supposed to return the parent directory of whatever it is given, so that path.Dir("a") returns ".".

@stephane-archer
Copy link
Author

stephane-archer commented May 9, 2023 via email

@ianlancetaylor
Copy link
Contributor

Whether it was a mistake or not, we can't change it now, or we would break the Go 1 compatibility guarantee: https://go.dev/doc/go1compat.

@golang golang locked as resolved and limited conversation to collaborators May 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants