Skip to content

embed: FS root directory is always the package directory, while os.DirFS may have another root #43431

@alicebob

Description

@alicebob

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

go version devel +2ff33f5e44 Thu Dec 17 16:03:19 2020 +0000 linux/amd64

(that's 1.16beta1)

Does this issue reproduce with the latest release?

yes

What did you do?

I want to serve static documents in a webserver using the new embed package, but with an option to use local files.

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

There is a subdir ./docroot/ with an index.html file:

myuser@mymachine:/tmp/root$ ls -R
.:
docroot/  go.mod  main.go  root

./docroot:
index.html

When I use the os.DirFS() version (./root -docroot ./docroot) I get the index.html nicely at http://localhost:6602/index.html. If I use the embed version (./root) it ends up at http://localhost:6602/docroot/index.html

What did you expect to see?

I expect either none or both to have the ./docroot/ subdir in there. This way I can't use http.StripPrefix(). But maybe I missed something else.

What did you see instead?

The embed.FS and os.DirFS seem to behave inconsistently.

If embed.FS implemented fs.SubFS I think I could make this work by using docroot.Sub("./docroot/")

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions