Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proposal: runtime/debug: embed vcs info into runtime/debug.BuildInfo when running go install #67745

Closed
MakDon opened this issue May 31, 2024 · 6 comments
Labels
Milestone

Comments

@MakDon
Copy link
Contributor

MakDon commented May 31, 2024

Proposal Details

why

When we go install a pkg, we could not get the vcs info from runtime/debug.BuildSetting

So we could not get the binary version that installed from go install:

➜  git:(master) go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-grpc-gateway@cc70731
go: downloading github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.1-0.20240523165541-cc70731f9bd2
➜  git:(master) protoc-gen-grpc-gateway --version                                                   
commit unknown, built at unknown

And it is how the code does:

if commit == "unknown" {
  buildInfo, ok := debug.ReadBuildInfo()
  if ok {
    for _, setting := range buildInfo.Settings {
      if setting.Key == "vcs.revision" {
        commit = setting.Value
      }
      if setting.Key == "vcs.time" {
        date = setting.Value
      }
    }
    fmt.Printf("commit %v, built at %v\n", commit, date)
    os.Exit(0)
  }
}

It would be nice if we could embed the vcs info and build time into the binary, so we could get the version and build time when installed from go install

Please refer to this issue for more detail

How

I am still not sure if it could work but I would try later.

This is part of stack trace when we run go install:

installOutsideModule
PackagesAndErrorsOutsideModule
PackagesAndErrors
loadImport
load
setBuildInfo

I wonder if we could update this opt
in installOutsideModulefrom

pkgOpts := load.PackageOpts{MainOnly: true}

to

pkgOpts := load.PackageOpts{MainOnly: true, AutoVCS: true}

so that this piece of code could embed the vcs info into BuildInfo

And maybe it will be nice if we could have a build time in runtime/debug.BuildInfo as well.


If this proposal is accepted I would try to make a PR for it.
yours sincerely.

@gopherbot gopherbot added this to the Proposal milestone May 31, 2024
@seankhliao
Copy link
Member

in general, downloading a module strips any vcs info (e.g. a module zip from the proxy doesn't contain any git history). what you have instead is the modules' (pseudo)version, from which you may be able to infer an a commit in the vcs, but it's not a part of the module.
Build time is not something we would include as it makes binaries non reproducible by default.

I'm going to close this as infeasible.

@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale May 31, 2024
@prattmic
Copy link
Member

The info endpoint added VCS info fairly recently if I recall correctly. For example, see https://proxy.golang.org/golang.org/x/text/@v/v0.15.0.info.

So it seems that this might be feasible?

@seankhliao
Copy link
Member

my understanding from #44742 (comment) was that the vcs info there was best-effort / non-canonical aid in debugging?

@MakDon
Copy link
Contributor Author

MakDon commented May 31, 2024

my understanding from #44742 (comment) was that the vcs info there was best-effort / non-canonical aid in debugging?

IMHO, the same "unauthenticated" vcs info is helpful enough, for debugging.

In this grpc-gateway's case, we need the vcs info because different version generates different stub code, so it would be nice if we could get the locate binary version and share it to my teammate.

The other generators may meet the same issue that "different binary generates differently, but I dont know which version I am using and could not align with my teammate"

Absolutely the author of the tools could manually update a version variable in the code every release, but it seems not elegant enough.

As module .info provides an "unauthenticated" just for debugging, it seems that we could embed it into the binary for the same reason.

@seankhliao
Copy link
Member

runtime/debug.BuildInfo.Main.Version should be automatically set if it was from go install pkg@version

@MakDon
Copy link
Contributor Author

MakDon commented May 31, 2024

runtime/debug.BuildInfo.Main.Version should be automatically set if it was from go install pkg@version

Thanks very much. I would have a look on this.
It seems that we could parse the runtime/debug.BuildInfo.Main.Version into <tag>-<Commit time>-<Commit hash>, which satisfies all we need

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

No branches or pull requests

4 participants