Skip to content

Commit

Permalink
gop.Import fix
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Jun 19, 2022
1 parent 7ea32db commit b3cb784
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 19 deletions.
5 changes: 0 additions & 5 deletions cl/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions cl/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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{
Expand Down
7 changes: 1 addition & 6 deletions gengo.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package gop

import (
"fmt"
"io/fs"
"os"
"path/filepath"
Expand Down Expand Up @@ -71,18 +70,14 @@ 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
}
return
}

if prompt {
fmt.Printf("GenGo %v ...\n", dir)
}

os.MkdirAll(dir, 0755)
file := filepath.Join(dir, autoGenFile)
err = out.WriteFile(file)
Expand Down
24 changes: 21 additions & 3 deletions imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go/token"
"go/types"
"os"
"os/exec"
"path/filepath"
"strings"

Expand Down Expand Up @@ -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
}

// -----------------------------------------------------------------------------
10 changes: 9 additions & 1 deletion load.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package gop

import (
"errors"
"fmt"
"go/token"
"go/types"
"io/fs"
Expand Down Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion tidy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit b3cb784

Please sign in to comment.