go-bindata-assetfs icon indicating copy to clipboard operation
go-bindata-assetfs copied to clipboard

assetFS function only returns first folder

Open awalterschulze opened this issue 9 years ago • 14 comments

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")
}

awalterschulze avatar Apr 20 '15 09:04 awalterschulze

What happens when you do that with go-bindata?

elazarl avatar Apr 20 '15 11:04 elazarl

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 avatar Apr 20 '15 11:04 awalterschulze

@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.

markwallsgrove avatar Jan 02 '16 13:01 markwallsgrove

@awalterschulze https://github.com/jteeuwen/go-bindata/issues/67

markwallsgrove avatar Jan 02 '16 13:01 markwallsgrove

@awalterschulze does this solve your problem?

markwallsgrove avatar Jan 02 '16 13:01 markwallsgrove

No, not at all. Why is the loop in the code above only returning the first one and not all folders in the list?

awalterschulze avatar Jan 04 '16 11:01 awalterschulze

I'm seeing the same issue, it only looks in the first directory returned by the 'range' map lookup

curtpm avatar Jan 04 '16 21:01 curtpm

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.

hut8 avatar Jan 17 '16 07:01 hut8

a simple workaround is adding an additional parent directory around the <dir1>... <dir2>... directories and then just referencing <parent_dir>...

schmohlio avatar Nov 11 '16 16:11 schmohlio

Why is a workaround needed when there is a clear bug here?

awalterschulze avatar Nov 12 '16 09:11 awalterschulze

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...

schmohlio avatar Nov 12 '16 18:11 schmohlio

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,

elazarl avatar Nov 28 '16 05:11 elazarl

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: ""}

tiramiseb avatar Jun 14 '17 14:06 tiramiseb

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 .

awalterschulze avatar Jun 14 '17 14:06 awalterschulze