From 52f666a5ac4160368efde1156553eab93f29acad Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 4 May 2022 22:41:16 +0200 Subject: [PATCH] goprocess: use debug/buildinfo on Go 1.18 and newer Use package debug/buildinfo [1] introduced in Go 1.18 [2] when possible. This should avoid previous issues with rsc.io/goversion where Go processes were no longer listed. [1] https://pkg.go.dev/debug/buildinfo [2] https://go.dev/doc/go1.18#debug/buildinfo For #102 For #159 For #160 --- goprocess/{gp.go => goprocess.go} | 6 +----- goprocess/goprocess_1.18.go | 18 ++++++++++++++++++ goprocess/goprocess_lt1.18.go | 18 ++++++++++++++++++ goprocess/{gp_test.go => goprocess_test.go} | 0 4 files changed, 37 insertions(+), 5 deletions(-) rename goprocess/{gp.go => goprocess.go} (95%) create mode 100644 goprocess/goprocess_1.18.go create mode 100644 goprocess/goprocess_lt1.18.go rename goprocess/{gp_test.go => goprocess_test.go} (100%) diff --git a/goprocess/gp.go b/goprocess/goprocess.go similarity index 95% rename from goprocess/gp.go rename to goprocess/goprocess.go index 8a53d945..18fa051d 100644 --- a/goprocess/gp.go +++ b/goprocess/goprocess.go @@ -9,8 +9,6 @@ import ( "os" "sync" - goversion "rsc.io/goversion/version" - "github.com/google/gops/internal" ps "github.com/keybase/go-ps" ) @@ -119,13 +117,11 @@ func isGo(pr ps.Process) (path, version string, agent, ok bool, err error) { if err != nil { return } - var versionInfo goversion.Version - versionInfo, err = goversion.ReadExe(path) + version, err = goVersion(path) if err != nil { return } ok = true - version = versionInfo.Release pidfile, err := internal.PIDFile(pr.Pid()) if err == nil { _, err := os.Stat(pidfile) diff --git a/goprocess/goprocess_1.18.go b/goprocess/goprocess_1.18.go new file mode 100644 index 00000000..30c36985 --- /dev/null +++ b/goprocess/goprocess_1.18.go @@ -0,0 +1,18 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build go1.18 +// +build go1.18 + +package goprocess + +import "debug/buildinfo" + +func goVersion(path string) (string, error) { + info, err := buildinfo.ReadFile(path) + if err != nil { + return "", err + } + return info.GoVersion, nil +} diff --git a/goprocess/goprocess_lt1.18.go b/goprocess/goprocess_lt1.18.go new file mode 100644 index 00000000..b06e05f4 --- /dev/null +++ b/goprocess/goprocess_lt1.18.go @@ -0,0 +1,18 @@ +// Copyright 2022 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build !go1.18 +// +build !go1.18 + +package goprocess + +import goversion "rsc.io/goversion/version" + +func goVersion(path string) (string, error) { + versionInfo, err := goversion.ReadExe(path) + if err != nil { + return "", err + } + return versionInfo.Release, nil +} diff --git a/goprocess/gp_test.go b/goprocess/goprocess_test.go similarity index 100% rename from goprocess/gp_test.go rename to goprocess/goprocess_test.go