Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1301 #1302

Merged
merged 1 commit into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 14 additions & 14 deletions build_install_run.go
Expand Up @@ -17,27 +17,27 @@
package gop

import (
"errors"
"log"
"os"

"github.com/goplus/gop/x/gocmd"
"github.com/qiniu/x/errors"
)

// -----------------------------------------------------------------------------

func InstallDir(dir string, conf *Config, install *gocmd.InstallConfig) (err error) {
_, _, err = GenGo(dir, conf, false)
if err != nil {
return
return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
}
return gocmd.Install(dir, install)
}

func InstallPkgPath(workDir, pkgPath string, conf *Config, install *gocmd.InstallConfig) (err error) {
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, true)
if err != nil {
return
return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, true)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, true)
}
old := chdir(localDir)
defer os.Chdir(old)
Expand All @@ -54,7 +54,7 @@ func cwdParam(recursively bool) string {
func InstallFiles(files []string, conf *Config, install *gocmd.InstallConfig) (err error) {
files, err = GenGoFiles("", files, conf)
if err != nil {
return
return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
}
return gocmd.InstallFiles(files, install)
}
Expand All @@ -76,15 +76,15 @@ func chdir(dir string) string {
func BuildDir(dir string, conf *Config, build *gocmd.BuildConfig) (err error) {
_, _, err = GenGo(dir, conf, false)
if err != nil {
return
return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
}
return gocmd.Build(dir, build)
}

func BuildPkgPath(workDir, pkgPath string, conf *Config, build *gocmd.BuildConfig) (err error) {
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, false)
if err != nil {
return
return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, false)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, false)
}
old := chdirAndMod(localDir)
defer restoreDirAndMod(old)
Expand All @@ -94,7 +94,7 @@ func BuildPkgPath(workDir, pkgPath string, conf *Config, build *gocmd.BuildConfi
func BuildFiles(files []string, conf *Config, build *gocmd.BuildConfig) (err error) {
files, err = GenGoFiles("", files, conf)
if err != nil {
return
return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
}
return gocmd.BuildFiles(files, build)
}
Expand All @@ -114,18 +114,18 @@ func restoreDirAndMod(old string) {
func RunDir(dir string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
_, _, err = GenGo(dir, conf, false)
if err != nil {
return
return errors.NewWith(err, `GenGo(dir, conf, false)`, -2, "gop.GenGo", dir, conf, false)
}
return gocmd.RunDir(dir, args, run)
}

func RunPkgPath(pkgPath string, args []string, chDir bool, conf *Config, run *gocmd.RunConfig) (err error) {
localDir, recursively, err := GenGoPkgPath("", pkgPath, conf, true)
if err != nil {
return
return errors.NewWith(err, `GenGoPkgPath("", pkgPath, conf, true)`, -2, "gop.GenGoPkgPath", "", pkgPath, conf, true)
}
if recursively {
return errors.New("can't use ... pattern for `gop run` command")
return errors.NewWith(errors.New("can't use ... pattern for `gop run` command"), `recursively`, -1, "", recursively)
}
if chDir {
old := chdir(localDir)
Expand All @@ -138,7 +138,7 @@ func RunPkgPath(pkgPath string, args []string, chDir bool, conf *Config, run *go
func RunFiles(autogen string, files []string, args []string, conf *Config, run *gocmd.RunConfig) (err error) {
files, err = GenGoFiles(autogen, files, conf)
if err != nil {
return
return errors.NewWith(err, `GenGoFiles(autogen, files, conf)`, -2, "gop.GenGoFiles", autogen, files, conf)
}
return gocmd.RunFiles(files, args, run)
}
Expand All @@ -148,15 +148,15 @@ func RunFiles(autogen string, files []string, args []string, conf *Config, run *
func TestDir(dir string, conf *Config, test *gocmd.TestConfig) (err error) {
_, _, err = GenGo(dir, conf, true)
if err != nil {
return
return errors.NewWith(err, `GenGo(dir, conf, true)`, -2, "gop.GenGo", dir, conf, true)
}
return gocmd.Test(dir, test)
}

func TestPkgPath(workDir, pkgPath string, conf *Config, test *gocmd.TestConfig) (err error) {
localDir, recursively, err := GenGoPkgPath(workDir, pkgPath, conf, false)
if err != nil {
return
return errors.NewWith(err, `GenGoPkgPath(workDir, pkgPath, conf, false)`, -2, "gop.GenGoPkgPath", workDir, pkgPath, conf, false)
}
old := chdirAndMod(localDir)
defer restoreDirAndMod(old)
Expand All @@ -166,7 +166,7 @@ func TestPkgPath(workDir, pkgPath string, conf *Config, test *gocmd.TestConfig)
func TestFiles(files []string, conf *Config, test *gocmd.TestConfig) (err error) {
files, err = GenGoFiles("", files, conf)
if err != nil {
return
return errors.NewWith(err, `GenGoFiles("", files, conf)`, -2, "gop.GenGoFiles", "", files, conf)
}
return gocmd.TestFiles(files, test)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/internal/gopget/get.go
Expand Up @@ -20,8 +20,8 @@ import (
"fmt"
"log"
"os"
"syscall"

"github.com/goplus/gop"
"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modcache"
Expand Down Expand Up @@ -63,16 +63,16 @@ func runCmd(cmd *base.Command, args []string) {
func get(pkgPath string) {
modBase := ""
mod, err := modload.Load(".", 0)
hasMod := (err != syscall.ENOENT)
if hasMod {
noMod := gop.NotFound(err)
if !noMod {
check(err)
check(mod.UpdateGoMod(gopenv.Get(), true))
modBase = mod.Path()
}

pkgModVer, _, err := modfetch.GetPkg(pkgPath, modBase)
check(err)
if !hasMod {
if noMod {
return
}

Expand Down
3 changes: 1 addition & 2 deletions cmd/internal/mod/tidy.go
Expand Up @@ -19,7 +19,6 @@ package mod
import (
"fmt"
"os"
"syscall"

"github.com/goplus/gop"
"github.com/goplus/gop/cmd/internal/base"
Expand All @@ -39,7 +38,7 @@ func init() {
func runTidy(cmd *base.Command, args []string) {
err := gop.Tidy(".", gopenv.Get())
if err != nil {
if err == syscall.ENOENT {
if gop.NotFound(err) {
fmt.Fprintln(os.Stderr, "gop.mod not found")
} else {
fmt.Fprintln(os.Stderr, err)
Expand Down
21 changes: 15 additions & 6 deletions gengo.go
Expand Up @@ -75,23 +75,28 @@ func genGoIn(dir string, conf *Config, genTestPkg, prompt bool) (err error) {
if err == syscall.ENOENT { // no Go+ source files
return nil
}
return
return errors.NewWith(err, `LoadDir(dir, conf, genTestPkg, prompt)`, -5, "gop.LoadDir", dir, conf, genTestPkg, prompt)
}

os.MkdirAll(dir, 0755)
file := filepath.Join(dir, autoGenFile)
err = out.WriteFile(file)
if err != nil {
return
return errors.NewWith(err, `out.WriteFile(file)`, -2, "(*gox.Package).WriteFile", out, file)
}

err = out.WriteFile(filepath.Join(dir, autoGenTestFile), testingGoFile)
testFile := filepath.Join(dir, autoGenTestFile)
err = out.WriteFile(testFile, testingGoFile)
if err != nil && err != syscall.ENOENT {
return
return errors.NewWith(err, `out.WriteFile(testFile, testingGoFile)`, -2, "(*gox.Package).WriteFile", out, testFile, testingGoFile)
}

if test != nil {
err = test.WriteFile(filepath.Join(dir, autoGen2TestFile), testingGoFile)
testFile = filepath.Join(dir, autoGen2TestFile)
err = test.WriteFile(testFile, testingGoFile)
if err != nil {
return errors.NewWith(err, `test.WriteFile(testFile, testingGoFile)`, -2, "(*gox.Package).WriteFile", test, testFile, testingGoFile)
}
} else {
err = nil
}
Expand All @@ -112,7 +117,7 @@ func GenGoPkgPath(workDir, pkgPath string, conf *Config, allowExtern bool) (loca
}

mod, err := gopmod.Load(workDir, 0)
if err == syscall.ENOENT && allowExtern {
if NotFound(err) && allowExtern {
remotePkgPathDo(pkgPath, func(dir string) {
os.Chmod(dir, modWritable)
defer os.Chmod(dir, modReadonly)
Expand Down Expand Up @@ -165,10 +170,14 @@ func GenGoFiles(autogen string, files []string, conf *Config) (result []string,
}
out, err := LoadFiles(files, conf)
if err != nil {
err = errors.NewWith(err, `LoadFiles(files, conf)`, -2, "gop.LoadFiles", files, conf)
return
}
result = append(result, autogen)
err = out.WriteFile(autogen)
if err != nil {
err = errors.NewWith(err, `out.WriteFile(autogen)`, -2, "(*gox.Package).WriteFile", out, autogen)
}
return
}

Expand Down
26 changes: 21 additions & 5 deletions load.go
Expand Up @@ -17,7 +17,6 @@
package gop

import (
"errors"
"fmt"
"go/token"
"go/types"
Expand All @@ -33,6 +32,7 @@ import (
"github.com/goplus/gox"
"github.com/goplus/mod/env"
"github.com/goplus/mod/gopmod"
"github.com/qiniu/x/errors"
)

type Config struct {
Expand All @@ -47,18 +47,27 @@ type Config struct {

// -----------------------------------------------------------------------------

func NotFound(err error) bool {
return errors.Err(err) == syscall.ENOENT
}

func loadMod(dir string, gop *env.Gop, conf *Config) (mod *gopmod.Module, err error) {
mod, err = gopmod.Load(dir, 0)
if err != nil && err != syscall.ENOENT {
if err != nil && !NotFound(err) {
err = errors.NewWith(err, `gopmod.Load(dir, 0`, -2, "gopmod.Load", dir, 0)
return
}
if mod != nil {
err = mod.RegisterClasses()
if err != nil {
err = errors.NewWith(err, `mod.RegisterClasses()`, -2, "(*gopmod.Module).RegisterClasses", mod)
return
}
if !conf.DontUpdateGoMod {
err = mod.UpdateGoMod(gop, !conf.DontCheckModChanged)
if err != nil {
err = errors.NewWith(err, `mod.UpdateGoMod(gop, !conf.DontCheckModChanged)`, -2, "(*gopmod.Module).UpdateGoMod", mod, gop, !conf.DontCheckModChanged)
}
}
return
}
Expand Down Expand Up @@ -170,27 +179,34 @@ func LoadFiles(files []string, conf *Config) (out *gox.Package, err error) {
}
mod, err := loadMod("", gop, conf)
if err != nil {
err = errors.NewWith(err, `loadMod("", gop, conf)`, -2, "gop.loadMod", "", gop, conf)
return
}

pkgs, err := parser.ParseFiles(fset, files, parser.ParseComments)
if err != nil {
err = errors.NewWith(err, `parser.ParseFiles(fset, files, parser.ParseComments)`, -2, "parser.ParseFiles", fset, files, parser.ParseComments)
return
}
if len(pkgs) != 1 {
return nil, errMultiPackges
err = errors.NewWith(errMultiPackges, `len(pkgs) != 1`, -1, "!=", len(pkgs), 1)
return
}
for _, pkg := range pkgs {
imp := conf.Importer
if imp == nil {
imp = NewImporter(mod, gop, fset)
}
out, err = cl.NewPackage("", pkg, &cl.Config{
clConf := &cl.Config{
Fset: fset,
Importer: imp,
LookupClass: mod.LookupClass,
LookupPub: lookupPub(mod),
})
}
out, err = cl.NewPackage("", pkg, clConf)
if err != nil {
err = errors.NewWith(err, `cl.NewPackage("", pkg, clConf)`, -2, "cl.NewPackage", "", pkg, clConf)
}
break
}
return
Expand Down