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

Improve runtime/debug Go 1.18 incompatibility via stable go.mod file parsing #129

Closed
svengreb opened this issue Nov 15, 2022 · 0 comments · Fixed by #130
Closed

Improve runtime/debug Go 1.18 incompatibility via stable go.mod file parsing #129

svengreb opened this issue Nov 15, 2022 · 0 comments · Fixed by #130

Comments

@svengreb
Copy link
Owner

svengreb commented Nov 15, 2022

As of Go 1.18 the debug.ReadBuildInfo function does not work for Mage executables anymore because the way how module information is stored changed. Therefore the fields of the returned debug.Module type only has zero values, including the module path. The debug.Module.Version field has a default value ((devel)) which is not Semver compatible and causes the parsing to fail. The change in Go 1.18 also came with the new debug/buildinfo package which allows to read the information from compiled binaries while the runtime/debug.ReadBuildInfo function returns information from within the running binary. Both are not suitable anymore which is also described in the Go 1.18 version command release notes:

The underlying data format of the embedded build information can change with new go releases, so an older version of go may not handle the build information produced with a newer version of go. To read the version information from a binary built with go 1.18, use the go version command and the debug/buildinfo package from go 1.18+.

To get the required module information that was previously provided by the runtime/debug package the official golang.org/x/mod/modfile package will be used instead that provides the implementation for a parser and formatter for go.mod files 1. This allows to safely get the module path without the need to depend on runtime/dynamic logic that might change in future Go versions.

Note that this change will also increase the minimum Go version from 1.17 to 1.19!

Footnotes

  1. https://go.dev/ref/mod#go-mod-file

@svengreb svengreb added this to the version-next milestone Nov 15, 2022
@svengreb svengreb self-assigned this Nov 15, 2022
svengreb added a commit that referenced this issue Nov 15, 2022
As of Go 1.18 [1] the debug.ReadBuildInfo [2] function does not work for
Mage executables anymore because the way how module information is
stored changed. Therefore the fields of the returned debug.Module [3]
type only has zero values, including the module path.
The debug.Module.Version [4] field has a default value [5] (`(devel)`)
which is not Semver compatible and causes the parsing to fail.
The change in Go 1.18 [9] also came with the new `debug/buildinfo`
package [10] which allows to read the information from compiled binaries
while the `runtime/debug.ReadBuildInfo` function returns information
from within the running binary. Both are not suitable anymore which is
also described in the Go 1.18 `version` command release notes:

  "The underlying data format of the embedded build information can
  change with new `go` releases, so an older version of `go` may not
  handle the build information produced with a newer version of `go`.
  To read the version information from a binary built with `go` 1.18,
  use the `go` version command and the `debug/buildinfo` package from
  `go` 1.18+."

To get the required module information that was previously provided by
the runtime/debug [6] package the official golang.org/x/mod/modfile [7]
package is now used instead that provides the implementation for a
parser and formatter for `go.mod` files [8] [^1]. This allows to safely
get the module path without the need to depend on runtime/dynamic logic
that might change in future Go versions.

Note that this change also increased the minimum Go version from `1.17`
to `1.19`!

[1]: golang/go@9cec77ac#diff-abdadaf0d85a2e6c8e45da716909b2697d830b0c75149b9e35accda9c38622bdR2234
[2]: https://pkg.go.dev/runtime/debug@go1.18#ReadBuildInfo
[3]: https://pkg.go.dev/runtime/debug#Module
[4]: https://github.com/golang/go/blob/9cec77ac/src/runtime/debug/mod.go#L52
[5]: https://github.com/golang/go/blob/122a22e0e9eba7fe712030d429fc4bcf6f447f5e/src/cmd/go/internal/load/pkg.go#L2288
[6]: https://pkg.go.dev/runtime/debug@go1.18.8
[7]: https://pkg.go.dev/golang.org/x/mod/modfile
[8]: https://pkg.go.dev/cmd/go#hdr-The_go_mod_file
[9]: https://tip.golang.org/doc/go1.18#go-version
[10]: https://tip.golang.org/doc/go1.18#debug/buildinfo

[^1]: https://go.dev/ref/mod#go-mod-file

GH-129
svengreb added a commit that referenced this issue Nov 15, 2022
…130)

As of Go 1.18 [1] the debug.ReadBuildInfo [2] function does not work for
Mage executables anymore because the way how module information is
stored changed. Therefore the fields of the returned debug.Module [3]
type only has zero values, including the module path.
The debug.Module.Version [4] field has a default value [5] (`(devel)`)
which is not Semver compatible and causes the parsing to fail.
The change in Go 1.18 [9] also came with the new `debug/buildinfo`
package [10] which allows to read the information from compiled binaries
while the `runtime/debug.ReadBuildInfo` function returns information
from within the running binary. Both are not suitable anymore which is
also described in the Go 1.18 `version` command release notes:

  "The underlying data format of the embedded build information can
  change with new `go` releases, so an older version of `go` may not
  handle the build information produced with a newer version of `go`.
  To read the version information from a binary built with `go` 1.18,
  use the `go` version command and the `debug/buildinfo` package from
  `go` 1.18+."

To get the required module information that was previously provided by
the runtime/debug [6] package the official golang.org/x/mod/modfile [7]
package is now used instead that provides the implementation for a
parser and formatter for `go.mod` files [8] [^1]. This allows to safely
get the module path without the need to depend on runtime/dynamic logic
that might change in future Go versions.

Note that this change also increased the minimum Go version from `1.17`
to `1.19`!

[1]: golang/go@9cec77ac#diff-abdadaf0d85a2e6c8e45da716909b2697d830b0c75149b9e35accda9c38622bdR2234
[2]: https://pkg.go.dev/runtime/debug@go1.18#ReadBuildInfo
[3]: https://pkg.go.dev/runtime/debug#Module
[4]: https://github.com/golang/go/blob/9cec77ac/src/runtime/debug/mod.go#L52
[5]: https://github.com/golang/go/blob/122a22e0e9eba7fe712030d429fc4bcf6f447f5e/src/cmd/go/internal/load/pkg.go#L2288
[6]: https://pkg.go.dev/runtime/debug@go1.18.8
[7]: https://pkg.go.dev/golang.org/x/mod/modfile
[8]: https://pkg.go.dev/cmd/go#hdr-The_go_mod_file
[9]: https://tip.golang.org/doc/go1.18#go-version
[10]: https://tip.golang.org/doc/go1.18#debug/buildinfo

[^1]: https://go.dev/ref/mod#go-mod-file

GH-129
@svengreb svengreb removed their assignment Nov 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant