gobuffalo / packr

The simple and easy way to embed static files into Go binaries.
MIT License
3.41k stars 196 forks source link

Box still looks at development path when binary deployed to server. #70

Closed peacefixation closed 6 years ago

peacefixation commented 6 years ago

I've followed the example in the README file and I'm not able to run my binary on a different machine, packr always looks for the path on my development machine.

Relevant project structure:

|-model/database.go |-sql/subDir/file.sql |-main.go

In model/database.go

getQuery("subDir/file.sql")

func getQuery(filename) { box := packr.NewBox("../sql") query := box.String(filename) return query }

I build with:

packr -v go build

or

packr build -v

I see expected output:

packing file subDir/file.sql packed file subDir/file.sql built box ../sql with ["subDir/file.sql"]

and run the binary:

./app-binary

On my server, the files are not found, I just get a blank string from box.String(filename). If I use box.MustString(filename) and inspect the error I can see it is looking at the path on my development machine (which doesn't exist on the server). If I call box.Has(filename) it returns false.

I can see the -packr.go file after running packr and my files exist there:

func init() { packr.PackJSONBytes("../sql", "subDir/file.sql", "\"SU5TRVJblah\"") }

Am I missing something?

markbates commented 6 years ago

The ../ indicates you are going up your directory tree, packr only works going down your directory. The reason is that since it drops go files those files need to be in the path you’re about to compile. Go doesn’t compile up your path, only down it.

peacefixation commented 6 years ago

I might not have illustrated my problem sufficiently, but I fixed the issue and it wasn't a packr problem so I'm sorry to mark it as one. I was reading the query files into global variables, and the read was executed before the init() function that initialises the packr box.

I'm still learning Go and on a new project and I didn't realise the order of execution that was occurring.