kubebuilder: No GOPATH environment variable breaks code generators
Problem
If one uses the default gopath at $HOME/go, and therefore does not set a GOPATH environment variable, the deepcopy-gen will save its output files into the wrong destination path.
This is the output if one just tries to follow the quickstart without a defined GOPATH environment variable:
go generate ./pkg/... ./cmd/...
go fmt ./pkg/... ./cmd/...
go vet ./pkg/... ./cmd/...
# ships/pkg/apis/ships/pkg/apis/ships/v1beta1
pkg/apis/ships/pkg/apis/ships/v1beta1/zz_generated.deepcopy.go:27:11: undefined: Sloop
make: *** [Makefile:38: vet] Error 2
As one can see the output gets saved to pkg/apis/ships/pkg/apis/ships, so the relative output base is wrong.
The deepcopygen command line is defined in the pkg/apis/apis.go like this:
// Generate deepcopy for apis
//go:generate go run ../../vendor/k8s.io/code-generator/cmd/deepcopy-gen/main.go -O zz_generated.deepcopy -i ./... -h ../../hack/boilerplate.go.txt
It is not setting the output base on the command line, which works like this according to the help:
-o, --output-base string Output base; defaults to $GOPATH/src/ or ./ if $GOPATH is not set. (default "./")
As there is no GOPATH set, the default is the current working directory, and this is why there is the duplicate pkg/apis/ships path in the destination.
Workaround:
Simply set your GOPATH environment variable like:
export GOPATH="$HOME/go"
Possible Solutions:
- simply document the workaround that the GOPATH environment variable must be set
- maybe be specific in the
go:generatecommand line and add a-o - change the default behaviour for
deepcopy-gen? I don’t think this is a good idea though, it’s pretty clear how it should work
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 16 (8 by maintainers)
Commits related to this issue
- deepcopy-gen sets an output base When there is no GOPATH, deepcopy-gen must set the output base. Use the build package to determine what the appropriate GOPATH is regardless. Fixes #359 — committed to fraenkel/kubebuilder by fraenkel 6 years ago
- deepcopy-gen sets an output base When there is no GOPATH, deepcopy-gen must set the output base. Use the build package to determine what the appropriate GOPATH is regardless. Fixes #359 — committed to fraenkel/kubebuilder by fraenkel 6 years ago
- :bug: report error if GOPATH env is missing fixes #531 #359 — committed to droot/kubebuilder by droot 6 years ago
I just faced this, and the other issues about paths being all weird (such as NEEDing to be under
/usr/local/kubebuilder/bin(I had just copied them under my private~/bindirectory.)+1 for failing fast in the Makefile. As a pure user, and not contributor, I can and will fix everything you want in my environment, so long as I know what you want. 😃 I just always want to know what the tool needs and I’ll go make it happen.
Once I realized my mistake, I reworked the PR to only add the base when the GOPATH was missing. At the time I also noticed that the generate line wasn’t changing. So I decided to replicate your original issue first so I could see where things were really breaking. But that too proved to be a problem. I was running with no GOPATH but not hitting any issue. Like you I found a lot of little quirks, like the tools need to live in /usr/local and not on the PATH for certain commands. Anyhow, I haven’t gone back to replicate the error which I need to do first or else I don’t know if and when I am fixing the issue.