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

proposal: path: should path.Ext() handle dotfiles specially? #24239

Closed
lassik opened this issue Mar 4, 2018 · 6 comments
Closed

proposal: path: should path.Ext() handle dotfiles specially? #24239

lassik opened this issue Mar 4, 2018 · 6 comments
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Milestone

Comments

@lassik
Copy link

lassik commented Mar 4, 2018

What Go version, operating system and architecture are you using?

  • go1.10 nacl/amd64p32
  • go1.10 darwin/amd64

What did you do?

Call path.Ext() on various filenames that start with a dot. Complete example at https://play.golang.org/p/leXBZvbpbfj

What did you expect to see?

path.Ext() is meant to return the filename extension, which by convention specifies the file type. But if the very first character of the filename is a dot, that denotes a "hidden" file under Unix. Then the part after that dot is simply meant to denote the file's name, not its type. Altogether we have 4 cases:

  1. name - normal file without extension
  2. name.ext - normal file with extension
  3. .name - dotfile without extension
  4. .name.ext - dotfile with extension

I expected path.Ext() not to return the name part in any of these cases.

What did you see instead?

As seen in the playground example, path.Ext() currently gives an intuitive result for all cases except 3. The implementation has no special handling for dotfiles so in case 3 it thinks the entire filename denotes an extension with the actual name part being blank.

Python's os.path.splitext() handles dotfiles specially:

Split the pathname path into a pair (root, ext) such that root + ext == path, and ext is empty or begins with a period and contains at most one period. Leading periods on the basename are ignored; splitext('.cshrc') returns ('.cshrc', '').

It would seem reasonable to me for Go's path.Ext() to also have this behavior. What do others think?

@cznic
Copy link
Contributor

cznic commented Mar 4, 2018

The implementation has no special handling for dotfiles so in case 3 it thinks the entire filename denotes an extension with the actual name part being blank.

That's good design in my books. Less special cases are better. The definition of what a file extension is can then stay simple and that's desirable.

@mvdan
Copy link
Member

mvdan commented Mar 4, 2018

I agree that the behavior is somewhat confusing if you're used to modern unix-like systems, which make heavy use of dotfiles. However, I think it's also worth pointing out that dotfiles are not special at all on some systems such as Windows, as they have a completely different definition of hidden files.

With that in mind, I'm not sure it's a good idea for these APIs to handle dotfiles. For example, what should path.Ext(".exe") do on Windows? There can be multiple answers to that question, but I think they all add complexity to Ext.

@mvdan
Copy link
Member

mvdan commented Mar 4, 2018

Playing my own devil's advocate - you could also argue that this isn't about dotfiles, but about filename extensions being a suffix to a non-empty name. That depends on how systems like Windows initerpret filenames like .exe - is it a file with name .exe and no extension, or a file with no name and .exe extension?

@slrz
Copy link

slrz commented Mar 4, 2018

Considering that the concept of a file name extension is defined right there in the path.Ext docs, I think the current behaviour is fine and can't be changed anyway (for Go 1, at least).

The extension is the suffix beginning at the final dot in the final slash-separated element of path; it is empty if there is no dot.

@bradfitz
Copy link
Contributor

bradfitz commented Mar 5, 2018

Yeah, we can't chnnge this for Go 1, so this is by definition a Go 2 proposal.

But I believe enough people on the Go team view dotfiles as horrible that I don't imagine they'd get much if any special support.

@bradfitz bradfitz added the v2 A language change or incompatible library change label Mar 5, 2018
@bradfitz bradfitz changed the title path: should path.Ext() handle dotfiles specially? proposal: path: should path.Ext() handle dotfiles specially? Mar 5, 2018
@gopherbot gopherbot added this to the Proposal milestone Mar 5, 2018
@ianlancetaylor
Copy link
Contributor

The current path.Ext function is simple, and this proposal would make it slightly more complex. If you want to handle dot files specially, it's trivial to write your own function. We don't need to change the standard one.

@golang golang locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge Proposal v2 A language change or incompatible library change
Projects
None yet
Development

No branches or pull requests

7 participants