From b3cb7848ec026ae8790b1274e61d6708546eeb22 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 19 Jun 2022 09:38:04 +0800 Subject: [PATCH] gop.Import fix --- cl/classfile.go | 5 ----- cl/compile.go | 11 ++++++++--- gengo.go | 7 +------ imp.go | 24 +++++++++++++++++++++--- load.go | 10 +++++++++- tidy.go | 1 - 6 files changed, 39 insertions(+), 19 deletions(-) diff --git a/cl/classfile.go b/cl/classfile.go index a5ed6d961..432e1a2da 100644 --- a/cl/classfile.go +++ b/cl/classfile.go @@ -61,11 +61,6 @@ func (p *gmxSettings) getScheds(cb *gox.CodeBuilder) []goast.Stmt { } func newGmx(ctx *pkgCtx, pkg *gox.Package, file string, conf *Config) *gmxSettings { - defer func() { - if e := recover(); e != nil { - ctx.handleRecover(e) - } - }() _, name := filepath.Split(file) ext := filepath.Ext(name) if idx := strings.Index(name, "."); idx > 0 { diff --git a/cl/compile.go b/cl/compile.go index 7198eb3ed..591d0537d 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -373,6 +373,14 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, DefaultGoFile: defaultGoFile, NoSkipConstant: conf.NoSkipConstant, } + if enableRecover { + defer func() { + if e := recover(); e != nil { + ctx.handleRecover(e) + err = ctx.errs.ToError() + } + }() + } p = gox.NewPackage(pkgPath, pkg.Name, confGox) ctx.cpkgs = cpackages.NewImporter(&cpackages.Config{ Pkg: p, LookupPub: conf.LookupPub, @@ -383,9 +391,6 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package, break } } - if ctx.errs != nil { - return nil, ctx.errs.ToError() - } for fpath, f := range files { fileLine := !conf.NoFileLine ctx := &blockCtx{ diff --git a/gengo.go b/gengo.go index c44d53e54..fb7d943e4 100644 --- a/gengo.go +++ b/gengo.go @@ -17,7 +17,6 @@ package gop import ( - "fmt" "io/fs" "os" "path/filepath" @@ -71,7 +70,7 @@ func genGoDir(dir string, conf *Config, genTestPkg, recursively bool) (err error } func genGoIn(dir string, conf *Config, genTestPkg, prompt bool) (err error) { - out, test, err := LoadDir(dir, conf, genTestPkg) + out, test, err := LoadDir(dir, conf, genTestPkg, prompt) if err != nil { if err == syscall.ENOENT { // no Go+ source files return nil @@ -79,10 +78,6 @@ func genGoIn(dir string, conf *Config, genTestPkg, prompt bool) (err error) { return } - if prompt { - fmt.Printf("GenGo %v ...\n", dir) - } - os.MkdirAll(dir, 0755) file := filepath.Join(dir, autoGenFile) err = out.WriteFile(file) diff --git a/imp.go b/imp.go index 059b2eb24..50d4dac91 100644 --- a/imp.go +++ b/imp.go @@ -20,6 +20,7 @@ import ( "go/token" "go/types" "os" + "os/exec" "path/filepath" "strings" @@ -86,21 +87,38 @@ func (p *Importer) Import(pkgPath string) (pkg *types.Package, err error) { if err = p.genGoExtern(ret.Dir, false); err != nil { return } + case gopmod.PkgtStandard: + return p.impFrom.ImportFrom(pkgPath, p.gop.Root, 0) } } return p.impFrom.Import(pkgPath) } func (p *Importer) genGoExtern(dir string, isExtern bool) (err error) { - genfile := filepath.Join(dir, autoGenFile) - if _, err = os.Lstat(genfile); err == nil { // has gop_autogen.go + gosum := filepath.Join(dir, "go.sum") + if _, err = os.Lstat(gosum); err == nil { // has go.sum return } + if isExtern { os.Chmod(dir, modWritable) defer os.Chmod(dir, modReadonly) } - return genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, false) + + genfile := filepath.Join(dir, autoGenFile) + if _, err = os.Lstat(genfile); err != nil { // has gop_autogen.go + err = genGoIn(dir, &Config{Gop: p.gop, Importer: p, Fset: p.fset}, false, false) + if err != nil { + return + } + } + + cmd := exec.Command("go", "mod", "tidy") + cmd.Stdout = os.Stdout + cmd.Stderr = os.Stderr + cmd.Dir = dir + err = cmd.Run() + return } // ----------------------------------------------------------------------------- diff --git a/load.go b/load.go index 88ab6df9a..8f432b43b 100644 --- a/load.go +++ b/load.go @@ -18,6 +18,7 @@ package gop import ( "errors" + "fmt" "go/token" "go/types" "io/fs" @@ -79,7 +80,7 @@ func lookupPub(mod *gopmod.Module) func(pkgPath string) (pubfile string, err err // ----------------------------------------------------------------------------- -func LoadDir(dir string, conf *Config, genTestPkg bool) (out, test *gox.Package, err error) { +func LoadDir(dir string, conf *Config, genTestPkg bool, promptGenGo ...bool) (out, test *gox.Package, err error) { if conf == nil { conf = new(Config) } @@ -104,6 +105,13 @@ func LoadDir(dir string, conf *Config, genTestPkg bool) (out, test *gox.Package, if err != nil { return } + if len(pkgs) == 0 { + return nil, nil, syscall.ENOENT + } + + if promptGenGo != nil && promptGenGo[0] { + fmt.Printf("GenGo %v ...\n", dir) + } imp := conf.Importer if imp == nil { diff --git a/tidy.go b/tidy.go index dc11867f1..e96e2031e 100644 --- a/tidy.go +++ b/tidy.go @@ -75,7 +75,6 @@ func Tidy(dir string, gop *env.Gop) (err error) { cmd := exec.Command("go", "mod", "tidy") cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr - cmd.Stdin = os.Stdin cmd.Dir = modRoot err = cmd.Run() if err != nil {