diff --git a/gopmod/classfile.go b/gopmod/classfile.go index 0ffed46..b132bf2 100644 --- a/gopmod/classfile.go +++ b/gopmod/classfile.go @@ -17,8 +17,7 @@ package gopmod import ( - "syscall" - + "github.com/goplus/mod" "github.com/goplus/mod/modcache" "github.com/goplus/mod/modfetch" "github.com/goplus/mod/modfile" @@ -46,9 +45,17 @@ var ( ) var ( + ErrNotFound = mod.ErrNotFound ErrNotClassFileMod = errors.New("not a classfile module") ) +// IsNotFound returns a boolean indicating whether the error is known to +// report that a module or package does not exist. It is satisfied by +// ErrNotFound. +func IsNotFound(err error) bool { + return errors.Err(err) == ErrNotFound +} + // ----------------------------------------------------------------------------- // ClassKind checks a fname is a known classfile or not. @@ -98,10 +105,10 @@ func (p *Module) ImportClasses(importClass ...func(c *Project)) (err error) { func (p *Module) importMod(modPath string, imcls func(c *Project)) (err error) { mod, ok := p.LookupDepMod(modPath) if !ok { - return syscall.ENOENT + return ErrNotFound } err = p.importClassFrom(mod, imcls) - if errors.Err(err) != syscall.ENOENT { + if !IsNotFound(err) { return } mod, err = modfetch.Get(mod.String()) diff --git a/gopmod/gopmod_test.go b/gopmod/gopmod_test.go index e41670e..ec3dc82 100644 --- a/gopmod/gopmod_test.go +++ b/gopmod/gopmod_test.go @@ -85,6 +85,11 @@ func TestClassfile(t *testing.T) { if !mod.IsClass("_yap.gox") { t.Fatal("mod.IsClass _yap.gox: not ok?") } + + modVer = module.Version{Path: "github.com/unknown-repo/x", Version: "v0.5.0"} + if _, err = LoadMod(modVer); !IsNotFound(err) { + log.Fatal("LoadMod github.com/unknown-repo/x:", err) + } } func TestClassfile2(t *testing.T) { diff --git a/gopmod/module.go b/gopmod/module.go index 27f2b3a..c32a73c 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -22,7 +22,6 @@ import ( "path/filepath" "runtime" "strings" - "syscall" "github.com/goplus/mod/modcache" "github.com/goplus/mod/modfetch" @@ -173,7 +172,7 @@ func LoadFrom(gomod, gopmod string) (*Module, error) { // If we only want to load a Go modfile, pass env parameter as nil. func LoadMod(mod module.Version) (p *Module, err error) { p, err = loadModFrom(mod) - if errors.Err(err) != syscall.ENOENT { + if !IsNotFound(err) { return } mod, err = modfetch.Get(mod.String()) diff --git a/mod.go b/mod.go index aae3ea9..3678399 100644 --- a/mod.go +++ b/mod.go @@ -23,6 +23,10 @@ import ( "syscall" ) +var ( + ErrNotFound = syscall.ENOENT +) + // ----------------------------------------------------------------------------- func FindGoMod(dirFrom string) (dir, file string, err error) { @@ -41,7 +45,7 @@ func FindGoMod(dirFrom string) (dir, file string, err error) { break } } - err = syscall.ENOENT + err = ErrNotFound return } diff --git a/modfetch/fetch.go b/modfetch/fetch.go index 57f15bc..169a9d4 100644 --- a/modfetch/fetch.go +++ b/modfetch/fetch.go @@ -27,8 +27,8 @@ import ( "os/exec" "path/filepath" "strings" - "syscall" + xmod "github.com/goplus/mod" "github.com/goplus/mod/modcache" "golang.org/x/mod/module" "golang.org/x/mod/semver" @@ -172,7 +172,7 @@ func Get(modPath string, noCache ...bool) (mod module.Version, err error) { } if noCache == nil || !noCache[0] { mod, err = getFromCache(modPath) - if err != syscall.ENOENT { + if err != xmod.ErrNotFound { return } } @@ -190,7 +190,7 @@ func Get(modPath string, noCache ...bool) (mod module.Version, err error) { cmd.Run() if stderr.Len() > 0 { mod, err = getResult(stderr.String()) - if err != syscall.ENOENT { + if err != xmod.ErrNotFound { if debugVerbose { log.Println("modfetch.Get ret:", err) } @@ -212,7 +212,7 @@ func getResult(data string) (mod module.Version, err error) { } return getMod(data[len(downloading):], nil) } - err = syscall.ENOENT + err = xmod.ErrNotFound return } @@ -227,7 +227,7 @@ func getMod(data string, next *string) (mod module.Version, err error) { return } } - err = syscall.ENOENT + err = xmod.ErrNotFound return } @@ -252,7 +252,7 @@ func lookupFromCache(modPath string) (modRoot string, mod module.Version, err er if pos > 0 { // has version fi, e := os.Stat(modRoot) if e != nil || !fi.IsDir() { - err = syscall.ENOENT + err = xmod.ErrNotFound } return } @@ -262,7 +262,7 @@ func lookupFromCache(modPath string) (modRoot string, mod module.Version, err er err = errors.Unwrap(err) return } - err = syscall.ENOENT + err = xmod.ErrNotFound for _, fi := range fis { if fi.IsDir() { if name := fi.Name(); strings.HasPrefix(name, fname) {