From 07039262a05bcf508068f2bd11c98bd456860ba2 Mon Sep 17 00:00:00 2001 From: Marc Vertes Date: Thu, 19 May 2022 18:34:08 +0200 Subject: [PATCH] interp: implements detection of packages with no Go files This avoids panic during import, and print a proper diagnostic instead. Fixes #1394. --- _test/p3/empty | 0 interp/interp_eval_test.go | 11 +++++++++++ interp/src.go | 4 ++++ 3 files changed, 15 insertions(+) create mode 100644 _test/p3/empty diff --git a/_test/p3/empty b/_test/p3/empty new file mode 100644 index 000000000..e69de29bb diff --git a/interp/interp_eval_test.go b/interp/interp_eval_test.go index b4edc4428..86a193a77 100644 --- a/interp/interp_eval_test.go +++ b/interp/interp_eval_test.go @@ -5,6 +5,7 @@ import ( "bytes" "context" "fmt" + "go/build" "go/parser" "io" "log" @@ -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{ diff --git a/interp/src.go b/interp/src.go index 46fcba1c6..f7ccfad87 100644 --- a/interp/src.go +++ b/interp/src.go @@ -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