Skip to content

Embed does not respect Windows filepaths #44305

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

Closed
200sc opened this issue Feb 16, 2021 · 2 comments
Closed

Embed does not respect Windows filepaths #44305

200sc opened this issue Feb 16, 2021 · 2 comments

Comments

@200sc
Copy link

200sc commented Feb 16, 2021

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

$ go version
go version go1.16 windows/amd64

Does this issue reproduce with the latest release?

Yes, it is introduced by the latest release, which introduced embed

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GOARCH=amd64
set GOHOSTARCH=amd64
set GOHOSTOS=windows

What did you do?

With a file at ./assets/test.json:

package main

import (
	"embed"
	"fmt"
	"io/ioutil"
	"path/filepath"
)

//go:embed assets/*
var assets embed.FS

func main() {
	path := filepath.Join("assets", "test.json")
	data, err := ioutil.ReadFile(path)
	if err != nil {
		fmt.Println("error reading real file:", err)
		return
	}
	fmt.Println(string(data))

	data, err = assets.ReadFile(path)
	if err != nil {
		fmt.Println("error reading embedded file:", err)
		return
	}
	fmt.Println(string(data))
}

What did you expect to see?

$ go run main.go
{"hello":"world"}
{"hello":"world"}

What did you see instead?

$ go run main.go
{"hello":"world"}
error reading embedded file: open assets\test.json: file does not exist

We can infer this is a path problem because if we instead do:

path = strings.ReplaceAll(path, "\\", "/")
data, err = assets.ReadFile(path)

Then the embed call succeeds.

I tested this with cmd and bash, and both exhibited this behavior.

@eliasnaur
Copy link
Contributor

The io/fs.FS package uses forward slashes for filenames, even on Windows. See https://golang.org/pkg/io/fs/#ValidPath:

"Note that paths are slash-separated on all systems, even Windows."

You should use path.Join, not filepath.Join for embed filenames.

@200sc
Copy link
Author

200sc commented Feb 16, 2021

It seems really unfortunate that embed doesn't accept filepaths used by the OS-- it almost feels like using path/filepath at all is a bad idea now.

But if that's what's decided, that's what's decided.

@golang golang locked and limited conversation to collaborators Feb 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants