Repository: motemen/gore
Pull request #164 (2019-12-20 06:26:33)

Help PR to support go module
the Old PR is pending in test failed, I have investigated the issue, and located the what's going wrong there, but it is no response from contributor. https://github.com/motemen/gore/pull/151#discussion_r358791369
so just create new PR for passed test.


Patch:
diff --git a/.gitignore b/.gitignore
index 162e369..796b96d 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,1 +1,1 @@
-/gore
+/build
diff --git a/Makefile b/Makefile
index 7250f9f..16596c3 100644
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ all: clean build
 
 .PHONY: build
 build:
-	go build -ldflags=$(BUILD_LDFLAGS) -o $(BIN) ./cmd/...
+	go build -ldflags=$(BUILD_LDFLAGS) -o build/$(BIN) ./cmd/...
 
 .PHONY: install
 install:
@@ -25,9 +25,9 @@ lint: $(GOBIN)/golint
 	golint -set_exit_status ./...
 
 $(GOBIN)/golint:
-	cd && go get golang.org/x/lint/golint
+	GO111MODULE=off go get golang.org/x/lint/golint
 
 .PHONY: clean
 clean:
-	rm -f $(BIN)
+	rm -rf build
 	go clean
diff --git a/README.md b/README.md
index 6a2627a..b8c82f1 100644
--- a/README.md
+++ b/README.md
@@ -47,7 +47,7 @@ Some functionalities are provided as commands in the REPL:
 The gore command requires Go tool-chains on runtime, so standalone binary is not distributed.
 
 ```sh
-go get -u github.com/motemen/gore/cmd/gore
+GO111MODULE=off go get -u github.com/motemen/gore/cmd/gore
 ```
 
 Make sure `$GOPATH/bin` is in your `$PATH`.
@@ -55,8 +55,8 @@ Make sure `$GOPATH/bin` is in your `$PATH`.
 Also recommended:
 
 ```sh
-go get -u github.com/mdempsky/gocode   # for code completion
-go get -u github.com/k0kubun/pp        # or github.com/davecgh/go-spew/spew
+GO111MODULE=off go get -u github.com/mdempsky/gocode   # for code completion
+GO111MODULE=off go get -u github.com/k0kubun/pp        # or github.com/davecgh/go-spew/spew
 ```
 
 ## FAQ/Caveats
