Skip to content

Commit

Permalink
interp: implements detection of packages with no Go files
Browse files Browse the repository at this point in the history
This avoids panic during import, and print a proper diagnostic
instead.

Fixes #1394.
  • Loading branch information
mvertes committed May 19, 2022
1 parent 4ed9ccb commit 0703926
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
Empty file added _test/p3/empty
Empty file.
11 changes: 11 additions & 0 deletions interp/interp_eval_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"bytes"
"context"
"fmt"
"go/build"
"go/parser"
"io"
"log"
Expand Down Expand Up @@ -1651,6 +1652,16 @@ func TestStdio(t *testing.T) {
}
}

func TestNoGoFiles(t *testing.T) {
i := interp.New(interp.Options{GoPath: build.Default.GOPATH})
_, err := i.Eval(`import "github.com/traefik/yaegi/_test/p3"`)
if strings.Contains(err.Error(), "no Go files in") {
return
}

t.Fatalf("failed to detect no Go files: %v", err)
}

func TestIssue1142(t *testing.T) {
i := interp.New(interp.Options{})
runTests(t, i, []testCase{
Expand Down
4 changes: 4 additions & 0 deletions interp/src.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ func (interp *Interpreter) importSrc(rPath, importPath string, skipTest bool) (s
// the global symbols in the package scope.
interp.mutex.Lock()
gs := interp.scopes[importPath]
if gs == nil {
// A nil scope means that no even an empty package is created from source.
return "", fmt.Errorf("no Go files in %s", dir)
}
interp.srcPkg[importPath] = gs.sym
interp.pkgNames[importPath] = pkgName

Expand Down

0 comments on commit 0703926

Please sign in to comment.