From 8a28b64b0c9df503ea09911bdc12b0531cf72533 Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 7 Jan 2024 21:48:03 +0800 Subject: [PATCH 1/2] Module.IsValid => HasModfile --- gopmod/module.go | 5 ----- modload/module.go | 20 +++++++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/gopmod/module.go b/gopmod/module.go index ecbf4b9..cfec257 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -45,11 +45,6 @@ type Module struct { depmods []depmodInfo } -// IsValid returns if this module exists or not. -func (p *Module) IsValid() bool { - return p != nil && p.File != nil -} - // PkgType specifies a package type. type PkgType int diff --git a/modload/module.go b/modload/module.go index 24dc4ef..d204b11 100644 --- a/modload/module.go +++ b/modload/module.go @@ -41,18 +41,25 @@ type Module struct { Opt *modfile.File } -func (p Module) IsDefault() bool { - return p.Syntax == nil +// HasModfile returns if this module exists or not. +func (p Module) HasModfile() bool { + return p.Syntax != nil } // Modfile returns absolute path of the module file. func (p Module) Modfile() string { - return p.Syntax.Name + if syn := p.Syntax; syn != nil { + return syn.Name + } + return "" } // Root returns absolute root path of this module. func (p Module) Root() string { - return filepath.Dir(p.Syntax.Name) + if syn := p.Syntax; syn != nil { + return filepath.Dir(syn.Name) + } + return "" } // Path returns the module path. @@ -207,11 +214,10 @@ func hasGopExtended(opt *modfile.File) bool { // Save saves all changes of this module. func (p Module) Save() (err error) { - if p.IsDefault() { + modfile := p.Modfile() + if modfile == "" { return ErrSaveDefault } - - modfile := p.Modfile() data, err := p.Format() if err != nil { return From 5ae616baf0da4bc6afab70caa5df941012934f1d Mon Sep 17 00:00:00 2001 From: xushiwei Date: Sun, 7 Jan 2024 21:57:08 +0800 Subject: [PATCH 2/2] Module.PkgType fix: support Default module --- gopmod/module.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gopmod/module.go b/gopmod/module.go index cfec257..9faf8a2 100644 --- a/gopmod/module.go +++ b/gopmod/module.go @@ -78,7 +78,7 @@ func (p *Module) PkgType(pkgPath string) PkgType { } func isPkgInMod(pkgPath, modPath string) bool { - if strings.HasPrefix(pkgPath, modPath) { + if modPath != "" && strings.HasPrefix(pkgPath, modPath) { suffix := pkgPath[len(modPath):] return suffix == "" || suffix[0] == '/' }