Skip to content
This repository has been archived by the owner on Jun 25, 2022. It is now read-only.

Calling packr build/install with a *.go files causes boxes not to be packed #75

Closed
selfup opened this issue Jul 29, 2018 · 20 comments
Closed

Comments

@selfup
Copy link
Contributor

selfup commented Jul 29, 2018

Hey so this is a longshot..

I am using packr to compile static assets to a single go binary.

It's a webserver. I can move the exe around on my machine and it works.

However when someone else on a different windows machine tries it, it says 404 not found :thinking_face:

Haven't tried on Mac yet, but I guess I would run into the same issues 😂

Here is my repo: https://github.com/selfup/hmrcmd

Any help would be awesome! 🙏

@selfup
Copy link
Contributor Author

selfup commented Jul 30, 2018

Ok so turns out you can't really use any flag you want with packr build. If I do something like:

packr build -o ./releases/foo main.go

Then the files are not found. Binary seems to build fine until another machine tries to run it.

Server runs fine, just the static assets are not there. Seems that with the flag, packr defaults to absolute paths as if in development?

However:

packr build && mv dirname_bin ./releases/foo

Works!

Unclear if the language in the README needs to be updated or not, but this took me for quite the spin 😂


Here is an example I use:

if [[ -d build ]]
then
  rm -rf build
fi

if [[ -d releases ]]
then
  rm -rf releases
fi

npm run build \
  && mkdir releases \
  && GOOS=darwin GOARCH=amd64 packr build && mv ./hmrcmd ./releases/darwin-hmrcmd \
  && GOOS=linux GOARCH=amd64 packr build && mv ./hmrcmd ./releases/linux-hmrcmd \
  && GOOS=linux GOARCH=arm GOARM=5 packr build && mv ./hmrcmd ./releases/rpi-hmrcmd \
  && GOOS=windows GOARCH=386 packr build && mv ./hmrcmd.exe ./releases/hmrcmd.exe \
  && packr clean

Seems to do the trick 🎉

@alexsasharegan
Copy link

alexsasharegan commented Jul 30, 2018

I think I'm running into a similar problem trying to package something for a raspberry pi. Your mv fix isn't working for me.

Fixed with https://github.com/rakyll/statik

@selfup
Copy link
Contributor Author

selfup commented Jul 30, 2018

Interesting. I haven't tried on the rpi yet 🤔

Thanks for bringing it up!

@alexsasharegan
Copy link

Yeah, and just to be clear, I didn't mean to imply the other lib is better or anything. I just needed something working by tomorrow, so after an hour debugging I was open to seeing if anything else "just worked".

@markbates
Copy link
Member

@selfup please make sure you are using the latest version. Also, when I tried to build it wouldn't because you were asking for packr.NewBox("build") which doesn't exist in the repo, causing it to fail. When I changed to it to ./src it compiled correctly.

If the issue persists with the latest version of packr, please re-open. Thanks.

@selfup
Copy link
Contributor Author

selfup commented Aug 8, 2018

@markbates build exists once you npm run build, there is a script in my scripts directory. Sorry for the confusion. I don't version control transpiled JS.

I did go get -u github.com/gobuffalo/packr/... so it is the latest version. No worries I figured it out using the mv command.

As laid out in my snippet here:

if [[ -d build ]]
then
  rm -rf build
fi

if [[ -d releases ]]
then
  rm -rf releases
fi

npm run build \
  && mkdir releases \
  && GOOS=darwin GOARCH=amd64 packr build && mv ./hmrcmd ./releases/darwin-hmrcmd \
  && GOOS=linux GOARCH=amd64 packr build && mv ./hmrcmd ./releases/linux-hmrcmd \
  && GOOS=linux GOARCH=arm GOARM=5 packr build && mv ./hmrcmd ./releases/rpi-hmrcmd \
  && GOOS=windows GOARCH=386 packr build && mv ./hmrcmd.exe ./releases/hmrcmd.exe \
  && packr clean

It's all good, at least this issue is here with a fix so people know what to do when they run into this as well 🙏

@marhaupe
Copy link

marhaupe commented Aug 20, 2018

