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

Add Mockgen Version Flag #362

Merged
merged 3 commits into from
Jan 20, 2020
Merged

Add Mockgen Version Flag #362

merged 3 commits into from
Jan 20, 2020

Conversation

minicuts
Copy link
Contributor

Add -version flag that shows a version provided by Go modules.

This strategy works well for module aware invocations of go get and go run (which means using GO111MODULE=on).

If we ever plan to distribute built binaries, it is necessary to amend
the strategy with build flags.

GOPATH mode versioning is unsupported.

Add -version flag that shows a version provided by Go modules.

This strategy works well for module aware invocations of `go get` and `go
run` (which means using GO111MODULE=on).

If we ever plan to distribute built binaries, it is necessary to amend
the strategy with build flags.

GOPATH mode versioning is unsupported.
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
mockgen/version.1.11.go Outdated Show resolved Hide resolved
mockgen/version.1.12.go Outdated Show resolved Hide resolved

func printVersion() {
if bi, exists := debug.ReadBuildInfo(); exists {
fmt.Println(bi.Main.Version)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't seen this pattern before (usually see a const str). Does this return the version metadata of the go module? Does that mean it won't work unless installed via module support? I'm guessing that's what the build checks and error messaging are about.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not leaning strongly towards the implementation in this PR, but I tend to think it's the best out of the three options that I have considered:

  1. using constant string
  2. using build flags
  3. using debug.ReadBuildInfo

Each option has pros and cons.

Problem with constant string is that any time somebody pulls master version X (not the latest version) of mockgen with go get, they will always end up with mockgen that includes all the commits since X but the version string is unchanged (the binary would report same version for binaries with different commits, which is exactly what we dont want).

Problem with build flags is that they won't work unless we distribute binaries. And I see that current assumption is that people use go get to get the mockgen

Problem with debug.ReadBuildInfo is that it relies on using modules.

I lean towards using debug.ReadBuildInfo. I think it should be the best practice for the users of mockgen to always use predictable version, especially for CI builds. That was the original problem in #250. Thus I think that users should not run go get in GOPATH mode and they should use @ to specify which version of mockgen they want to use. Note that the version info is embedded in the binary, so after go get is used to download the binary the clients can copy the mockgen out of the GOPATH and -version should still work.

As I mentioned in the description of this PR (the GOPATH mode versioning is unsupported) this wont work unless installed via module support

WDYT?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem with constant string is that any time somebody pulls master version X (not the latest version) of mockgen with go get, they will always end up with mockgen that includes all the commits since X but the version string is unchanged (the binary would report same version for binaries with different commits, which is exactly what we dont want).

I've always used a constant string in projects but I haven't thought about the UX of pulling master in that case. I don't think I have a strong opinion and I don't disagree with the point you've made.

I don't think there is anything wrong with making module support a requirement for the version command to work; as long as we fall back to some other behavior when module support is not enabled.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like there is some nice fallback behavior using the build tags for different versions of go :👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cvgw wrt build tags. Are you referring to the // +build go1.12 tag that has been in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've always used a constant string in projects but I haven't thought about the UX of pulling master in that case. I don't think I have a strong opinion and I don't disagree with the point you've made.

Yeah I agree. Constant string is a good solution if you build and distribute the binary. But! :-) If we did build and distribute the binary, it think there is even better solution than version string. The better solution is to use -ldflags with go build command. That solution allows you to set a value to global property in your golang program during build time.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cvgw wrt build tags. Are you referring to the // +build go1.12 tag that has been in this PR?

yes

The better solution is to use -ldflags with go build command. That solution allows you to set a value to global property in your golang program during build time.

I realized that's what we do during compile on another project I help on https://github.com/GoogleContainerTools/kaniko/blob/master/Makefile#L30

@minicuts minicuts self-assigned this Jan 7, 2020
minicuts and others added 2 commits January 7, 2020 13:39
Co-Authored-By: Cody Oss <6331106+codyoss@users.noreply.github.com>
Co-Authored-By: Cody Oss <6331106+codyoss@users.noreply.github.com>
@minicuts minicuts requested review from codyoss and cvgw January 7, 2020 15:20
README.md Show resolved Hide resolved
Copy link
Collaborator

@cvgw cvgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

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

Successfully merging this pull request may close these issues.

3 participants