Skip to content

runtime: Version() returns Go version provided by the compiler. #1065

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

Merged
merged 3 commits into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,11 @@ func (s *Session) InstallSuffix() string {
return ""
}

// GoRelease returns Go release version this session is building with.
func (s *Session) GoRelease() string {
return compiler.GoRelease(s.options.GOROOT)
}

func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string) error {
if s.Watcher != nil {
s.Watcher.Add(packagePath)
Expand Down Expand Up @@ -778,7 +783,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
if err != nil {
return err
}
return compiler.WriteProgramCode(deps, sourceMapFilter)
return compiler.WriteProgramCode(deps, sourceMapFilter, s.GoRelease())
}

func NewMappingCallback(m *sourcemap.Map, goroot, gopath string, localMap bool) func(generatedLine, generatedColumn int, originalPos token.Position) {
Expand Down
2 changes: 1 addition & 1 deletion circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ workflows:
parameters:
go_version:
type: string
default: "1.17"
default: "1.17.1"
nvm_version:
type: string
default: "0.38.0"
Expand Down
9 changes: 6 additions & 3 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ type dceInfo struct {
methodFilter string
}

func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) error {
func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter, goVersion string) error {
mainPkg := pkgs[len(pkgs)-1]
minify := mainPkg.Minified

Expand Down Expand Up @@ -242,6 +242,10 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) error {
if _, err := w.Write([]byte("\"use strict\";\n(function() {\n\n")); err != nil {
return err
}
if _, err := w.Write([]byte(fmt.Sprintf("var $goVersion = %q;\n", goVersion))); err != nil {
return err
}

preludeJS := prelude.Prelude
if minify {
preludeJS = prelude.Minified
Expand All @@ -260,10 +264,9 @@ func WriteProgramCode(pkgs []*Archive, w *SourceMapFilter) error {
}
}

if _, err := w.Write([]byte("$synthesizeMethods();\n$initAllLinknames();var $mainPkg = $packages[\"" + string(mainPkg.ImportPath) + "\"];\n$packages[\"runtime\"].$init();\n$go($mainPkg.$init, []);\n$flushConsole();\n\n}).call(this);\n")); err != nil {
if _, err := w.Write([]byte("$synthesizeMethods();\n$initAllLinknames();\nvar $mainPkg = $packages[\"" + string(mainPkg.ImportPath) + "\"];\n$packages[\"runtime\"].$init();\n$go($mainPkg.$init, []);\n$flushConsole();\n\n}).call(this);\n")); err != nil {
return err
}

return nil
}

Expand Down
12 changes: 6 additions & 6 deletions compiler/natives/fs_vfsdata.go

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions compiler/natives/src/runtime/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ func init() {
js.Global.Set("$jsObjectPtr", jsPkg.Get("Object").Get("ptr"))
js.Global.Set("$jsErrorPtr", jsPkg.Get("Error").Get("ptr"))
js.Global.Set("$throwRuntimeError", js.InternalObject(throw))
buildVersion = js.Global.Get("$goVersion").String()
// avoid dead code elimination
var e error
e = &TypeAssertionError{}
Expand Down Expand Up @@ -353,9 +354,10 @@ func LockOSThread() {}

func UnlockOSThread() {}

var buildVersion string // Set by init()

func Version() string {
// TODO: Make this smarter
return "go1.17rc1"
return buildVersion
}

func StartTrace() error { return nil }
Expand Down
56 changes: 50 additions & 6 deletions compiler/version_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
package compiler

import (
"bytes"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
)

// Version is the GopherJS compiler version string.
const Version = "1.17.0+go1.17"
const Version = "1.17.0+go1.17.1"

// GoVersion is the current Go 1.x version that GopherJS is compatible with.
const GoVersion = 17
Expand All @@ -25,12 +25,56 @@ func CheckGoVersion(goroot string) error {
if nvc, err := strconv.ParseBool(os.Getenv("GOPHERJS_SKIP_VERSION_CHECK")); err == nil && nvc {
return nil
}
v, err := ioutil.ReadFile(filepath.Join(goroot, "VERSION"))
v, err := goRootVersion(goroot)
if err != nil {
return fmt.Errorf("GopherJS %s requires a Go 1.%d.x distribution, but failed to read its VERSION file: %v", Version, GoVersion, err)
return fmt.Errorf("unable to detect Go version for %q: %w", goroot, err)
}
if !bytes.HasPrefix(v, []byte("go1."+strconv.Itoa(GoVersion))) {
if !strings.HasPrefix(v, "go1."+strconv.Itoa(GoVersion)) {
return fmt.Errorf("GopherJS %s requires a Go 1.%d.x distribution, but found version %s", Version, GoVersion, v)
}
return nil
}

// goRootVersion defermines Go release for the given GOROOT installation.
func goRootVersion(goroot string) (string, error) {
v, err := os.ReadFile(filepath.Join(goroot, "VERSION"))
if err == nil {
// Standard Go distribution has VERSION file inside its GOROOT, checking it
// is the most efficient option.
return string(v), nil
}

// Fall back to the "go version" command.
cmd := exec.Command(filepath.Join(goroot, "bin", "go"), "version")
out, err := cmd.Output()
if err != nil {
return "", fmt.Errorf("`go version` command failed: %w", err)
}
// Expected output: go version go1.17.1 linux/amd64
parts := strings.Split(string(out), " ")
if len(parts) != 4 {
return "", fmt.Errorf("unexpected `go version` output %q, expected 4 words", string(out))
}
return parts[2], nil
}

// GoRelease does a best-effort to identify Go release we are building with.
// If unable to determin the precise version for the given GOROOT, falls back
// to the best guess available.
func GoRelease(goroot string) string {
v, err := goRootVersion(goroot)
if err == nil {
// Prefer using the actual version of the GOROOT we are working with.
return v
}

// Use Go version GopherJS release was tested against as a fallback. By
// convention, it is included in the GopherJS version after the plus sign.
parts := strings.Split(Version, "+")
if len(parts) == 2 {
return parts[1]
}

// If everything else fails, return just the Go version without patch level.
return fmt.Sprintf("go1.%d", GoVersion)
}
28 changes: 28 additions & 0 deletions compiler/version_check_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package compiler

import (
"runtime"
"strings"
"testing"
)

func TestGoRelease(t *testing.T) {
t.Run("goroot", func(t *testing.T) {
got := GoRelease(runtime.GOROOT())
want := runtime.Version()
if got != want {
t.Fatalf("Got: goRelease(%q) returned %q. Want %s.", runtime.GOROOT(), got, want)
}
})

t.Run("fallback", func(t *testing.T) {
const goroot = "./invalid goroot"
got := GoRelease(goroot)
if got == "" {
t.Fatalf("Got: goRelease(%q) returned \"\". Want: a Go version.", goroot)
}
if !strings.HasSuffix(Version, "+"+got) {
t.Fatalf("Got: goRelease(%q) returned %q. Want: a fallback to GopherJS version suffix %q.", goroot, got, Version)
}
})
}
6 changes: 6 additions & 0 deletions tests/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -856,3 +856,9 @@ func TestUntypedNil(t *testing.T) {
f(nil)
}
}

func TestVersion(t *testing.T) {
if got := runtime.Version(); !strings.HasPrefix(got, "go1.") {
t.Fatalf("Got: runtime.Version() returned %q. Want: a valid Go version.", got)
}
}
2 changes: 1 addition & 1 deletion tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ func (fs serveCommandFileSystem) Open(requestName string) (http.File, error) {
if err != nil {
return err
}
if err := compiler.WriteProgramCode(deps, sourceMapFilter); err != nil {
if err := compiler.WriteProgramCode(deps, sourceMapFilter, s.GoRelease()); err != nil {
return err
}

Expand Down