Skip to content

Commit d532c07

Browse files
committed
internal/lsp: fix race condition in type-checking
Change-Id: Idff7cc3a28b4a03ce0d374c6c5035b51330143b1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/178724 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent d487f80 commit d532c07

File tree

1 file changed

+22
-25
lines changed

1 file changed

+22
-25
lines changed

internal/lsp/cache/check.go

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (v *view) parse(ctx context.Context, f *goFile) ([]packages.Error, error) {
5151
go imp.Import(importPath)
5252
}
5353
// Type-check package.
54-
pkg, err := imp.typeCheck(f.meta.pkgPath)
54+
pkg, err := imp.getPkg(f.meta.pkgPath)
5555
if pkg == nil || pkg.GetTypes() == nil {
5656
return nil, err
5757
}
@@ -181,6 +181,14 @@ type importer struct {
181181
}
182182

183183
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) {
184192
if _, ok := imp.seen[pkgPath]; ok {
185193
return nil, fmt.Errorf("circular import detected")
186194
}
@@ -205,7 +213,7 @@ func (imp *importer) Import(pkgPath string) (*types.Package, error) {
205213
if e.err != nil {
206214
return nil, e.err
207215
}
208-
return e.pkg.types, nil
216+
return e.pkg, nil
209217
}
210218

211219
func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
@@ -266,32 +274,32 @@ func (imp *importer) typeCheck(pkgPath string) (*pkg, error) {
266274
check.Files(pkg.syntax)
267275

268276
// Add every file in this package to our cache.
269-
imp.view.cachePackage(imp.ctx, pkg, meta)
277+
imp.cachePackage(imp.ctx, pkg, meta)
270278

271279
return pkg, nil
272280
}
273281

274-
func (v *view) cachePackage(ctx context.Context, pkg *pkg, meta *metadata) {
282+
func (imp *importer) cachePackage(ctx context.Context, pkg *pkg, meta *metadata) {
275283
for _, file := range pkg.GetSyntax() {
276284
// TODO: If a file is in multiple packages, which package do we store?
277285
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)
279287
continue
280288
}
281-
tok := v.Session().Cache().FileSet().File(file.Pos())
289+
tok := imp.view.Session().Cache().FileSet().File(file.Pos())
282290
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)
284292
continue
285293
}
286294
fURI := span.FileURI(tok.Name())
287-
f, err := v.getFile(fURI)
295+
f, err := imp.view.getFile(fURI)
288296
if err != nil {
289-
v.Session().Logger().Errorf(ctx, "no file: %v", err)
297+
imp.view.Session().Logger().Errorf(ctx, "no file: %v", err)
290298
continue
291299
}
292300
gof, ok := f.(*goFile)
293301
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())
295303
continue
296304
}
297305
gof.token = tok
@@ -300,26 +308,15 @@ func (v *view) cachePackage(ctx context.Context, pkg *pkg, meta *metadata) {
300308
gof.pkg = pkg
301309
}
302310

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-
316311
// Set imports of package to correspond to cached packages.
317312
// We lock the package cache, but we shouldn't get any inconsistencies
318313
// because we are still holding the lock on the view.
319314
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
322318
}
319+
pkg.imports[importPath] = importPkg
323320
}
324321
}
325322

0 commit comments

Comments
 (0)