Repository: motemen/gore (go)

Issue:
:type not works with -context
To Reproduce:
---

file `test1.go`:

```
package test

var x = 1
```

in shell:

```
gore -context test1.go
```

in gore:

```
gore> x
 1

gore> :t x
 type: cannot get type: x

gore> :t 1
 int
```

Expected:
---

`:t x` show type of `x` correctly.

Observed:
---

`x` is loaded to current environment but `:t x` doesn't works.


Pull request description:
fix :type and :doc for -context
As title, closes #149

Patch:
diff --git a/commands.go b/commands.go
index 0eb421e..49be37f 100644
--- a/commands.go
+++ b/commands.go
@@ -119,7 +119,7 @@ func actionImport(s *Session, arg string) error {
 	}
 	if !found {
 		astutil.AddNamedImport(s.fset, s.file, "_", path)
-		_, err = s.types.Check("_tmp", s.fset, []*ast.File{s.file}, nil)
+		_, err = s.types.Check("_tmp", s.fset, append(s.extraFiles, s.file), nil)
 		if err != nil && strings.Contains(err.Error(), "could not import "+path) {
 			astutil.DeleteNamedImport(s.fset, s.file, "_", path)
 			return fmt.Errorf("could not import %q", path)
@@ -297,7 +297,7 @@ func actionType(s *Session, in string) error {
 		Defs:   make(map[*ast.Ident]types.Object),
 		Scopes: make(map[ast.Node]*types.Scope),
 	}
-	_, err = s.types.Check("_tmp", s.fset, []*ast.File{s.file}, &s.typeInfo)
+	_, err = s.types.Check("_tmp", s.fset, append(s.extraFiles, s.file), &s.typeInfo)
 	if err != nil {
 		debugf("typecheck error (ignored): %s", err)
 	}
@@ -358,7 +358,7 @@ func actionDoc(s *Session, in string) error {
 		Defs:   make(map[*ast.Ident]types.Object),
 		Scopes: make(map[ast.Node]*types.Scope),
 	}
-	_, err = s.types.Check("_tmp", s.fset, []*ast.File{s.file}, &s.typeInfo)
+	_, err = s.types.Check("_tmp", s.fset, append(s.extraFiles, s.file), &s.typeInfo)
 	if err != nil {
 		debugf("typecheck error (ignored): %s", err)
 	}
diff --git a/quickfix.go b/quickfix.go
index e6df460..a9e6e98 100644
--- a/quickfix.go
+++ b/quickfix.go
@@ -24,12 +24,9 @@ quickFixAttempt:
 			Types: make(map[ast.Expr]types.TypeAndValue),
 		}
 
-		files := s.extraFiles
-		files = append(files, s.file)
-
 		config := quickfix.Config{
 			Fset:     s.fset,
-			Files:    files,
+			Files:    append(s.extraFiles, s.file),
 			TypeInfo: &s.typeInfo,
 			Dir:      s.tempDir,
 		}
diff --git a/session.go b/session.go
index f3e144a..5cbf855 100644
--- a/session.go
+++ b/session.go
@@ -529,15 +529,12 @@ func (s *Session) importPackages(src []byte) error {
 
 // importFile adds external golang file to goRun target to use its function
 func (s *Session) importFile(src []byte) error {
-	// Don't need to same directory
-	tmp, err := ioutil.TempFile(s.tempDir, "gore_extarnal_")
+	tmp, err := ioutil.TempFile(s.tempDir, "gore_external_*.go")
 	if err != nil {
 		return err
 	}
 
-	ext := tmp.Name() + ".go"
-
-	f, err := parser.ParseFile(s.fset, ext, src, parser.Mode(0))
+	f, err := parser.ParseFile(s.fset, tmp.Name(), src, parser.Mode(0))
 	if err != nil {
 		return err
 	}
@@ -558,7 +555,7 @@ func (s *Session) importFile(src []byte) error {
 		}
 	}
 
-	out, err := os.Create(ext)
+	out, err := os.Create(tmp.Name())
 	if err != nil {
 		return err
 	}
@@ -569,8 +566,8 @@ func (s *Session) importFile(src []byte) error {
 		return err
 	}
 
-	debugf("import file: %s", ext)
-	s.extraFilePaths = append(s.extraFilePaths, ext)
+	debugf("import file: %s", tmp.Name())
+	s.extraFilePaths = append(s.extraFilePaths, tmp.Name())
 	s.extraFiles = append(s.extraFiles, f)
 
 	return nil


Test patch:
diff --git a/session_test.go b/session_test.go
index bb5a026..a9e52e9 100644
--- a/session_test.go
+++ b/session_test.go
@@ -2,6 +2,8 @@ package gore
 
 import (
 	"bytes"
+	"io/ioutil"
+	"os"
 	"regexp"
 	"testing"
 
@@ -384,3 +386,34 @@ invalid argument f() (type int) for len
 invalid operation: f() + g() (mismatched types int and string)
 `, stderr.String())
 }
+
+func TestSession_ExtraFiles(t *testing.T) {
+	stdout, stderr := new(bytes.Buffer), new(bytes.Buffer)
+	tempDir, _ := ioutil.TempDir("", "gore-")
+	defer chdir(tempDir)()
+	defer os.RemoveAll(tempDir)
+	require.NoError(t, ioutil.WriteFile("test.go", []byte(`package test
+
+// V is a value
+var V = 42
+`), 0644))
+	s, err := NewSession(stdout, stderr)
+	defer s.Clear()
+	require.NoError(t, err)
+
+	s.includeFiles([]string{"test.go"})
+	codes := []string{
+		`V`,
+		`:type V`,
+		`:doc V`,
+	}
+
+	for _, code := range codes {
+		_ = s.Eval(code)
+	}
+
+	assert.Contains(t, stdout.String(), `42
+int
+package builtin`)
+	assert.Equal(t, ``, stderr.String())
+}