Navigation Menu

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: FS.Open not compatible with os.Open #46626

Closed
acynothia opened this issue Jun 7, 2021 · 2 comments
Closed

embed: FS.Open not compatible with os.Open #46626

acynothia opened this issue Jun 7, 2021 · 2 comments

Comments

@acynothia
Copy link

acynothia commented Jun 7, 2021

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

$ go version
go version go1.16.5 linux/amd64

Does this issue reproduce with the latest release?

yes

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

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/root/.cache/go-build"
GOENV="/root/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/root/.go/pkg/mod"
GONOPROXY="gitlab.papegames.com,gitee.com"
GONOSUMDB="gitlab.papegames.com,gitee.com"
GOOS="linux"
GOPATH="/root/.go:/root/go"
GOPRIVATE=""
GOPROXY="https://goproxy.cn,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="go1.16.5"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/root/go/src/test/testdir/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build2652719403=/tmp/go-build -gno-record-gcc-switches"

What did you do?

package main

import (
    "embed"
    "fmt"
    "os"
)

//go:embed test
var staticFS embed.FS

func main() {
    fmt.Println(os.Open("./test"))
    fmt.Println("----")
    fmt.Println(os.Open("./test/"))
    fmt.Println("----")
    fmt.Println(staticFS.Open("test"))
    fmt.Println("----")
    fmt.Println(staticFS.Open("test/"))
}

What did you expect to see?

the same as fmt.Println(staticFS.Open("test"))

What did you see instead?

&{0xc00004e180} <nil>
----
&{0xc00004e1e0} <nil>
----
&{0x4eab98 [{test/a str1
 [63 163 252 108 86 4 30 104 91 211 217 230 175 19 117 126]} {test/b str2
 [157 163 134 109 137 33 72 250 70 235 54 167 230 171 44 117]}] 0} <nil>
----
<nil> open test/: file does not exist

It cause http.FS can not found index.html, because dir always end with /

@seankhliao
Copy link
Member

This works as intended, fs.FS implementations should validate paths with https://pkg.go.dev/io/fs#ValidPath which rejects trailing slashes. If you want to serve index.html then you need to rewrite/redirect the request, see what http.FileServer does.

Closing as this works as intended

@acynothia
Copy link
Author

This works as intended, fs.FS implementations should validate paths with https://pkg.go.dev/io/fs#ValidPath which rejects trailing slashes. If you want to serve index.html then you need to rewrite/redirect the request, see what http.FileServer does.

Thanks for your explanation. If fs.FS implementations should validate paths, can we trim slashes in http.FS https://github.com/golang/go/blob/master/src/net/http/fs.go#L758 ?

For now. http.Handle("/", http.FileServer(http.FS(fsys))) with embed.FS can only work in root path because of specified dotFile make it work.

@golang golang locked and limited conversation to collaborators Jun 7, 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