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

Function buildMetadata

gopls/internal/cache/load.go:392–575  ·  view source on GitHub ↗

buildMetadata populates the updates map with metadata updates to apply, based on the given pkg. It recurs through pkg.Imports to ensure that metadata exists for all dependencies. Returns the metadata.Package that was built (or which was already present in updates), or nil if the package could not b

(updates map[PackageID]*metadata.Package, loadDir string, standalone bool, pkg *packages.Package)

Source from the content-addressed store, hash-verified

390// updates), or nil if the package could not be built. Notably, the resulting
391// metadata.Package may have an ID that differs from pkg.ID.
392func buildMetadata(updates map[PackageID]*metadata.Package, loadDir string, standalone bool, pkg *packages.Package) *metadata.Package {
393 // Allow for multiple ad-hoc packages in the workspace (see #47584).
394 pkgPath := PackagePath(pkg.PkgPath)
395 id := PackageID(pkg.ID)
396
397 if metadata.IsCommandLineArguments(id) {
398 var f string // file to use as disambiguating suffix
399 if len(pkg.GoFiles) > 0 {
400 f = pkg.GoFiles[0]
401
402 // If there are multiple files, we can't use only the first. Note that we
403 // consider GoFiles, rather than CompiledGoFiles, as there can be
404 // multiple CompiledGoFiles in the presence of cgo processing, whereas a
405 // command-line-arguments package should always have exactly one nominal
406 // Go source file. (See golang/go#64557.)
407 if len(pkg.GoFiles) > 1 {
408 bug.Reportf("unexpected files in command-line-arguments package: %v", pkg.GoFiles)
409 return nil
410 }
411 } else if len(pkg.IgnoredFiles) > 0 {
412 // A file=empty.go query results in IgnoredFiles=[empty.go].
413 f = pkg.IgnoredFiles[0]
414 } else {
415 bug.Reportf("command-line-arguments package has neither GoFiles nor IgnoredFiles")
416 return nil
417 }
418 id = PackageID(pkg.ID + f)
419 pkgPath = PackagePath(pkg.PkgPath + f)
420 }
421
422 // Duplicate?
423 if existing, ok := updates[id]; ok {
424 // A package was encountered twice due to shared
425 // subgraphs (common) or cycles (rare). Although "go
426 // list" usually breaks cycles, we don't rely on it.
427 // breakImportCycles in metadataGraph.Clone takes care
428 // of it later.
429 return existing
430 }
431
432 if pkg.TypesSizes == nil {
433 panic(id + ".TypeSizes is nil")
434 }
435
436 // Recreate the metadata rather than reusing it to avoid locking.
437 mp := &metadata.Package{
438 ID: id,
439 PkgPath: pkgPath,
440 Name: PackageName(pkg.Name),
441 ForTest: PackagePath(pkg.ForTest),
442 TypesSizes: pkg.TypesSizes,
443 LoadDir: loadDir,
444 Module: pkg.Module,
445 Errors: pkg.Errors,
446 DepsErrors: packagesinternal.GetDepsErrors(pkg),
447 Standalone: standalone,
448 }
449

Callers 2

loadMethod · 0.85
loadPackagesFunction · 0.85

Calls 8

IsCommandLineArgumentsFunction · 0.92
ReportfFunction · 0.92
URIFromPathFunction · 0.92
PackagePathTypeAlias · 0.85
PackageIDTypeAlias · 0.85
PackageNameTypeAlias · 0.85
appendFunction · 0.85
ImportPathTypeAlias · 0.85

Tested by 1

loadPackagesFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…