golang/go

Embed does not respect Windows filepaths

Closed this issue · 2 comments

200sc commented

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.

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 commented

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.