Skip to content

Embed does not respect Windows filepaths #44305

@200sc

Description

@200sc

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions