Skip to content

Commit 513e84f

Browse files
committed
Module.IsValid; LookupExternPkg
1 parent e69a335 commit 513e84f

File tree

1 file changed

+18
-14
lines changed

1 file changed

+18
-14
lines changed

gopmod/module.go

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,11 @@ type Module struct {
4444
depmods []depmodInfo
4545
}
4646

47+
// IsValid returns if this module exists or not.
48+
func (p *Module) IsValid() bool {
49+
return p != nil && p.File != nil
50+
}
51+
4752
// PkgType specifies a package type.
4853
type PkgType int
4954

@@ -102,31 +107,30 @@ func (p *Module) Lookup(pkgPath string) (pkg *Package, err error) {
102107
dir := modDir + pkgPath[len(modPath):]
103108
pkg = &Package{Type: PkgtModule, ModPath: modPath, ModDir: modDir, Dir: dir}
104109
case PkgtExtern:
105-
if modPath, modVer, ok := p.lookupExternPkg(pkgPath); ok {
106-
if modDir, e := modcache.Path(modVer); e == nil {
107-
dir := modDir + pkgPath[len(modPath):]
108-
pkg = &Package{Type: PkgtExtern, ModPath: modPath, ModDir: modDir, Dir: dir}
109-
} else {
110-
return nil, e
111-
}
112-
} else {
113-
err = syscall.ENOENT
114-
}
110+
pkg, _, err = p.LookupExternPkg(pkgPath)
115111
default:
116112
log.Panicln("Module.Lookup:", pkgPath, "unsupported pkgType:", pt)
117113
}
118114
return
119115
}
120116

121-
// lookupExternPkg lookups a external package from depended modules.
117+
// LookupExternPkg lookups a external package from depended modules.
122118
// If modVer.Path is replace to be a local path, it will be canonical to an absolute path.
123-
func (p *Module) lookupExternPkg(pkgPath string) (modPath string, modVer module.Version, ok bool) {
119+
func (p *Module) LookupExternPkg(pkgPath string) (pkg *Package, modVer module.Version, err error) {
124120
for _, m := range p.depmods {
125121
if isPkgInMod(pkgPath, m.path) {
126-
modPath, modVer, ok = m.path, m.real, true
127-
break
122+
modVer = m.real
123+
if modDir, e := modcache.Path(modVer); e == nil {
124+
modPath := m.path
125+
dir := modDir + pkgPath[len(modPath):]
126+
pkg = &Package{Type: PkgtExtern, ModPath: modPath, ModDir: modDir, Dir: dir}
127+
} else {
128+
err = e
129+
}
130+
return
128131
}
129132
}
133+
err = syscall.ENOENT
130134
return
131135
}
132136

0 commit comments

Comments
 (0)