This repository has been archived by the owner on Mar 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathresolver.go
131 lines (109 loc) · 2.72 KB
/
resolver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package gb
import (
"fmt"
"go/build"
"os"
pathpkg "path"
"path/filepath"
"runtime"
"strings"
"github.com/pkg/errors"
)
type nullImporter struct{}
func (i *nullImporter) Import(path string) (*build.Package, error) {
return nil, errors.Errorf("import %q not found", path)
}
type srcImporter struct {
Importer
im importer
}
func (i *srcImporter) Import(path string) (*build.Package, error) {
pkg, err := i.im.Import(path)
if err == nil {
return pkg, nil
}
// gb expects, when there is a failure to resolve packages that
// live in $PROJECT/src that the importer for that directory
// will report them.
pkg, err2 := i.Importer.Import(path)
if err2 == nil {
return pkg, nil
}
return nil, err
}
type _importer struct {
Importer
im importer
}
func (i *_importer) Import(path string) (*build.Package, error) {
pkg, err := i.im.Import(path)
if err != nil {
return i.Importer.Import(path)
}
return pkg, nil
}
type fixupImporter struct {
Importer
}
func (i *fixupImporter) Import(path string) (*build.Package, error) {
pkg, err := i.Importer.Import(path)
switch err.(type) {
case *os.PathError:
return nil, errors.Wrapf(err, "import %q: not found", path)
default:
return pkg, err
}
}
type importer struct {
*build.Context
Root string // root directory
}
type importErr struct {
path string
msg string
}
func (e *importErr) Error() string {
return fmt.Sprintf("import %q: %v", e.path, e.msg)
}
func (i *importer) Import(path string) (*build.Package, error) {
if path == "" {
return nil, errors.WithStack(&importErr{path: path, msg: "invalid import path"})
}
if path == "." || path == ".." || strings.HasPrefix(path, "./") || strings.HasPrefix(path, "../") {
return nil, errors.WithStack(&importErr{path: path, msg: "relative import not supported"})
}
if strings.HasPrefix(path, "/") {
return nil, errors.WithStack(&importErr{path: path, msg: "cannot import absolute path"})
}
var p *build.Package
loadPackage := func(importpath, dir string) error {
pkg, err := i.ImportDir(dir, 0)
if err != nil {
return err
}
p = pkg
p.ImportPath = importpath
return nil
}
// if this is the stdlib, then search vendor first.
// this isn't real vendor support, just enough to make net/http compile.
if i.Root == runtime.GOROOT() {
path := pathpkg.Join("vendor", path)
dir := filepath.Join(i.Root, "src", filepath.FromSlash(path))
fi, err := os.Stat(dir)
if err == nil && fi.IsDir() {
err := loadPackage(path, dir)
return p, err
}
}
dir := filepath.Join(i.Root, "src", filepath.FromSlash(path))
fi, err := os.Stat(dir)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return nil, errors.Errorf("import %q: not a directory", path)
}
err = loadPackage(path, dir)
return p, err
}