I too experienced this bug. The -o command seems to mess up the whole building process for some reason. @selfup your workaround saved me so much time, but doesn't actually solve the issue itself.

This Dockerfile command works:

RUN go get -u github.com/gobuffalo/packr/... && \
  cd ./server && \
  CGO_ENABLED=0 GOOS=linux packr build -a -installsuffix cgo && ls && mv ./server /src/bin/GoReactBootstrap && ls /src/bin/ 

While this does not work:

RUN go get -u github.com/gobuffalo/packr/... && \
  cd ./server && \
  CGO_ENABLED=0 GOOS=linux packr build -a -installsuffix cgo -o /src/bin/GoReactBootstrap main.go

The difference is that I specified the output file with -o and the entry file main.go. @markbates: Since the snippets use the latest version of packr, can you reopen the issue please?

@markbates markbates reopened this Aug 20, 2018
@selfup
Copy link
Contributor Author

selfup commented Aug 20, 2018

@marhaupe glad it saved you time 😄

While not a fix, at least it's documented and indexed for those running into this as well.

Thanks for the re-open 🙏

@marhaupe
Copy link

If anyone needs some test data: The snippet above can be found at https://github.com/marhaupe/GoReactBootstrap/blob/master/Dockerfile. packr version number at the time this didn't work was v1.13.2.

@markbates markbates changed the title File not found when go binary is used on another machine Calling packr build/install with a *.go files causes boxes not to be packed Sep 12, 2018
@markbates
Copy link
Member

I've updated the ticket title, since the cause of this issue is now known.

The problem is actually that when calling go build main.go Go only compiles main.go and any imports associated. So when running packr build main.go Go still only compiles main.go and ignores the the *-packr.go file in the directory.

The solution, until a "fix" can be found, is to call packr build with folders, not with files.

markbates added a commit that referenced this issue Sep 12, 2018
@selfup
Copy link
Contributor Author

selfup commented Sep 13, 2018

Thanks for the update!

@markbates
Copy link
Member

I believe this PR solves this issue https://github.com/gobuffalo/packr/pull/94/files I would love those affected by this issue could try it out.

@marhaupe
Copy link

I compiled the binary in your "small-fixes" branch with RUN CGO_ENABLED=0 GOOS=linux go build and directly copied it into the Docker-Container at https://github.com/marhaupe/go-react-bootstrap. Running

RUN CGO_ENABLED=0 GOOS=linux ./packr build -a -installsuffix cgo -o /src/bin/GoReactBootstrap main.go

still won't produce the desired result. But to be completely honest, I'm not exactly sure if I compiled the binary correctly. Feel free to use the repo to reproduce the issue or give me a heads up on what I did wrong.

@selfup
Copy link
Contributor Author

selfup commented Sep 21, 2018

I don't mind using mv bin ./releases

If that's how it's going to be we should add a section to the README or something?

Otherwise we can continue to help 😄

Would be neat once the behavior is seamless

Thanks for the updates @marhaupe 🙏

@markbates
Copy link
Member

@marhaupe thanks for trying. Question, did you compile the binary under the v2 folder or the root folder? I just want to make sure you tried with v2, and not just v1. I’ll try your repo and see if I can figure it out.

@selfup adding a note to the README about how Go works in those situations is a great. PR?

@selfup
Copy link
Contributor Author

selfup commented Sep 22, 2018

Yea I can make one in a bit! Errands and stuff this morning. 👍

@marhaupe
Copy link

@markbates I only compiled it for v1. Compiling it for v2 outputs the following error messages, but that is most likely due to a mistake on my side:

cannot find package "github.com/gobuffalo/packr/v2"
cannot find package "github.com/gobuffalo/packr/v2/file/resolver"

To compile the binary, I ran GO111MODULE=on go get && GOOS=linux go build from the ./v2/packr folder. Am I missing something?

@markbates
Copy link
Member

markbates commented Sep 22, 2018 via email

@selfup
Copy link
Contributor Author

selfup commented Sep 22, 2018

PR is up #95 🎉

@markbates
Copy link
Member

This should be fixed in v2. Closing this issue for now. Please re-open if the problem still persists with v2.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants