Repository: goplus/gop
Pull request #1182 (2022-05-18 08:38:23)

LoadDir: add conf.GopRoot to enable gopImporter


Patch:
diff --git a/cl/builtin.go b/cl/builtin.go
index 0a5d5f15..751afc2a 100644
--- a/cl/builtin.go
+++ b/cl/builtin.go
@@ -19,6 +19,7 @@ package cl
 import (
 	"go/token"
 	"go/types"
+	"strings"
 
 	"github.com/goplus/gox"
 )
@@ -68,3 +69,29 @@ func newBuiltinDefault(pkg gox.PkgImporter, conf *gox.Config) *types.Package {
 }
 
 // -----------------------------------------------------------------------------
+
+type gopImporter struct {
+	gopRoot string
+	impFrom types.ImporterFrom
+}
+
+func newGopImporter(gopRoot string, imp types.Importer) types.Importer {
+	if impFrom, ok := imp.(types.ImporterFrom); ok && gopRoot != "" {
+		return &gopImporter{gopRoot: gopRoot, impFrom: impFrom}
+	}
+	return imp
+}
+
+func (p *gopImporter) Import(pkgPath string) (pkg *types.Package, err error) {
+	const (
+		gop = "github.com/goplus/gop"
+	)
+	if strings.HasPrefix(pkgPath, gop) {
+		if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
+			return p.impFrom.ImportFrom(pkgPath, p.gopRoot, 0)
+		}
+	}
+	return p.impFrom.Import(pkgPath)
+}
+
+// -----------------------------------------------------------------------------
diff --git a/cl/compile.go b/cl/compile.go
index 515dc341..466d3fce 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -82,6 +82,9 @@ type Config struct {
 	// TargetDir is the directory in which to generate Go files.
 	TargetDir string
 
+	// GopRoot specifies the Go+ root directory.
+	GopRoot string
+
 	// Fset provides source position information for syntax trees and types.
 	// If Fset is nil, Load will use a new fileset, but preserve Fset's value.
 	Fset *token.FileSet
@@ -374,7 +377,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
 	ctx := &pkgCtx{syms: make(map[string]loader), nodeInterp: interp}
 	confGox := &gox.Config{
 		Fset:            conf.Fset,
-		Importer:        conf.Importer,
+		Importer:        newGopImporter(conf.GopRoot, conf.Importer),
 		LoadNamed:       ctx.loadNamed,
 		HandleErr:       ctx.handleErr,
 		NodeInterpreter: interp,
diff --git a/go.mod b/go.mod
index 6735ad67..45071377 100644
--- a/go.mod
+++ b/go.mod
@@ -3,9 +3,8 @@ module github.com/goplus/gop
 go 1.16
 
 require (
-	github.com/goplus/gox v1.11.3
+	github.com/goplus/gox v1.11.5
 	github.com/goplus/mod v0.9.1
 	github.com/qiniu/x v1.11.5
 	golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3
-	golang.org/x/tools v0.1.10
 )
diff --git a/go.sum b/go.sum
index 4c5a8194..1320032e 100644
--- a/go.sum
+++ b/go.sum
@@ -1,5 +1,5 @@
-github.com/goplus/gox v1.11.3 h1:CU4AhLkC2bs5gMgperqluZk6hc8oR03OHdCZVXJABdQ=
-github.com/goplus/gox v1.11.3/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
+github.com/goplus/gox v1.11.5 h1:/RL4L0f42b55/8RVeuYkOCNEPvfvyaT1dXfzmLUFYKo=
+github.com/goplus/gox v1.11.5/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
 github.com/goplus/mod v0.9.1 h1:Mcn1Tmkgc5jaQJ2vCw1BNJEZnwsK2g14XfnMz50xlis=
 github.com/goplus/mod v0.9.1/go.mod h1:Yf4wlAd5TPf7OS0AjvGFSP5WHl6fgWlmFHa8h+A5XBE=
 github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
diff --git a/load.go b/load.go
index b4613904..a78abec3 100644
--- a/load.go
+++ b/load.go
@@ -82,6 +82,7 @@ func LoadDir(dir string, conf *Config) (out, test *gox.Package, err error) {
 	var pkgTest *ast.Package
 	var clConf = &cl.Config{
 		WorkingDir:  dir,
+		GopRoot:     gop.Root,
 		Fset:        fset,
 		Importer:    imp,
 		LookupClass: mod.LookupClass,


Test patch:
diff --git a/cl/builtin_test.go b/cl/builtin_test.go
index c7e4726a..338aa423 100644
--- a/cl/builtin_test.go
+++ b/cl/builtin_test.go
@@ -129,6 +129,12 @@ func lookupClass(ext string) (c *gopmod.Class, ok bool) {
 	return
 }
 
+func TestImporter(t *testing.T) {
+	if newGopImporter("", nil) != nil {
+		t.Fatal("TestImporter failed")
+	}
+}
+
 func TestGetGoFile(t *testing.T) {
 	if f := getGoFile("a_test.gop", true); f != testingGoFile {
 		t.Fatal("TestGetGoFile:", f)
diff --git a/cl/compile_test.go b/cl/compile_test.go
index 994bea24..f6fbceab 100644
--- a/cl/compile_test.go
+++ b/cl/compile_test.go
@@ -46,6 +46,7 @@ func init() {
 		Fset:          gblFset,
 		Importer:      imp,
 		LookupClass:   lookupClass,
+		GopRoot:       "..",
 		NoFileLine:    true,
 		NoAutoGenMain: true,
 	}