MCPcopy
hub / github.com/golang/tools / getPackage

Method getPackage

gopls/internal/cache/check.go:367–426  ·  view source on GitHub ↗

getPackage type checks one [Package] in the batch.

(ctx context.Context, ph *packageHandle)

Source from the content-addressed store, hash-verified

365
366// getPackage type checks one [Package] in the batch.
367func (b *typeCheckBatch) getPackage(ctx context.Context, ph *packageHandle) (*Package, error) {
368 return b.syntaxPackages.get(ctx, ph.mp.ID, func(ctx context.Context) (*Package, error) {
369 // Wait for predecessors.
370 // Record imports of this package to avoid redundant work in typesConfig.
371 imports := make(map[PackagePath]*types.Package)
372 fset := b.fset
373 if ph.state >= validImports {
374 for _, imp := range ph.pkgData.imports {
375 imports[PackagePath(imp.Path())] = imp
376 }
377 // Reusing imports requires that their positions are mapped by the FileSet.
378 fset = tokeninternal.CloneFileSet(ph.pkgData.fset)
379 } else {
380 var impMu sync.Mutex
381 var g errgroup.Group
382 for depPath, depID := range ph.mp.DepsByPkgPath {
383 g.Go(func() error {
384 imp, err := b.getImportPackage(ctx, depID)
385 if err == nil {
386 impMu.Lock()
387 imports[depPath] = imp
388 impMu.Unlock()
389 }
390 return err
391 })
392 }
393 if err := g.Wait(); err != nil {
394 // Failure to import a package should not abort the whole operation.
395 // Stop only if the context was cancelled, a likely cause.
396 // Import errors will be reported as type diagnostics.
397 if ctx.Err() != nil {
398 return nil, ctx.Err()
399 }
400 }
401 }
402
403 // Wait to acquire a CPU token.
404 //
405 // Note: it is important to acquire this token only after awaiting
406 // predecessors, to avoid starvation.
407 select {
408 case <-ctx.Done():
409 return nil, ctx.Err()
410 case b.cpulimit <- unit{}:
411 defer func() {
412 <-b.cpulimit // release CPU token
413 }()
414 }
415
416 // Compute the syntax package.
417 p, err := b.checkPackage(ctx, fset, ph, imports)
418 if err != nil {
419 return nil, err // e.g. I/O error, cancelled
420 }
421
422 // Update caches.
423 go storePackageResults(ctx, ph, p) // ...and write all packages to disk
424 return p, nil

Callers 2

handleSyntaxPackageMethod · 0.95
typeCheckMethod · 0.45

Calls 13

GoMethod · 0.95
getImportPackageMethod · 0.95
LockMethod · 0.95
UnlockMethod · 0.95
checkPackageMethod · 0.95
CloneFileSetFunction · 0.92
PackagePathTypeAlias · 0.85
storePackageResultsFunction · 0.85
PathMethod · 0.80
ErrMethod · 0.65
DoneMethod · 0.65
getMethod · 0.45

Tested by

no test coverage detected