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: Ext does not return correct values #37589

Open
Zarathustra2 opened this issue Mar 1, 2020 · 3 comments
Open

path/filepath: Ext does not return correct values #37589

Zarathustra2 opened this issue Mar 1, 2020 · 3 comments
Labels
NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@Zarathustra2
Copy link

Consider "path/.config" and ".gitignore". If I use filepath.Ext(path) on either one of those strings I would receive ".config" or ".gitignore".

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

This is in my opinion not correct. I opened a PR #37580 but @ianlancetaylor stated it would break compatibility and the function does as documentd. Hence I closed it and opened this issue to start a discussion about it.

An extension for files exists as they provided meta data for operating systems for example to open them with a default application.

A dot file is a hidden file with a normal name. So .gitignore does not have the extension .gitignore the name of the file is gitignore. Similar to hidden folders.
If the file would be .gitignore.go then it would have the .go extension.

In any case filepath.Ext(path) does return an extension for those cases - hidden file, hidden folder- even though they do not have an extension.

A new method could be added which takes that into account or the old gets adjusted. Or maybe my understanding of extensions is just fundamentally wrong.

@ianlancetaylor
Copy link
Contributor

To be clear, I don't disagree with your definition of extensions. I just think that the function behaves exactly as documented, and that we should not change the documentation and behavior without a very good reason. The fact that the function doesn't quite correspond to most people's definition of extension does not seem to me to be a good enough reason.

@Zarathustra2
Copy link
Author

The fact that the function doesn't quite correspond to most people's definition of extension does not seem to me to be a good enough reason.

In my eyes it is the reason. Yes, the function does exactly what is documented but that doesn't mean it is right and that's the reason why I am seeking a discussion to better understand the reasons for it.

My problem is that filepath.Ext(path) does not work as one would expect it to work because it differs from the definition of file extensions: A filename extension is an identifier specified as a suffix to the name of a computer file.

So if I pass a hidden file without an extension to Ext() I would it expect to return no extension as the file just has a name.

Having a python background and also finding the same mechanics in rust, I wondered that golang behaved differently.

Examples Rust/Python

import os

# ('.gitignore', '')
print(os.path.splitext('.gitignore'))

# ('path/.config', '')
print(os.path.splitext('path/.config'))

# ('main', '.go')
print(os.path.splitext('main.go'))
use std::path::Path;

fn main() {
    
    // None
    println!("{:?}", Path::new(".gitignore").extension());

    // None
    println!("{:?}", Path::new("path/.config").extension());
    
    // go 
    println!("{:?}", Path::new("main.go").extension())
}

And I cannot find a use case where someone depends on filepath.Ext() being implemented as it is now. For me the filepath.Ext(path) does not behave like a function designed working with files but more like a util string function.

@dmitshur dmitshur added the NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. label Mar 2, 2020
@dmitshur dmitshur added this to the Backlog milestone Mar 2, 2020
@dcominottim
Copy link

IMO, the current behavior makes it unsuitable for anything since the result simply can't be trusted. What value do application developers get from it as-is? The fact it does what the documentation says it does only means it's badly implemented and conceived. It needs to correspond to how the supported operating systems' see/handle file extensions rather than to a bogus arbitrary definition.

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

No branches or pull requests

4 participants