diff --git a/commands.go b/commands.go
index 49a851e..1704141 100644
--- a/commands.go
+++ b/commands.go
@@ -18,6 +18,7 @@ import (
 	"go/types"
 
 	"golang.org/x/tools/go/ast/astutil"
+	"golang.org/x/tools/go/packages"
 )
 
 type command struct {
@@ -104,7 +105,8 @@ func actionImport(s *Session, arg string) error {
 	path := strings.Trim(arg, `"`)
 
 	// check if the package specified by path is importable
-	_, err := s.types.Importer.Import(path)
+	_, err := packages.Load(&packages.Config{}, path)
+	// the importer cannot check go mod package, skip
 	if err != nil {
 		return err
 	}
@@ -346,11 +348,7 @@ func actionDoc(s *Session, in string) error {
 	}
 
 	godoc := exec.Command("go", args...)
-	godoc.Dir = s.tempDir
-	godoc.Env = append(os.Environ(), "GO111MODULE=on")
-	ef := newErrFilter(s.stderr)
-	godoc.Stderr = ef
-	defer ef.Close()
+	godoc.Stderr = s.stderr
 
 	// TODO just use PAGER?
 	if pagerCmd := os.Getenv("GORE_PAGER"); pagerCmd != "" {
diff --git a/errfilter.go b/errfilter.go
index 28c67a9..9eb2947 100644
--- a/errfilter.go
+++ b/errfilter.go
@@ -47,9 +47,6 @@ func replaceErrMsg(p []byte) []byte {
 	if bytes.HasPrefix(p, []byte("# command-line-arguments")) {
 		return nil
 	}
-	if bytes.HasPrefix(p, []byte(`warning: pattern "all" matched no module dependencies`)) {
-		return nil
-	}
 	if i := bytes.Index(p, []byte("gore_session.go")); i >= 0 {
 		if j := bytes.IndexRune(p[i:], ' '); j >= 0 {
 			return p[i+j+1:]
diff --git a/go.mod b/go.mod
index fccae44..d48aa6b 100644
--- a/go.mod
+++ b/go.mod
@@ -3,10 +3,10 @@ module github.com/motemen/gore
 go 1.12
 
 require (
-	github.com/mattn/go-runewidth v0.0.7 // indirect
+	github.com/mitchellh/go-homedir v1.1.0
 	github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679
 	github.com/peterh/liner v1.1.0
 	github.com/stretchr/testify v1.3.0
 	golang.org/x/text v0.3.2
-	golang.org/x/tools v0.0.0-20191212051200-825cb0626375
+	golang.org/x/tools v0.0.0-20190717194535-128ec6dfca09
 )
diff --git a/go.sum b/go.sum
index 22d952a..e43d367 100644
--- a/go.sum
+++ b/go.sum
@@ -2,8 +2,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/mattn/go-runewidth v0.0.3 h1:a+kO+98RDGEfo6asOGMmpodZq4FNtnGP54yps8BzLR4=
 github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
-github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
-github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
+github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
+github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
 github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679 h1:UkBp8kp/ChZOCNV1nyiKpfufVkTdYU32saV0Y6iQgKk=
 github.com/motemen/go-quickfix v0.0.0-20160413151302-5c522febc679/go.mod h1:MIkbIO1YK5LNJNfBzYUstnQr/eQTEFKzlBuRsOkknUI=
 github.com/peterh/liner v1.1.0 h1:f+aAedNJA6uk7+6rXsYBnhdo4Xux7ESLe+kcuVUF5os=
@@ -21,6 +21,5 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
 golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
 golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
-golang.org/x/tools v0.0.0-20191212051200-825cb0626375 h1:hrBCt+pkb1irxFjg/GeadSn24gLxi5/Z861I53OkQbc=
-golang.org/x/tools v0.0.0-20191212051200-825cb0626375/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
-golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
+golang.org/x/tools v0.0.0-20190717194535-128ec6dfca09 h1:4E8vPHnP5LziSZdudGZpi697p5agtemBV2l/jWYmG+M=
+golang.org/x/tools v0.0.0-20190717194535-128ec6dfca09/go.mod h1:jcCCGcm9btYwXyDqrUWc6MKQKKGJCWEQ3AfLSRIbEuI=
diff --git a/gore.go b/gore.go
index 0a5b621..80c53c7 100644
--- a/gore.go
+++ b/gore.go
@@ -6,6 +6,8 @@ import (
 	"os"
 	"path/filepath"
 	"strings"
+
+	"github.com/mitchellh/go-homedir"
 )
 
 // Version of gore.
@@ -139,7 +141,7 @@ func homeDir() (home string, err error) {
 		return
 	}
 
-	home, err = os.UserHomeDir()
+	home, err = homedir.Dir()
 	if err != nil {
 		return
 	}
diff --git a/session.go b/session.go
index f22bccf..4d9032a 100644
--- a/session.go
+++ b/session.go
@@ -1,6 +1,7 @@
 package gore
 
 import (
+	"bufio"
 	"bytes"
 	"errors"
 	"fmt"
@@ -21,6 +22,7 @@ import (
 	"syscall"
 	"unicode"
 
+	"golang.org/x/tools/go/packages"
 	"golang.org/x/tools/imports"
 
 	"github.com/motemen/go-quickfix"
@@ -91,6 +93,57 @@ func NewSession(stdout, stderr io.Writer) (*Session, error) {
 	return s, nil
 }
 
+func (s *Session) initGoMod() (err error) {
+	hasMod, replaces, err := getModReplaces()
+	if err != nil || !hasMod {
+		return
+	}
+
+	tempModule := filepath.Base(s.tempDir)
+	goModPath := filepath.Join(s.tempDir, "go.mod")
+
+	mod := "module " + tempModule + "\n" + strings.Join(replaces, "\n")
+
+	return ioutil.WriteFile(goModPath, []byte(mod), 0644)
+}
+
+func getModReplaces() (hasMod bool, replaces []string, err error) {
+	pwd, err := os.Getwd()
+	if err != nil {
+		return
+	}
+
+	file, err := os.Open(pwd + "/go.mod")
+	if err != nil {
+		if os.IsNotExist(err) {
+			err = nil
+		}
+		return
+	}
+	defer file.Close()
+
+	out, err := exec.Command("go", "list", "-m", "all").Output()
+	s := bufio.NewScanner(bytes.NewReader(out))
+
+	s.Scan()
+	module := s.Text()
+	if module == "" {
+		return
+	}
+
+	hasMod = true
+	replaces = append(replaces, "replace "+module+" => "+pwd)
+
+	for s.Scan() {
+		replace := s.Text()
+		if strings.Contains(replace, "=>") {
+			replaces = append(replaces, "replace "+replace)
+		}
+	}
+
+	return
+}
+
 func (s *Session) init() (err error) {
 	s.fset = token.NewFileSet()
 	s.types = &types.Config{Importer: importer.Default()}
@@ -100,7 +153,7 @@ func (s *Session) init() (err error) {
 
 	var initialSource string
 	for _, pp := range printerPkgs {
-		_, err := s.types.Importer.Import(pp.path)
+		_, err := packages.Load(&packages.Config{}, pp.path)
 		if err == nil {
 			initialSource = fmt.Sprintf(initialSourceTemplate, pp.path, pp.code)
 			break
@@ -108,6 +161,8 @@ func (s *Session) init() (err error) {
 		debugf("could not import %q: %s", pp.path, err)
 	}
 
+	s.initGoMod()
+
 	if initialSource == "" {
 		return fmt.Errorf(`Could not load pretty printing package (even "fmt"; something is wrong)`)
 	}


Test patch:
diff --git a/errfilter_test.go b/errfilter_test.go
index ae7203b..0aa23c2 100644
--- a/errfilter_test.go
+++ b/errfilter_test.go
@@ -36,11 +36,6 @@ func TestErrFilter(t *testing.T) {
 			"# command-line-arguments foo\n/tmp/gore_session.go:10:24: undefined: foo",
 			"undefined: foo",
 		},
-		{
-			"no module dependencies warning",
-			"warning: pattern \"all\" matched no module dependencies\nwarning: pattern \"all\" matched no module dependencie",
-			"warning: pattern \"all\" matched no module dependencie",
-		},
 	}
 	for _, tc := range testCases {
 		t.Run(tc.id, func(t *testing.T) {
diff --git a/session_test.go b/session_test.go
index 2a3d97c..8b16613 100644
--- a/session_test.go
+++ b/session_test.go
@@ -2,6 +2,7 @@ package gore
 
 import (
 	"bytes"
+	"os"
 	"regexp"
 	"testing"
 
@@ -389,3 +390,10 @@ invalid argument f() (type int) for len
 invalid operation: f() + g() (mismatched types int and string)
 `, stderr.String())
 }
+
+func TestGetCurrentModule(t *testing.T) {
+	_, replaces, _ := getModReplaces()
+	pwd, _ := os.Getwd()
+	expected := "replace github.com/motemen/gore => " + pwd
+	assert.Equal(t, expected, replaces[0])
+}