CenturyLinkLabs / golang-builder

Containerized build environment for compiling an executable Golang package and packaging it in a light-weight Docker container.
Apache License 2.0
398 stars 85 forks source link

go install github.com/hashicorp/vault: build output "vault" already exists and is a directory #14

Closed odigity closed 8 years ago

odigity commented 8 years ago

I'm trying to use golang-builder to build an image for Vault (https://vaultproject.io/), and getting the above error.

I read the golang-builder README, cloned the vault repo (main package in root), created a Dockerfile in root:

FROM scratch
EXPOSE 8200
COPY vault /
ENTRYPOINT ["/vault"]

Then ran the build command:

docker run --rm -v "$(pwd):/src" -v /var/run/docker.sock:/var/run/docker.sock  centurylink/golang-builder

And got this:

Building github.com/hashicorp/vault
go install github.com/hashicorp/vault: build output "vault" already exists and is a directory

I'm not fluent in Go, but when I posted the issue to the vault repo, one of the maintainers said:

go install is supposed to be keyed off of the GOPATH into a bin dir. It sounds like they're either ignoring the GOPATH or they're doing custom things with it, like setting it to the root of the source directory -- which would also explain why they need the canonical import path (my guess is that some time ago when I used it a bit they were taking the current dir, removing GOPATH from the front, and using that inside the builder container, and now they're just using the source directory directly).

There's nothing we're doing wrong, here -- and it's not uncommon practice, either -- so this really needs to be fixed on their end.

https://github.com/hashicorp/vault/issues/165#issuecomment-154210535

chiefy commented 8 years ago

@odigity I was curious about this, so I looked into it. The issue is that by default the builder tries to place the compiled binary (in this case a file named vault) into the $pkgName directory ($GOPATH/src/github.com/hashicorp/vault). Since the vault project has a directory inside it called vault, the compiled binary can't get copied. Hence the error you were seeing.

I forked the project and got it working by adding an output directory called bin by default, so now the binary will get copied into $GOHOME/src/github.com/hashicorp/vault/bin.

odigity commented 8 years ago

I cloned your repo (chiefly/golang-builder/master), ran docker build . in the build/ dir, and ran my original docker run command with your image instead of centurylinklabs/golang-builder which resulted in a vault binary being created in bin/.

Your fix works! Thanks.

(Going to wait till the PR is merged and a new image is pushed to Docker Hub before continuing so I don't have to code to a fork and then update later.)

davidgardner11 commented 8 years ago

PR#13 accepted (https://github.com/CenturyLinkLabs/golang-builder/pull/13) - closing this issue.

leifhanack commented 8 years ago

Hello @chiefy, I still got the above error, do you have an idea what I'm doing wrong?

╭─ in ~/projects/go/src/github.com/hashicorp/vault on master ✘ (origin/master)
╰$ ls
CHANGELOG.md    Godeps          README.md       builtin         helper          main.go         physical        terraform       vendor
CONTRIBUTING.md LICENSE         api             cli             http            main_test.go    scripts         test            version
Dockerfile      Makefile        audit           command         logical         make.bat        shamir          vault           website
╭─ in ~/projects/go/src/github.com/hashicorp/vault on master ✘ (origin/master)
╰$ docker run --rm -v "$(pwd):/src" -v /var/run/docker.sock:/var/run/docker.sock  centurylink/golang-builder
Building github.com/hashicorp/vault
go install github.com/hashicorp/vault: build output "vault" already exists and is a directory
╭─ in ~/projects/go/src/github.com/hashicorp/vault on master ✘ (origin/master)
╰$ echo $GOPATH
/Users/leif.hanack/projects/go
╭─ in ~/projects/go/src/github.com/hashicorp/vault on master ✘ (origin/master)
╰$ echo $GOROOT
/usr/local/Cellar/go/1.6/libexec

The golang-builder image is 3 weeks old so it should be the one, that is containing your fix:

centurylink/golang-builder latest 9491cd026567 3 weeks ago 754.8 MB

My Dockerfile contains

FROM scratch
EXPOSE 8200
COPY vault /
ENTRYPOINT ["/vault"]

Thanks a lot, Leif

leifhanack commented 8 years ago

Hi there,

for me it looks like this issue isn't fixed and should be opened until PR #15 is somehow integrated. Issue #13 alone did not solves it. If I apply #15 locally everything is fine.

Regards, Leif

chiefy commented 8 years ago

@leifhanack feel free to use my fork :mask:

tyrannosaurus-becks commented 5 years ago

This issue is long closed, but just in case anyone stumbles upon it again, I just wanted to offer two additional solutions. The issue occurs because go build is trying to make a vault binary, but there's already a vault directory it doesn't want to overwrite, which is good.

This can be worked around by using a different location for the generated binary to live:

$ go build -o=bin/vault

However, even better would be doing it how we do it on the Vault team:

make bootstrap
make build

This can be found in the README but may not have been documented at the time of this issue.