Skip to content

Commit 2b2ff77

Browse files
committed
fixes
1 parent effe0f8 commit 2b2ff77

File tree

3 files changed

+51
-23
lines changed

3 files changed

+51
-23
lines changed

importer.go

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type Importer struct {
2020
importHandler func(path, src, dir string)
2121
isCommentLocator bool
2222
ctx build.Context
23+
gopath []string
2324
}
2425

2526
// NewImporter creates a new importer
@@ -32,7 +33,8 @@ func NewImporter(options ...Option) *Importer {
3233
errorHandler: func(err error) {
3334
return
3435
},
35-
ctx: build.Default,
36+
ctx: build.Default,
37+
gopath: filepath.SplitList(build.Default.GOPATH),
3638
}
3739
for _, v := range options {
3840
v(i)
@@ -46,7 +48,7 @@ func (i *Importer) ImportPackage(path string, pkg *ast.Package) (Type, error) {
4648
if ok {
4749
return t, nil
4850
}
49-
np := newParser(i, i.isCommentLocator, path, false)
51+
np := newParser(i, i.isCommentLocator, "", path, false)
5052
t = np.ParsePackage(pkg)
5153
i.bufType[path] = t
5254
return t, nil
@@ -58,7 +60,7 @@ func (i *Importer) ImportFile(path string, f *ast.File) (Type, error) {
5860
if ok {
5961
return t, nil
6062
}
61-
np := newParser(i, i.isCommentLocator, path, false)
63+
np := newParser(i, i.isCommentLocator, "", path, false)
6264
t = np.ParseFile(f)
6365
i.bufType[path] = t
6466
return t, nil
@@ -79,28 +81,52 @@ func (i *Importer) FileSet() *token.FileSet {
7981
}
8082

8183
func (i *Importer) importPath(path string, src string) (string, string, error) {
82-
if filepath.HasPrefix(path, ".") {
83-
pwd, err := os.Getwd()
84+
if !filepath.HasPrefix(src, "/") {
85+
abs, err := filepath.Abs(src)
8486
if err != nil {
8587
return "", "", err
8688
}
87-
src = pwd
88-
} else {
89-
if filepath.HasPrefix(src, ".") {
90-
pwd, err := os.Getwd()
91-
if err != nil {
92-
return "", "", err
89+
src = abs
90+
}
91+
92+
// If modules are not enabled, then the in-process code works fine and we should keep using it.
93+
switch os.Getenv("GO111MODULE") {
94+
case "off":
95+
return path, src, nil
96+
case "on":
97+
// ok
98+
default: // "", "auto", anything else
99+
// Automatic mode: no module use in $GOPATH/src.
100+
for _, root := range i.gopath {
101+
if filepath.HasPrefix(src, filepath.Join(root, "src")) {
102+
return path, src, nil
93103
}
94-
src = filepath.Join(pwd, src)
95-
} else {
96-
src = filepath.Clean(src)
97104
}
98-
if !filepath.HasPrefix(src, "/") {
99-
gopath := filepath.Join(i.ctx.GOPATH, "src")
100-
src = filepath.Join(gopath, src)
105+
}
106+
107+
for _, root := range i.gopath {
108+
if filepath.HasPrefix(src, filepath.Join(root, "pkg", "mod")) {
109+
src, _ = os.Getwd()
110+
return path, src, nil
101111
}
102112
}
103113

114+
// Look to see if there is a go.mod.
115+
abs := src
116+
for {
117+
info, err := os.Stat(filepath.Join(abs, "go.mod"))
118+
if err == nil && !info.IsDir() {
119+
break
120+
}
121+
d, _ := filepath.Split(abs)
122+
if len(d) >= len(abs) {
123+
return path, src, nil
124+
}
125+
abs = d
126+
}
127+
128+
src = abs
129+
104130
return path, src, nil
105131
}
106132

@@ -171,7 +197,7 @@ func (i *Importer) Import(path string, src string) (Type, error) {
171197
}
172198

173199
for _, v := range p {
174-
np := newParser(i, i.isCommentLocator, imp.ImportPath, imp.Goroot)
200+
np := newParser(i, i.isCommentLocator, dir, imp.ImportPath, imp.Goroot)
175201
t := np.ParsePackage(v)
176202
i.bufType[dir] = t
177203
return t, nil

info.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ package gotype
66
type info struct {
77
Named types // var, func, type, packgae
88
Methods map[string]types // type method
9+
Src string
910
PkgPath string
1011
Goroot bool
1112
}
1213

13-
func newInfo(pkg string, goroot bool) *info {
14+
func newInfo(src string, pkg string, goroot bool) *info {
1415
return &info{
16+
Src: src,
1517
PkgPath: pkg,
1618
Goroot: goroot,
1719
Methods: map[string]types{},

parser.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ type parser struct {
1414
}
1515

1616
// NewParser
17-
func newParser(i importer, c bool, pkg string, goroot bool) *parser {
17+
func newParser(i importer, c bool, src string, pkg string, goroot bool) *parser {
1818
r := &parser{
1919
importer: i,
2020
isCommentLocator: c,
21-
info: newInfo(pkg, goroot),
21+
info: newInfo(src, pkg, goroot),
2222
}
2323
return r
2424
}
@@ -138,7 +138,7 @@ func (r *parser) parseImport(decl *ast.GenDecl) {
138138
}
139139

140140
if s.Name == nil {
141-
tt := newTypeImport("", path, r.info.PkgPath, r.importer)
141+
tt := newTypeImport("", path, r.info.Src, r.importer)
142142
tt = newTypeOrigin(tt, s, r.info, doc, comment)
143143
r.info.Named.AddNoRepeat(tt)
144144
} else {
@@ -157,7 +157,7 @@ func (r *parser) parseImport(decl *ast.GenDecl) {
157157
r.info.Named.AddNoRepeat(tt)
158158
}
159159
default:
160-
tt := newTypeImport(s.Name.Name, path, r.info.PkgPath, r.importer)
160+
tt := newTypeImport(s.Name.Name, path, r.info.Src, r.importer)
161161
tt = newTypeOrigin(tt, s, r.info, doc, comment)
162162
r.info.Named.AddNoRepeat(tt)
163163
}

0 commit comments

Comments
 (0)