From 440ef8c4d24861cbfb01758e7e03fa3b4164f55a Mon Sep 17 00:00:00 2001 From: Dmitri Shuralyov Date: Wed, 4 Jan 2023 20:54:34 -0500 Subject: [PATCH] cmd/internal/osinfo: report Node.js version Seeing the Node.js version that was used during a particular test run should be helpful during the upcoming migration from Node.js 14 to 18. Add minimal support for that. For golang/go#57614. Change-Id: Id55ba25a7ee4a803788316d4a646cd4b6f4297e9 Reviewed-on: https://go-review.googlesource.com/c/go/+/460655 Reviewed-by: Dmitri Shuralyov TryBot-Result: Gopher Robot Run-TryBot: Dmitri Shuralyov Reviewed-by: Michael Pratt Reviewed-by: Richard Musiol Reviewed-by: Johan Brandhorst-Satzkorn Auto-Submit: Dmitri Shuralyov --- src/cmd/internal/osinfo/os_js.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/src/cmd/internal/osinfo/os_js.go b/src/cmd/internal/osinfo/os_js.go index 882580d652..f4f03aa312 100644 --- a/src/cmd/internal/osinfo/os_js.go +++ b/src/cmd/internal/osinfo/os_js.go @@ -8,14 +8,31 @@ package osinfo import ( "fmt" + "syscall/js" ) // Version returns the OS version name/number. func Version() (string, error) { - // Version detection on wasm varies depending on the underlying runtime + // Version detection on Wasm varies depending on the underlying runtime // (browser, node, etc), nor is there a standard via something like - // WASI (see https://go.dev/issue/31105). We could attempt multiple - // combinations, but for now we leave this unimplemented for - // simplicity. - return "", fmt.Errorf("unimplemented") + // WASI (see https://go.dev/issue/31105). For now, attempt a few simple + // combinations for the convenience of reading logs at build.golang.org + // and local development. It's not a goal to recognize all environments. + if v, ok := node(); ok { + return "Node.js " + v, nil + } + return "", fmt.Errorf("unrecognized environment") +} + +func node() (version string, ok bool) { + // Try the https://nodejs.org/api/process.html#processversion API. + p := js.Global().Get("process") + if p.IsUndefined() { + return "", false + } + v := p.Get("version") + if v.IsUndefined() { + return "", false + } + return v.String(), true } -- 2.50.0