Set Go variable with ldflags conflicts with vendor folder

931 Views Asked by At

I'm currently developing a small Go app and I want to set a specific variable (like Version, GitCommit, BuildID...etc.) at build or runtime (with go build or go run) by using the -ldflags option.

Because in my company we have several projects with the same base, I decided to extract the code with these variables in a separate "info" module which could be imported in every project.

Here's my problem, say I'm running the app like this:

go run -ldflags "-X git.mycompany.com/utils/info.Version=1.0.0" app.go

This works well, and the variable is set correctly even though the variable is not part of the "main" app but in a dependency.

Then I decided to deploy the app so I used the new dep tool to generate the vendor folder. Therefore, the "info" dependency is now in: vendor/git.mycompany.com/utils/info

Now when I run the same command as above, the said variable (Version) is not set anymore.

Am I missing something here ? As soon as I delete the vendor folder, everything works fine again. It's like this vendor folder is conflicting with the -ldflags option or something.

Thanks in advance!

1

There are 1 best solutions below

0
On

We had exactly the same problem, after lots of research we stumbled upon the solution in a comment to GitHub issue: cmd/link: -X doesn't work for vendored packages.

Solution: the full path name, relative to $GOPATH should be specified.

It works when developing git.mycompany.com/utils/info because the full path is correct.
It doesn't work for vendored dependencies because the full path from $GOPATH would be like git.mycompany.com/name-of/package/vendor/git.mycompany.com/utils/info.Version=1.0.0

Unfortunately, no documentation seem to be present about this ( for further info look at the issue ) but as Dave Cheney points out in a comment:

this is a side effect of the language way vendoring is implemented