@@ -51,7 +51,7 @@ func (v *view) parse(ctx context.Context, f *goFile) ([]packages.Error, error) {
51
51
go imp .Import (importPath )
52
52
}
53
53
// Type-check package.
54
- pkg , err := imp .typeCheck (f .meta .pkgPath )
54
+ pkg , err := imp .getPkg (f .meta .pkgPath )
55
55
if pkg == nil || pkg .GetTypes () == nil {
56
56
return nil , err
57
57
}
@@ -181,6 +181,14 @@ type importer struct {
181
181
}
182
182
183
183
func (imp * importer ) Import (pkgPath string ) (* types.Package , error ) {
184
+ pkg , err := imp .getPkg (pkgPath )
185
+ if err != nil {
186
+ return nil , err
187
+ }
188
+ return pkg .types , nil
189
+ }
190
+
191
+ func (imp * importer ) getPkg (pkgPath string ) (* pkg , error ) {
184
192
if _ , ok := imp .seen [pkgPath ]; ok {
185
193
return nil , fmt .Errorf ("circular import detected" )
186
194
}
@@ -205,7 +213,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
205
213
if e .err != nil {
206
214
return nil , e .err
207
215
}
208
- return e .pkg . types , nil
216
+ return e .pkg , nil
209
217
}
210
218
211
219
func (imp * importer ) typeCheck (pkgPath string ) (* pkg , error ) {
@@ -266,32 +274,32 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
266
274
check .Files (pkg .syntax )
267
275
268
276
// Add every file in this package to our cache.
269
- imp .view . cachePackage (imp .ctx , pkg , meta )
277
+ imp .cachePackage (imp .ctx , pkg , meta )
270
278
271
279
return pkg , nil
272
280
}
273
281
274
- func (v * view ) cachePackage (ctx context.Context , pkg * pkg , meta * metadata ) {
282
+ func (imp * importer ) cachePackage (ctx context.Context , pkg * pkg , meta * metadata ) {
275
283
for _ , file := range pkg .GetSyntax () {
276
284
// TODO: If a file is in multiple packages, which package do we store?
277
285
if ! file .Pos ().IsValid () {
278
- v .Session ().Logger ().Errorf (ctx , "invalid position for file %v" , file .Name )
286
+ imp . view .Session ().Logger ().Errorf (ctx , "invalid position for file %v" , file .Name )
279
287
continue
280
288
}
281
- tok := v .Session ().Cache ().FileSet ().File (file .Pos ())
289
+ tok := imp . view .Session ().Cache ().FileSet ().File (file .Pos ())
282
290
if tok == nil {
283
- v .Session ().Logger ().Errorf (ctx , "no token.File for %v" , file .Name )
291
+ imp . view .Session ().Logger ().Errorf (ctx , "no token.File for %v" , file .Name )
284
292
continue
285
293
}
286
294
fURI := span .FileURI (tok .Name ())
287
- f , err := v .getFile (fURI )
295
+ f , err := imp . view .getFile (fURI )
288
296
if err != nil {
289
- v .Session ().Logger ().Errorf (ctx , "no file: %v" , err )
297
+ imp . view .Session ().Logger ().Errorf (ctx , "no file: %v" , err )
290
298
continue
291
299
}
292
300
gof , ok := f .(* goFile )
293
301
if ! ok {
294
- v .Session ().Logger ().Errorf (ctx , "not a go file: %v" , f .URI ())
302
+ imp . view .Session ().Logger ().Errorf (ctx , "not a go file: %v" , f .URI ())
295
303
continue
296
304
}
297
305
gof .token = tok
@@ -300,26 +308,15 @@ func (v *view) cachePackage(ctx context.Context, pkg *pkg, meta *metadata) {
300
308
gof .pkg = pkg
301
309
}
302
310
303
- v .pcache .mu .Lock ()
304
- defer v .pcache .mu .Unlock ()
305
-
306
- // Cache the entry for this package.
307
- // All dependencies are cached through calls to *imp.Import.
308
- e := & entry {
309
- pkg : pkg ,
310
- err : nil ,
311
- ready : make (chan struct {}),
312
- }
313
- close (e .ready )
314
- v .pcache .packages [pkg .pkgPath ] = e
315
-
316
311
// Set imports of package to correspond to cached packages.
317
312
// We lock the package cache, but we shouldn't get any inconsistencies
318
313
// because we are still holding the lock on the view.
319
314
for importPath := range meta .children {
320
- if importEntry , ok := v .pcache .packages [importPath ]; ok {
321
- pkg .imports [importPath ] = importEntry .pkg
315
+ importPkg , err := imp .getPkg (importPath )
316
+ if err != nil {
317
+ continue
322
318
}
319
+ pkg .imports [importPath ] = importPkg
323
320
}
324
321
}
325
322
0 commit comments