go-bindata-assetfs
go-bindata-assetfs copied to clipboard
assetFS function only returns first folder
go-bindata-assetfs -pkg=bootstrap css fonts js
Only the css folder is returned.
func assetFS() *assetfs.AssetFS {
for k := range _bintree.Children {
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, Prefix: k}
}
panic("unreachable")
}
What happens when you do that with go-bindata?
I don't know, but it is pretty obvious from the code above that assetFS() is looping through all the Children and just returning the first one.
@awalterschulze I believe I read somewhere on go-bindata that if you define the folder such as "css/..." "fonts/..." "js/..."
all subdirectories are also included. I shall check to see where I found that information.
@awalterschulze https://github.com/jteeuwen/go-bindata/issues/67
@awalterschulze does this solve your problem?
No, not at all. Why is the loop in the code above only returning the first one and not all folders in the list?
I'm seeing the same issue, it only looks in the first directory returned by the 'range' map lookup
This seems totally broken. The error I'm getting, I think, is caused by this. I don't understand why the selecting of assets is done this way, but the lack of any sort of error handling code makes this really hard to debug when you're developing with this package. I started out generating the files like this:
go-bindata-assetfs -debug -prefix ../app/static/ ../app/static/...
then trying to use it like this:
controller.PathPrefix("/static/").Handler(
http.StripPrefix("/static/",
http.FileServer(assetFS())))
I ended up with a bunch of 500 errors with empty responses; nothing printed to the console or the response. So now to debug, I did some hackery:
controller.PathPrefix("/static/").Handler(
http.StripPrefix("/static/",
http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
fmt.Println("got request for: ", r.URL.Path)
f, err := assetFS().Open(r.URL.Path)
if err != nil {
fmt.Println(err)
}
fi, _ := f.Stat()
http.ServeContent(
rw, r,
fi.Name(),
fi.ModTime(),
f)
})))
That prints this:
got request for: img/gps.svg
open index.html/img/gps.svg: no such file or directory
:interrobang:
In my auto-generated file, this is an excerpt:
"glyphicons-halflings-regular.woff2": &bintree{fontsGlyphiconsHalflingsRegularWoff2, map[string]*bintree{}},
}},
"img": &bintree{nil, map[string]*bintree{
"gps.svg": &bintree{imgGpsSvg, map[string]*bintree{}},
}},
"index.html": &bintree{indexHtml, map[string]*bintree{}},
"js": &bintree{nil, map[string]*bintree{
"angular-moment.min.js": &bintree{jsAngularMomentMinJs, map[string]*bintree{}},
"angular-moment.min.js.map": &bintree{jsAngularMomentMinJsMap, map[string]*bintree{}},
"angular-resource.min.js": &bintree{jsAngularResourceMinJs, map[string]*bintree{}},
Same results without debug mode. When I use this, everything works fine:
controller.PathPrefix("/static/").Handler(
http.StripPrefix("/static/",
http.FileServer(http.Dir("../app/static/"))))
...and because of that, I would expect that this package would work too.
a simple workaround is adding an additional parent directory around the <dir1>... <dir2>...
directories and then just referencing <parent_dir>...
Why is a workaround needed when there is a clear bug here?
I would suggest fixing the bug, but offering a tip for those of us relying on the library currently. it saved me frustration; the ISSUE has been open for quite some time...
Hi all.
I don't have time to fix bugs myself, but I'll be happy to accept PR.
As I said in other places, I keep this library mainly as people already use it.
I think that today there are better solutions for embedding assets in Go, e.g., rice.go, and I don't see too much point with investing effort in another one.
Thanks,
For those having this problem, there is an approach to avoid this bug.
Given @hut8 's command:
go-bindata-assetfs -debug -prefix ../app/static/ ../app/static/...
... the command may be changed to:
go-bindata-assetfs -debug -prefix ../app/ ../app/static/...
... so that the (only) directory at the root of the assets is static/
and assetFS reads what is inside it.
The bug is in the assetFS()
function generated by the go-bindata-assetfs command
: this function loops on "root directories" in the assets and directly returns the first one.
I will not do a PR because it would break existing setups. Such a PR would change the following lines:
for k := range _bintree.Children {
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: k}
}
to:
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: ""}
I wanted to use assetfs again yesterday, but then I remembered this bug and decided not to :(
On Wed, 14 Jun 2017, 16:43 Sébastien Maccagnoni, [email protected] wrote:
For those having this problem, there is an approach to avoid this bug.
Given @hut8 https://github.com/hut8 's command:
go-bindata-assetfs -debug -prefix ../app/static/ ../app/static/...
... the command may be changed to:
go-bindata-assetfs -debug -prefix ../app/ ../app/static/...
... so that the (only) directory at the root of the assets is static/ and assetFS reads what is inside it.
The bug is in the assetFS() function generated by the go-bindata-assetfs command: this function loops on "root directories" in the assets and directly returns the first one.
I will not do a PR because it would break existing configurations. Such a PR would change the following lines:
for k := range _bintree.Children { return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: k} }
to:
return &assetfs.AssetFS{Asset: Asset, AssetDir: AssetDir, AssetInfo: assetInfo, Prefix: ""}
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/elazarl/go-bindata-assetfs/issues/14#issuecomment-308453630, or mute the thread https://github.com/notifications/unsubscribe-auth/ABvsLdfTrbsCpWlImGCXndFlmbIMX_yVks5sD_GUgaJpZM4EEHzf .