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

embed: embed path on different OS cannot open file #45230

Closed
kesuskim opened this issue Mar 25, 2021 · 3 comments
Closed

embed: embed path on different OS cannot open file #45230

kesuskim opened this issue Mar 25, 2021 · 3 comments

Comments

@kesuskim
Copy link

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

$ go version
1.16.2

Does this issue reproduce with the latest release?

Yes

Tested OS

Windows, Linux

What did you do?

//go:embed dir/myfile1.txt dir/myfile2.txt
var embedDir embed.FS

func init() {
    entries, _ := embedDir.ReadDir("dir")
    for _, entry := range entries {
        fmt.Println(embedDir.Name())  // this works
        fmt.Println(embedDir.Info())  // this works, too
        fmt.Println(embedDir.ReadFile(filepath.Join("dir", entry.Name()))) // this fails on Windows
    }
}

This works perfectly okay with Linux as it follows notation of Linux filesystem, but on Windows, even though it can fetch fs.dirEntry, it cannot read file with error given; open dir/myfile1.txt: file does not exist.

I can change embed notation with double backslash \\ and it will work.

//go:embed dir\\myfile1.txt dir\\myfile2.txt
var embedDir embed.FS // now it will work on Windows!!

So I can make that compile only on Windows, but somehow I think this is bug, because it can anyway fetch meta information of file, just cannot open it.

@kesuskim
Copy link
Author

From documentation of embed, it clearly says The path separator is a forward slash, even on Windows systems., so this seems bug.

image

@nanokatze
Copy link

filepath.Join will join using backward slash on Windows, path.Join will use forward slash

@kesuskim
Copy link
Author

Oh, so filepath.Join should not be used at all :( Previous implementation should also be little adjusted to that, thank you for clarification @nanokatze .

bingoohuang added a commit to bingoohuang/httplive that referenced this issue Mar 3, 2022
filepath.Join will join using backward slash on Windows, path.Join will use forward slash
golang/go#45230
@golang golang locked and limited conversation to collaborators Mar 25, 2022
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

3 participants