This function is a compressed version of snapshot.load from the internal/cache package, for use in testing. TODO(rfindley): it may be valuable to extract this logic from the snapshot, since it is otherwise standalone.
(query string, needExport bool)
| 328 | // TODO(rfindley): it may be valuable to extract this logic from the snapshot, |
| 329 | // since it is otherwise standalone. |
| 330 | func loadPackages(query string, needExport bool) (map[PackageID]string, MetadataSource, error) { |
| 331 | cfg := &packages.Config{ |
| 332 | Dir: *dir, |
| 333 | Mode: packages.NeedName | |
| 334 | packages.NeedFiles | |
| 335 | packages.NeedCompiledGoFiles | |
| 336 | packages.NeedImports | |
| 337 | packages.NeedDeps | |
| 338 | packages.NeedTypesSizes | |
| 339 | packages.NeedModule | |
| 340 | packages.NeedEmbedFiles | |
| 341 | packages.LoadMode(packagesinternal.DepsErrors) | |
| 342 | packages.NeedForTest, |
| 343 | Tests: true, |
| 344 | } |
| 345 | if needExport { |
| 346 | cfg.Mode |= packages.NeedExportFile // ExportFile is not requested by gopls: this is used to verify reachability |
| 347 | } |
| 348 | pkgs, err := packages.Load(cfg, query) |
| 349 | if err != nil { |
| 350 | return nil, nil, err |
| 351 | } |
| 352 | |
| 353 | meta := make(map[PackageID]*Metadata) |
| 354 | var buildMetadata func(pkg *packages.Package) |
| 355 | buildMetadata = func(pkg *packages.Package) { |
| 356 | id := PackageID(pkg.ID) |
| 357 | if meta[id] != nil { |
| 358 | return |
| 359 | } |
| 360 | mp := &Metadata{ |
| 361 | ID: id, |
| 362 | PkgPath: PackagePath(pkg.PkgPath), |
| 363 | Name: packageName(pkg.Name), |
| 364 | ForTest: PackagePath(pkg.ForTest), |
| 365 | TypesSizes: pkg.TypesSizes, |
| 366 | LoadDir: cfg.Dir, |
| 367 | Module: pkg.Module, |
| 368 | Errors: pkg.Errors, |
| 369 | DepsErrors: packagesinternal.GetDepsErrors(pkg), |
| 370 | } |
| 371 | meta[id] = mp |
| 372 | |
| 373 | for _, filename := range pkg.CompiledGoFiles { |
| 374 | mp.CompiledGoFiles = append(mp.CompiledGoFiles, protocol.URIFromPath(filename)) |
| 375 | } |
| 376 | for _, filename := range pkg.GoFiles { |
| 377 | mp.GoFiles = append(mp.GoFiles, protocol.URIFromPath(filename)) |
| 378 | } |
| 379 | |
| 380 | mp.DepsByImpPath = make(map[ImportPath]PackageID) |
| 381 | mp.DepsByPkgPath = make(map[PackagePath]PackageID) |
| 382 | for importPath, imported := range pkg.Imports { |
| 383 | importPath := ImportPath(importPath) |
| 384 | |
| 385 | // see note in gopls/internal/cache/load.go for an explanation of this check. |
| 386 | if importPath != "unsafe" && len(imported.CompiledGoFiles) == 0 { |
| 387 | mp.DepsByImpPath[importPath] = "" // missing |
no test coverage detected
searching dependent graphs…