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
runtime/debug: allow JSON encoding of BuildInfo
#51026
Comments
Change https://golang.org/cl/382274 mentions this issue: |
At work we print this information as part of a diagnostics file. type Diagnostics struct {
BuildInfo *debug.BuildInfo
[...]
}
func Write(w io.Writer) error {
d := Diagnostics{ ... }
buf, err := json.Marshal(d)
[...]
} We use JSON because it's easy for both humans and machines to parse. The new format makes the JSON output inscrutable for humans and breaks machine parsing. While there wasn't any guarantee that It would be nice if the fix could make it into 1.18. cc: @jayconrod |
Add -json flag to output in JSON format. Include the Go version info - go1.18: runtime/debug.BuildInfo.GoVersion - pre go1.18: runtime.Version Restructure ServiceVersion so we embed info from runtime/debug.BuildInfo. Instead of directly using runtime/debug.BuildInfo, we use our own BuildInfo type. That allows: for go1.17 or older versions, we can add GoVersion. for go1.18, we can drop MarshalText that prevents JSON encoding other languages and human can understand (golang/go#51026) For golang/go#49783 Change-Id: Ia5ab50ce1f5e6c3a912654834785ecea7f5034e2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/382274 Trust: Hyang-Ah Hana Kim <hyangah@gmail.com> Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com> gopls-CI: kokoro <noreply+kokoro@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
It's unfortunate that But maybe it would be better to add some form of escaping and switch to |
BuildInfo could also implement func (b BuildInfo) MarshalJSON() ([]byte, error) {
type buildInfo BuildInfo // Mask out the methods
return json.Marshal(buildInfo(b))
} |
It could, if it were in an ordinary package. Unfortunately, it is in So if we provided a |
Perhaps a targeted hack in JSON marshaler will help? |
Change https://go.dev/cl/384154 mentions this issue: |
Change https://go.dev/cl/384161 mentions this issue: |
Updates #51026 Change-Id: Id7af2ffa8c99970274b2a2b12622d986ea105b1b Reviewed-on: https://go-review.googlesource.com/c/go/+/384161 TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Russ Cox <rsc@golang.org> Auto-Submit: Bryan Mills <bcmills@google.com> Run-TryBot: Bryan Mills <bcmills@google.com>
go1.18 added
MarshalText
andUnmarshalText
methods forBuildInfo
.https://pkg.go.dev/runtime/debug@go1.18beta2#BuildInfo
This addition changed the behavior of this program.
With go1.17
With go1.18
My proposal is either drop
MarshalText
/UnmarshalText
(insteadString/Parse
,Encode/Decode
?), or provideMarshalJSON
/UnmarshalJSON
so JSON encoding can encode in a form that tools written in other languages can access the info easily.The text was updated successfully, but these errors were encountered: