@@ -39,11 +39,6 @@ func NewImporter(options ...Option) *Importer {
39
39
return i
40
40
}
41
41
42
- // Import returns go package scope
43
- func (i * Importer ) Import (path , src string ) (Type , error ) {
44
- return i .importParse (path , src )
45
- }
46
-
47
42
// ImportPackage returns go package scope
48
43
func (i * Importer ) ImportPackage (path string , pkg * ast.Package ) (Type , error ) {
49
44
np := newParser (i , i .isCommentLocator , path , false )
@@ -79,7 +74,14 @@ func (i *Importer) ImportBuild(path string, src string) (*build.Package, error)
79
74
src = filepath .Clean (src )
80
75
gopath := filepath .Join (i .ctx .GOPATH , "src" )
81
76
rsrc := src
82
- if ! filepath .HasPrefix (src , gopath ) {
77
+
78
+ if filepath .HasPrefix (src , "." ) {
79
+ pwd , err := os .Getwd ()
80
+ if err != nil {
81
+ return nil , err
82
+ }
83
+ rsrc = filepath .Join (pwd , src )
84
+ } else if ! filepath .HasPrefix (src , gopath ) {
83
85
rsrc = filepath .Join (gopath , src )
84
86
}
85
87
k := path + " " + rsrc
@@ -88,22 +90,31 @@ func (i *Importer) ImportBuild(path string, src string) (*build.Package, error)
88
90
}
89
91
imp , err := i .ctx .Import (path , rsrc , 0 )
90
92
if err != nil {
91
- i .errorHandler (err )
93
+ i .appendError (err )
92
94
return nil , err
93
95
}
94
96
i .bufBuild [k ] = imp
95
97
return imp , nil
96
98
}
97
99
98
- func (i * Importer ) importName (path string , src string ) (name string , goroot bool ) {
100
+ // appendError append error
101
+ func (i * Importer ) appendError (err error ) {
102
+ if i .errorHandler != nil {
103
+ i .errorHandler (err )
104
+ }
105
+ }
106
+
107
+ // ImportName returns go package name
108
+ func (i * Importer ) ImportName (path string , src string ) (name string , goroot bool ) {
99
109
imp , err := i .ImportBuild (path , src )
100
110
if err != nil {
101
111
return "" , false
102
112
}
103
113
return imp .Name , imp .Goroot
104
114
}
105
115
106
- func (i * Importer ) importParse (path string , src string ) (Type , error ) {
116
+ // Import returns go package scope
117
+ func (i * Importer ) Import (path string , src string ) (Type , error ) {
107
118
imp , err := i .ImportBuild (path , src )
108
119
if err != nil {
109
120
return nil , err
@@ -125,7 +136,7 @@ func (i *Importer) importParse(path string, src string) (Type, error) {
125
136
}, i .mode )
126
137
127
138
if err != nil {
128
- i .errorHandler (err )
139
+ i .appendError (err )
129
140
return nil , err
130
141
}
131
142
@@ -136,6 +147,6 @@ func (i *Importer) importParse(path string, src string) (Type, error) {
136
147
return t , nil
137
148
}
138
149
err = fmt .Errorf (`No go source code was found under the package path "%s"` , path )
139
- i .errorHandler (err )
150
+ i .appendError (err )
140
151
return nil , err
141
152
}
0 commit comments