@@ -44,6 +44,11 @@ type Module struct {
44
44
depmods []depmodInfo
45
45
}
46
46
47
+ // IsValid returns if this module exists or not.
48
+ func (p * Module ) IsValid () bool {
49
+ return p != nil && p .File != nil
50
+ }
51
+
47
52
// PkgType specifies a package type.
48
53
type PkgType int
49
54
@@ -102,31 +107,30 @@ func (p *Module) Lookup(pkgPath string) (pkg *Package, err error) {
102
107
dir := modDir + pkgPath [len (modPath ):]
103
108
pkg = & Package {Type : PkgtModule , ModPath : modPath , ModDir : modDir , Dir : dir }
104
109
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 )
115
111
default :
116
112
log .Panicln ("Module.Lookup:" , pkgPath , "unsupported pkgType:" , pt )
117
113
}
118
114
return
119
115
}
120
116
121
- // lookupExternPkg lookups a external package from depended modules.
117
+ // LookupExternPkg lookups a external package from depended modules.
122
118
// 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 ) {
124
120
for _ , m := range p .depmods {
125
121
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
128
131
}
129
132
}
133
+ err = syscall .ENOENT
130
134
return
131
135
}
132
136
0 commit comments