Skip to content

add Go modules support #971

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

Closed
wants to merge 5 commits into from
Closed
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
73 changes: 59 additions & 14 deletions build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"strings"
"time"

"github.com/visualfc/fastmod"

"github.com/fsnotify/fsnotify"
"github.com/gopherjs/gopherjs/compiler"
"github.com/gopherjs/gopherjs/compiler/gopherjspkg"
Expand Down Expand Up @@ -138,10 +140,10 @@ func Import(path string, mode build.ImportMode, installSuffix string, buildTags
wd = ""
}
bctx := NewBuildContext(installSuffix, buildTags)
return importWithSrcDir(*bctx, path, wd, mode, installSuffix)
return importWithSrcDir(*bctx, path, wd, mode, installSuffix, nil)
}

func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build.ImportMode, installSuffix string) (*PackageData, error) {
func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build.ImportMode, installSuffix string, mod *fastmod.Package) (*PackageData, error) {
// bctx is passed by value, so it can be modified here.
var isVirtual bool
switch path {
Expand All @@ -167,9 +169,19 @@ func importWithSrcDir(bctx build.Context, path string, srcDir string, mode build
mode |= build.IgnoreVendor
isVirtual = true
}
pkg, err := bctx.Import(path, srcDir, mode)
if err != nil {
return nil, err
var pkg *build.Package
var err error
if mod != nil && mod.IsValid() && !mod.IsStd() {
if _, dir, typ := mod.Lookup(path); typ != fastmod.PkgTypeNil {
srcDir = dir
pkg, err = bctx.ImportDir(srcDir, mode)
if err == nil {
pkg.ImportPath = path
}
}
}
if pkg == nil {
pkg, err = bctx.Import(path, srcDir, mode)
}

switch path {
Expand Down Expand Up @@ -484,6 +496,7 @@ type PackageData struct {
type Session struct {
options *Options
bctx *build.Context
mod *fastmod.Package
Archives map[string]*compiler.Archive
Types map[string]*types.Package
Watcher *fsnotify.Watcher
Expand All @@ -508,6 +521,7 @@ func NewSession(options *Options) (*Session, error) {
Archives: make(map[string]*compiler.Archive),
}
s.bctx = NewBuildContext(s.InstallSuffix(), s.options.BuildTags)
s.mod = fastmod.NewPackage(s.bctx)
s.Types = make(map[string]*types.Package)
if options.Watch {
if out, err := exec.Command("ulimit", "-n").Output(); err == nil {
Expand All @@ -525,6 +539,17 @@ func NewSession(options *Options) (*Session, error) {
return s, nil
}

func (s *Session) checkMod(pkg *PackageData) (err error) {
s.mod.Clear()
if !pkg.Goroot {
err := s.mod.LoadModule(pkg.Dir)
if err != nil {
return err
}
}
return nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I rather like this PR, but these two functions make the code rely on side-effects, which is quite error prone and brittle. Perhaps module info should be added to PackageData?

Note: I'm not a GopherJS maintainer, so my words carry limited weight :)

Copy link
Contributor Author

@visualfc visualfc Apr 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

246fd75
simplify mod check and clean code.


// BuildContext returns the session's build context.
func (s *Session) BuildContext() *build.Context { return s.bctx }

Expand All @@ -549,7 +574,7 @@ func (s *Session) BuildDir(packagePath string, importPath string, pkgObj string)
return err
}
pkg.JSFiles = jsFiles
archive, err := s.BuildPackage(pkg)
archive, err := s.buildPackage(pkg)
if err != nil {
return err
}
Expand All @@ -572,6 +597,10 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
Dir: packagePath,
},
}
err := s.checkMod(pkg)
if err != nil {
return err
}

for _, file := range filenames {
if strings.HasSuffix(file, ".inc.js") {
Expand All @@ -581,7 +610,7 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
pkg.GoFiles = append(pkg.GoFiles, file)
}

archive, err := s.BuildPackage(pkg)
archive, err := s.buildPackage(pkg)
if err != nil {
return err
}
Expand All @@ -592,20 +621,28 @@ func (s *Session) BuildFiles(filenames []string, pkgObj string, packagePath stri
}

func (s *Session) BuildImportPath(path string) (*compiler.Archive, error) {
_, archive, err := s.buildImportPathWithSrcDir(path, "")
_, archive, err := s.buildImportPathWithPackage(path, nil)
return archive, err
}

func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*PackageData, *compiler.Archive, error) {
pkg, err := importWithSrcDir(*s.bctx, path, srcDir, 0, s.InstallSuffix())
func (s *Session) buildImportPathWithPackage(path string, pkgData *PackageData) (*PackageData, *compiler.Archive, error) {
var srcDir string
var mod *fastmod.Package
if pkgData != nil {
srcDir = pkgData.Dir
if !pkgData.Goroot {
mod = s.mod
}
}
pkg, err := importWithSrcDir(*s.bctx, path, srcDir, 0, s.InstallSuffix(), mod)
if s.Watcher != nil && pkg != nil { // add watch even on error
s.Watcher.Add(pkg.Dir)
}
if err != nil {
return nil, nil, err
}

archive, err := s.BuildPackage(pkg)
archive, err := s.buildPackage(pkg)
if err != nil {
return nil, nil, err
}
Expand All @@ -614,6 +651,14 @@ func (s *Session) buildImportPathWithSrcDir(path string, srcDir string) (*Packag
}

func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
err := s.checkMod(pkg)
if err != nil {
return nil, err
}
return s.buildPackage(pkg)
}

func (s *Session) buildPackage(pkg *PackageData) (*compiler.Archive, error) {
if archive, ok := s.Archives[pkg.ImportPath]; ok {
return archive, nil
}
Expand Down Expand Up @@ -652,7 +697,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
if importedPkgPath == "unsafe" || ignored {
continue
}
importedPkg, _, err := s.buildImportPathWithSrcDir(importedPkgPath, pkg.Dir)
importedPkg, _, err := s.buildImportPathWithPackage(importedPkgPath, pkg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -709,7 +754,7 @@ func (s *Session) BuildPackage(pkg *PackageData) (*compiler.Archive, error) {
if archive, ok := localImportPathCache[path]; ok {
return archive, nil
}
_, archive, err := s.buildImportPathWithSrcDir(path, pkg.Dir)
_, archive, err := s.buildImportPathWithPackage(path, pkg)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -802,7 +847,7 @@ func (s *Session) WriteCommandPackage(archive *compiler.Archive, pkgObj string)
if archive, ok := s.Archives[path]; ok {
return archive, nil
}
_, archive, err := s.buildImportPathWithSrcDir(path, "")
_, archive, err := s.buildImportPathWithPackage(path, nil)
return archive, err
})
if err != nil {
Expand Down
38 changes: 38 additions & 0 deletions tests/gopherjsmodules_test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/sh
# Don't run this file directly. It's executed as part of TestGopherJSCanBeModules.

set -e

tmp=$(mktemp -d "${TMPDIR:-/tmp}/gopherjsmodules_test.XXXXXXXXXX")

cleanup() {
rm -rf "$tmp"
exit
}

trap cleanup EXIT HUP INT TERM

# Make a hello project that will Go modules GopherJS.
mkdir -p "$tmp/src/example.org/hello"
echo 'package main

import (
"github.com/gopherjs/gopherjs/js"
_ "github.com/gopherjs/websocket"
)

func main() {
js.Global.Get("console").Call("log", "hello using js pkg")
}' > "$tmp/src/example.org/hello/main.go"

# install gopherjs
go install github.com/gopherjs/gopherjs

# go mod init
(cd "$tmp/src/example.org/hello" && go mod init example.org/hello)

# go mod tidy
(cd "$tmp/src/example.org/hello" && go mod tidy)

# Use it to build and run the hello command.
(cd "$tmp/src/example.org/hello" && "$HOME/go/bin/gopherjs" run main.go)
17 changes: 17 additions & 0 deletions tests/gorepo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,20 @@ func TestGopherJSCanBeVendored(t *testing.T) {
t.Errorf("unexpected stdout from gopherjsvendored_test.sh:\ngot:\n%s\nwant:\n%s", got, want)
}
}

// Test that GopherJS can be Go modules into a project, and then used to build Go programs.
func TestGopherJSCanBeModules(t *testing.T) {
if runtime.GOARCH == "js" {
t.Skip("test meant to be run using normal Go compiler (needs os/exec)")
}

cmd := exec.Command("sh", "gopherjsmodules_test.sh")
cmd.Stderr = os.Stdout
got, err := cmd.Output()
if err != nil {
t.Fatal(err)
}
if want := "hello using js pkg\n"; string(got) != want {
t.Errorf("unexpected stdout from gopherjsmodules_test.sh:\ngot:\n%s\nwant:\n%s", got, want)
}
}