Skip to content

Commit df45523

Browse files
committed
Module.Lookup pkgPath
1 parent c26d35b commit df45523

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

gopmod/module.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@
1717
package gopmod
1818

1919
import (
20+
"log"
2021
"path/filepath"
22+
"runtime"
2123
"sort"
2224
"strings"
2325
"syscall"
@@ -84,6 +86,40 @@ func isPkgInMod(pkgPath, modPath string) bool {
8486
return false
8587
}
8688

89+
type Package struct {
90+
Type PkgType
91+
Dir string
92+
ModDir string
93+
ModPath string
94+
}
95+
96+
func (p *Module) Lookup(pkgPath string) (pkg *Package, err error) {
97+
switch pt := p.PkgType(pkgPath); pt {
98+
case PkgtStandard:
99+
modDir := runtime.GOROOT()
100+
pkg = &Package{Type: PkgtStandard, ModDir: modDir, Dir: filepath.Join(modDir, pkgPath)}
101+
case PkgtModule:
102+
modPath := p.Path()
103+
modDir := p.Root()
104+
dir := modDir + pkgPath[:len(modPath)]
105+
pkg = &Package{Type: PkgtModule, ModPath: modPath, ModDir: modDir, Dir: dir}
106+
case PkgtExtern:
107+
if modPath, modVer, ok := p.LookupExternPkg(pkgPath); ok {
108+
if modDir, e := modcache.Path(modVer); e == nil {
109+
dir := modDir + pkgPath[:len(modPath)]
110+
pkg = &Package{Type: PkgtExtern, ModPath: modPath, ModDir: modDir, Dir: dir}
111+
} else {
112+
return nil, e
113+
}
114+
} else {
115+
err = syscall.ENOENT
116+
}
117+
default:
118+
log.Panicln("Module.Lookup:", pkgPath, "unsupported pkgType:", pt)
119+
}
120+
return
121+
}
122+
87123
// LookupExternPkg lookups a external package from depended modules.
88124
// If modVer.Path is replace to be a local path, it will be canonical to an absolute path.
89125
func (p *Module) LookupExternPkg(pkgPath string) (modPath string, modVer module.Version, ok bool) {

0 commit comments

Comments
 (0)