packages.go (42539B)
1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package packages 6 7 // See doc.go for package documentation and implementation notes. 8 9 import ( 10 "context" 11 "encoding/json" 12 "fmt" 13 "go/ast" 14 "go/parser" 15 "go/scanner" 16 "go/token" 17 "go/types" 18 "io" 19 "io/ioutil" 20 "log" 21 "os" 22 "path/filepath" 23 "runtime" 24 "strings" 25 "sync" 26 "time" 27 28 "golang.org/x/tools/go/gcexportdata" 29 "golang.org/x/tools/internal/gocommand" 30 "golang.org/x/tools/internal/packagesinternal" 31 "golang.org/x/tools/internal/typeparams" 32 "golang.org/x/tools/internal/typesinternal" 33 ) 34 35 // A LoadMode controls the amount of detail to return when loading. 36 // The bits below can be combined to specify which fields should be 37 // filled in the result packages. 38 // The zero value is a special case, equivalent to combining 39 // the NeedName, NeedFiles, and NeedCompiledGoFiles bits. 40 // ID and Errors (if present) will always be filled. 41 // Load may return more information than requested. 42 type LoadMode int 43 44 const ( 45 // NeedName adds Name and PkgPath. 46 NeedName LoadMode = 1 << iota 47 48 // NeedFiles adds GoFiles and OtherFiles. 49 NeedFiles 50 51 // NeedCompiledGoFiles adds CompiledGoFiles. 52 NeedCompiledGoFiles 53 54 // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain 55 // "placeholder" Packages with only the ID set. 56 NeedImports 57 58 // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. 59 NeedDeps 60 61 // NeedExportFile adds ExportFile. 62 NeedExportFile 63 64 // NeedTypes adds Types, Fset, and IllTyped. 65 NeedTypes 66 67 // NeedSyntax adds Syntax. 68 NeedSyntax 69 70 // NeedTypesInfo adds TypesInfo. 71 NeedTypesInfo 72 73 // NeedTypesSizes adds TypesSizes. 74 NeedTypesSizes 75 76 // needInternalDepsErrors adds the internal deps errors field for use by gopls. 77 needInternalDepsErrors 78 79 // needInternalForTest adds the internal forTest field. 80 // Tests must also be set on the context for this field to be populated. 81 needInternalForTest 82 83 // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+. 84 // Modifies CompiledGoFiles and Types, and has no effect on its own. 85 typecheckCgo 86 87 // NeedModule adds Module. 88 NeedModule 89 90 // NeedEmbedFiles adds EmbedFiles. 91 NeedEmbedFiles 92 93 // NeedEmbedPatterns adds EmbedPatterns. 94 NeedEmbedPatterns 95 ) 96 97 const ( 98 // Deprecated: LoadFiles exists for historical compatibility 99 // and should not be used. Please directly specify the needed fields using the Need values. 100 LoadFiles = NeedName | NeedFiles | NeedCompiledGoFiles 101 102 // Deprecated: LoadImports exists for historical compatibility 103 // and should not be used. Please directly specify the needed fields using the Need values. 104 LoadImports = LoadFiles | NeedImports 105 106 // Deprecated: LoadTypes exists for historical compatibility 107 // and should not be used. Please directly specify the needed fields using the Need values. 108 LoadTypes = LoadImports | NeedTypes | NeedTypesSizes 109 110 // Deprecated: LoadSyntax exists for historical compatibility 111 // and should not be used. Please directly specify the needed fields using the Need values. 112 LoadSyntax = LoadTypes | NeedSyntax | NeedTypesInfo 113 114 // Deprecated: LoadAllSyntax exists for historical compatibility 115 // and should not be used. Please directly specify the needed fields using the Need values. 116 LoadAllSyntax = LoadSyntax | NeedDeps 117 118 // Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile. 119 NeedExportsFile = NeedExportFile 120 ) 121 122 // A Config specifies details about how packages should be loaded. 123 // The zero value is a valid configuration. 124 // Calls to Load do not modify this struct. 125 type Config struct { 126 // Mode controls the level of information returned for each package. 127 Mode LoadMode 128 129 // Context specifies the context for the load operation. 130 // If the context is cancelled, the loader may stop early 131 // and return an ErrCancelled error. 132 // If Context is nil, the load cannot be cancelled. 133 Context context.Context 134 135 // Logf is the logger for the config. 136 // If the user provides a logger, debug logging is enabled. 137 // If the GOPACKAGESDEBUG environment variable is set to true, 138 // but the logger is nil, default to log.Printf. 139 Logf func(format string, args ...interface{}) 140 141 // Dir is the directory in which to run the build system's query tool 142 // that provides information about the packages. 143 // If Dir is empty, the tool is run in the current directory. 144 Dir string 145 146 // Env is the environment to use when invoking the build system's query tool. 147 // If Env is nil, the current environment is used. 148 // As in os/exec's Cmd, only the last value in the slice for 149 // each environment key is used. To specify the setting of only 150 // a few variables, append to the current environment, as in: 151 // 152 // opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386") 153 // 154 Env []string 155 156 // gocmdRunner guards go command calls from concurrency errors. 157 gocmdRunner *gocommand.Runner 158 159 // BuildFlags is a list of command-line flags to be passed through to 160 // the build system's query tool. 161 BuildFlags []string 162 163 // modFile will be used for -modfile in go command invocations. 164 modFile string 165 166 // modFlag will be used for -modfile in go command invocations. 167 modFlag string 168 169 // Fset provides source position information for syntax trees and types. 170 // If Fset is nil, Load will use a new fileset, but preserve Fset's value. 171 Fset *token.FileSet 172 173 // ParseFile is called to read and parse each file 174 // when preparing a package's type-checked syntax tree. 175 // It must be safe to call ParseFile simultaneously from multiple goroutines. 176 // If ParseFile is nil, the loader will uses parser.ParseFile. 177 // 178 // ParseFile should parse the source from src and use filename only for 179 // recording position information. 180 // 181 // An application may supply a custom implementation of ParseFile 182 // to change the effective file contents or the behavior of the parser, 183 // or to modify the syntax tree. For example, selectively eliminating 184 // unwanted function bodies can significantly accelerate type checking. 185 ParseFile func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) 186 187 // If Tests is set, the loader includes not just the packages 188 // matching a particular pattern but also any related test packages, 189 // including test-only variants of the package and the test executable. 190 // 191 // For example, when using the go command, loading "fmt" with Tests=true 192 // returns four packages, with IDs "fmt" (the standard package), 193 // "fmt [fmt.test]" (the package as compiled for the test), 194 // "fmt_test" (the test functions from source files in package fmt_test), 195 // and "fmt.test" (the test binary). 196 // 197 // In build systems with explicit names for tests, 198 // setting Tests may have no effect. 199 Tests bool 200 201 // Overlay provides a mapping of absolute file paths to file contents. 202 // If the file with the given path already exists, the parser will use the 203 // alternative file contents provided by the map. 204 // 205 // Overlays provide incomplete support for when a given file doesn't 206 // already exist on disk. See the package doc above for more details. 207 Overlay map[string][]byte 208 } 209 210 // driver is the type for functions that query the build system for the 211 // packages named by the patterns. 212 type driver func(cfg *Config, patterns ...string) (*driverResponse, error) 213 214 // driverResponse contains the results for a driver query. 215 type driverResponse struct { 216 // NotHandled is returned if the request can't be handled by the current 217 // driver. If an external driver returns a response with NotHandled, the 218 // rest of the driverResponse is ignored, and go/packages will fallback 219 // to the next driver. If go/packages is extended in the future to support 220 // lists of multiple drivers, go/packages will fall back to the next driver. 221 NotHandled bool 222 223 // Sizes, if not nil, is the types.Sizes to use when type checking. 224 Sizes *types.StdSizes 225 226 // Roots is the set of package IDs that make up the root packages. 227 // We have to encode this separately because when we encode a single package 228 // we cannot know if it is one of the roots as that requires knowledge of the 229 // graph it is part of. 230 Roots []string `json:",omitempty"` 231 232 // Packages is the full set of packages in the graph. 233 // The packages are not connected into a graph. 234 // The Imports if populated will be stubs that only have their ID set. 235 // Imports will be connected and then type and syntax information added in a 236 // later pass (see refine). 237 Packages []*Package 238 239 // GoVersion is the minor version number used by the driver 240 // (e.g. the go command on the PATH) when selecting .go files. 241 // Zero means unknown. 242 GoVersion int 243 } 244 245 // Load loads and returns the Go packages named by the given patterns. 246 // 247 // Config specifies loading options; 248 // nil behaves the same as an empty Config. 249 // 250 // Load returns an error if any of the patterns was invalid 251 // as defined by the underlying build system. 252 // It may return an empty list of packages without an error, 253 // for instance for an empty expansion of a valid wildcard. 254 // Errors associated with a particular package are recorded in the 255 // corresponding Package's Errors list, and do not cause Load to 256 // return an error. Clients may need to handle such errors before 257 // proceeding with further analysis. The PrintErrors function is 258 // provided for convenient display of all errors. 259 func Load(cfg *Config, patterns ...string) ([]*Package, error) { 260 l := newLoader(cfg) 261 response, err := defaultDriver(&l.Config, patterns...) 262 if err != nil { 263 return nil, err 264 } 265 l.sizes = response.Sizes 266 return l.refine(response) 267 } 268 269 // defaultDriver is a driver that implements go/packages' fallback behavior. 270 // It will try to request to an external driver, if one exists. If there's 271 // no external driver, or the driver returns a response with NotHandled set, 272 // defaultDriver will fall back to the go list driver. 273 func defaultDriver(cfg *Config, patterns ...string) (*driverResponse, error) { 274 driver := findExternalDriver(cfg) 275 if driver == nil { 276 driver = goListDriver 277 } 278 response, err := driver(cfg, patterns...) 279 if err != nil { 280 return response, err 281 } else if response.NotHandled { 282 return goListDriver(cfg, patterns...) 283 } 284 return response, nil 285 } 286 287 // A Package describes a loaded Go package. 288 type Package struct { 289 // ID is a unique identifier for a package, 290 // in a syntax provided by the underlying build system. 291 // 292 // Because the syntax varies based on the build system, 293 // clients should treat IDs as opaque and not attempt to 294 // interpret them. 295 ID string 296 297 // Name is the package name as it appears in the package source code. 298 Name string 299 300 // PkgPath is the package path as used by the go/types package. 301 PkgPath string 302 303 // Errors contains any errors encountered querying the metadata 304 // of the package, or while parsing or type-checking its files. 305 Errors []Error 306 307 // TypeErrors contains the subset of errors produced during type checking. 308 TypeErrors []types.Error 309 310 // GoFiles lists the absolute file paths of the package's Go source files. 311 GoFiles []string 312 313 // CompiledGoFiles lists the absolute file paths of the package's source 314 // files that are suitable for type checking. 315 // This may differ from GoFiles if files are processed before compilation. 316 CompiledGoFiles []string 317 318 // OtherFiles lists the absolute file paths of the package's non-Go source files, 319 // including assembly, C, C++, Fortran, Objective-C, SWIG, and so on. 320 OtherFiles []string 321 322 // EmbedFiles lists the absolute file paths of the package's files 323 // embedded with go:embed. 324 EmbedFiles []string 325 326 // EmbedPatterns lists the absolute file patterns of the package's 327 // files embedded with go:embed. 328 EmbedPatterns []string 329 330 // IgnoredFiles lists source files that are not part of the package 331 // using the current build configuration but that might be part of 332 // the package using other build configurations. 333 IgnoredFiles []string 334 335 // ExportFile is the absolute path to a file containing type 336 // information for the package as provided by the build system. 337 ExportFile string 338 339 // Imports maps import paths appearing in the package's Go source files 340 // to corresponding loaded Packages. 341 Imports map[string]*Package 342 343 // Types provides type information for the package. 344 // The NeedTypes LoadMode bit sets this field for packages matching the 345 // patterns; type information for dependencies may be missing or incomplete, 346 // unless NeedDeps and NeedImports are also set. 347 Types *types.Package 348 349 // Fset provides position information for Types, TypesInfo, and Syntax. 350 // It is set only when Types is set. 351 Fset *token.FileSet 352 353 // IllTyped indicates whether the package or any dependency contains errors. 354 // It is set only when Types is set. 355 IllTyped bool 356 357 // Syntax is the package's syntax trees, for the files listed in CompiledGoFiles. 358 // 359 // The NeedSyntax LoadMode bit populates this field for packages matching the patterns. 360 // If NeedDeps and NeedImports are also set, this field will also be populated 361 // for dependencies. 362 // 363 // Syntax is kept in the same order as CompiledGoFiles, with the caveat that nils are 364 // removed. If parsing returned nil, Syntax may be shorter than CompiledGoFiles. 365 Syntax []*ast.File 366 367 // TypesInfo provides type information about the package's syntax trees. 368 // It is set only when Syntax is set. 369 TypesInfo *types.Info 370 371 // TypesSizes provides the effective size function for types in TypesInfo. 372 TypesSizes types.Sizes 373 374 // forTest is the package under test, if any. 375 forTest string 376 377 // depsErrors is the DepsErrors field from the go list response, if any. 378 depsErrors []*packagesinternal.PackageError 379 380 // module is the module information for the package if it exists. 381 Module *Module 382 } 383 384 // Module provides module information for a package. 385 type Module struct { 386 Path string // module path 387 Version string // module version 388 Replace *Module // replaced by this module 389 Time *time.Time // time version was created 390 Main bool // is this the main module? 391 Indirect bool // is this module only an indirect dependency of main module? 392 Dir string // directory holding files for this module, if any 393 GoMod string // path to go.mod file used when loading this module, if any 394 GoVersion string // go version used in module 395 Error *ModuleError // error loading module 396 } 397 398 // ModuleError holds errors loading a module. 399 type ModuleError struct { 400 Err string // the error itself 401 } 402 403 func init() { 404 packagesinternal.GetForTest = func(p interface{}) string { 405 return p.(*Package).forTest 406 } 407 packagesinternal.GetDepsErrors = func(p interface{}) []*packagesinternal.PackageError { 408 return p.(*Package).depsErrors 409 } 410 packagesinternal.GetGoCmdRunner = func(config interface{}) *gocommand.Runner { 411 return config.(*Config).gocmdRunner 412 } 413 packagesinternal.SetGoCmdRunner = func(config interface{}, runner *gocommand.Runner) { 414 config.(*Config).gocmdRunner = runner 415 } 416 packagesinternal.SetModFile = func(config interface{}, value string) { 417 config.(*Config).modFile = value 418 } 419 packagesinternal.SetModFlag = func(config interface{}, value string) { 420 config.(*Config).modFlag = value 421 } 422 packagesinternal.TypecheckCgo = int(typecheckCgo) 423 packagesinternal.DepsErrors = int(needInternalDepsErrors) 424 packagesinternal.ForTest = int(needInternalForTest) 425 } 426 427 // An Error describes a problem with a package's metadata, syntax, or types. 428 type Error struct { 429 Pos string // "file:line:col" or "file:line" or "" or "-" 430 Msg string 431 Kind ErrorKind 432 } 433 434 // ErrorKind describes the source of the error, allowing the user to 435 // differentiate between errors generated by the driver, the parser, or the 436 // type-checker. 437 type ErrorKind int 438 439 const ( 440 UnknownError ErrorKind = iota 441 ListError 442 ParseError 443 TypeError 444 ) 445 446 func (err Error) Error() string { 447 pos := err.Pos 448 if pos == "" { 449 pos = "-" // like token.Position{}.String() 450 } 451 return pos + ": " + err.Msg 452 } 453 454 // flatPackage is the JSON form of Package 455 // It drops all the type and syntax fields, and transforms the Imports 456 // 457 // TODO(adonovan): identify this struct with Package, effectively 458 // publishing the JSON protocol. 459 type flatPackage struct { 460 ID string 461 Name string `json:",omitempty"` 462 PkgPath string `json:",omitempty"` 463 Errors []Error `json:",omitempty"` 464 GoFiles []string `json:",omitempty"` 465 CompiledGoFiles []string `json:",omitempty"` 466 OtherFiles []string `json:",omitempty"` 467 EmbedFiles []string `json:",omitempty"` 468 EmbedPatterns []string `json:",omitempty"` 469 IgnoredFiles []string `json:",omitempty"` 470 ExportFile string `json:",omitempty"` 471 Imports map[string]string `json:",omitempty"` 472 } 473 474 // MarshalJSON returns the Package in its JSON form. 475 // For the most part, the structure fields are written out unmodified, and 476 // the type and syntax fields are skipped. 477 // The imports are written out as just a map of path to package id. 478 // The errors are written using a custom type that tries to preserve the 479 // structure of error types we know about. 480 // 481 // This method exists to enable support for additional build systems. It is 482 // not intended for use by clients of the API and we may change the format. 483 func (p *Package) MarshalJSON() ([]byte, error) { 484 flat := &flatPackage{ 485 ID: p.ID, 486 Name: p.Name, 487 PkgPath: p.PkgPath, 488 Errors: p.Errors, 489 GoFiles: p.GoFiles, 490 CompiledGoFiles: p.CompiledGoFiles, 491 OtherFiles: p.OtherFiles, 492 EmbedFiles: p.EmbedFiles, 493 EmbedPatterns: p.EmbedPatterns, 494 IgnoredFiles: p.IgnoredFiles, 495 ExportFile: p.ExportFile, 496 } 497 if len(p.Imports) > 0 { 498 flat.Imports = make(map[string]string, len(p.Imports)) 499 for path, ipkg := range p.Imports { 500 flat.Imports[path] = ipkg.ID 501 } 502 } 503 return json.Marshal(flat) 504 } 505 506 // UnmarshalJSON reads in a Package from its JSON format. 507 // See MarshalJSON for details about the format accepted. 508 func (p *Package) UnmarshalJSON(b []byte) error { 509 flat := &flatPackage{} 510 if err := json.Unmarshal(b, &flat); err != nil { 511 return err 512 } 513 *p = Package{ 514 ID: flat.ID, 515 Name: flat.Name, 516 PkgPath: flat.PkgPath, 517 Errors: flat.Errors, 518 GoFiles: flat.GoFiles, 519 CompiledGoFiles: flat.CompiledGoFiles, 520 OtherFiles: flat.OtherFiles, 521 EmbedFiles: flat.EmbedFiles, 522 EmbedPatterns: flat.EmbedPatterns, 523 ExportFile: flat.ExportFile, 524 } 525 if len(flat.Imports) > 0 { 526 p.Imports = make(map[string]*Package, len(flat.Imports)) 527 for path, id := range flat.Imports { 528 p.Imports[path] = &Package{ID: id} 529 } 530 } 531 return nil 532 } 533 534 func (p *Package) String() string { return p.ID } 535 536 // loaderPackage augments Package with state used during the loading phase 537 type loaderPackage struct { 538 *Package 539 importErrors map[string]error // maps each bad import to its error 540 loadOnce sync.Once 541 color uint8 // for cycle detection 542 needsrc bool // load from source (Mode >= LoadTypes) 543 needtypes bool // type information is either requested or depended on 544 initial bool // package was matched by a pattern 545 goVersion int // minor version number of go command on PATH 546 } 547 548 // loader holds the working state of a single call to load. 549 type loader struct { 550 pkgs map[string]*loaderPackage 551 Config 552 sizes types.Sizes 553 parseCache map[string]*parseValue 554 parseCacheMu sync.Mutex 555 exportMu sync.Mutex // enforces mutual exclusion of exportdata operations 556 557 // Config.Mode contains the implied mode (see impliedLoadMode). 558 // Implied mode contains all the fields we need the data for. 559 // In requestedMode there are the actually requested fields. 560 // We'll zero them out before returning packages to the user. 561 // This makes it easier for us to get the conditions where 562 // we need certain modes right. 563 requestedMode LoadMode 564 } 565 566 type parseValue struct { 567 f *ast.File 568 err error 569 ready chan struct{} 570 } 571 572 func newLoader(cfg *Config) *loader { 573 ld := &loader{ 574 parseCache: map[string]*parseValue{}, 575 } 576 if cfg != nil { 577 ld.Config = *cfg 578 // If the user has provided a logger, use it. 579 ld.Config.Logf = cfg.Logf 580 } 581 if ld.Config.Logf == nil { 582 // If the GOPACKAGESDEBUG environment variable is set to true, 583 // but the user has not provided a logger, default to log.Printf. 584 if debug { 585 ld.Config.Logf = log.Printf 586 } else { 587 ld.Config.Logf = func(format string, args ...interface{}) {} 588 } 589 } 590 if ld.Config.Mode == 0 { 591 ld.Config.Mode = NeedName | NeedFiles | NeedCompiledGoFiles // Preserve zero behavior of Mode for backwards compatibility. 592 } 593 if ld.Config.Env == nil { 594 ld.Config.Env = os.Environ() 595 } 596 if ld.Config.gocmdRunner == nil { 597 ld.Config.gocmdRunner = &gocommand.Runner{} 598 } 599 if ld.Context == nil { 600 ld.Context = context.Background() 601 } 602 if ld.Dir == "" { 603 if dir, err := os.Getwd(); err == nil { 604 ld.Dir = dir 605 } 606 } 607 608 // Save the actually requested fields. We'll zero them out before returning packages to the user. 609 ld.requestedMode = ld.Mode 610 ld.Mode = impliedLoadMode(ld.Mode) 611 612 if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 { 613 if ld.Fset == nil { 614 ld.Fset = token.NewFileSet() 615 } 616 617 // ParseFile is required even in LoadTypes mode 618 // because we load source if export data is missing. 619 if ld.ParseFile == nil { 620 ld.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) { 621 const mode = parser.AllErrors | parser.ParseComments 622 return parser.ParseFile(fset, filename, src, mode) 623 } 624 } 625 } 626 627 return ld 628 } 629 630 // refine connects the supplied packages into a graph and then adds type and 631 // and syntax information as requested by the LoadMode. 632 func (ld *loader) refine(response *driverResponse) ([]*Package, error) { 633 roots := response.Roots 634 rootMap := make(map[string]int, len(roots)) 635 for i, root := range roots { 636 rootMap[root] = i 637 } 638 ld.pkgs = make(map[string]*loaderPackage) 639 // first pass, fixup and build the map and roots 640 var initial = make([]*loaderPackage, len(roots)) 641 for _, pkg := range response.Packages { 642 rootIndex := -1 643 if i, found := rootMap[pkg.ID]; found { 644 rootIndex = i 645 } 646 647 // Overlays can invalidate export data. 648 // TODO(matloob): make this check fine-grained based on dependencies on overlaid files 649 exportDataInvalid := len(ld.Overlay) > 0 || pkg.ExportFile == "" && pkg.PkgPath != "unsafe" 650 // This package needs type information if the caller requested types and the package is 651 // either a root, or it's a non-root and the user requested dependencies ... 652 needtypes := (ld.Mode&NeedTypes|NeedTypesInfo != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) 653 // This package needs source if the call requested source (or types info, which implies source) 654 // and the package is either a root, or itas a non- root and the user requested dependencies... 655 needsrc := ((ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) || 656 // ... or if we need types and the exportData is invalid. We fall back to (incompletely) 657 // typechecking packages from source if they fail to compile. 658 (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && exportDataInvalid)) && pkg.PkgPath != "unsafe" 659 lpkg := &loaderPackage{ 660 Package: pkg, 661 needtypes: needtypes, 662 needsrc: needsrc, 663 goVersion: response.GoVersion, 664 } 665 ld.pkgs[lpkg.ID] = lpkg 666 if rootIndex >= 0 { 667 initial[rootIndex] = lpkg 668 lpkg.initial = true 669 } 670 } 671 for i, root := range roots { 672 if initial[i] == nil { 673 return nil, fmt.Errorf("root package %v is missing", root) 674 } 675 } 676 677 // Materialize the import graph. 678 679 const ( 680 white = 0 // new 681 grey = 1 // in progress 682 black = 2 // complete 683 ) 684 685 // visit traverses the import graph, depth-first, 686 // and materializes the graph as Packages.Imports. 687 // 688 // Valid imports are saved in the Packages.Import map. 689 // Invalid imports (cycles and missing nodes) are saved in the importErrors map. 690 // Thus, even in the presence of both kinds of errors, the Import graph remains a DAG. 691 // 692 // visit returns whether the package needs src or has a transitive 693 // dependency on a package that does. These are the only packages 694 // for which we load source code. 695 var stack []*loaderPackage 696 var visit func(lpkg *loaderPackage) bool 697 var srcPkgs []*loaderPackage 698 visit = func(lpkg *loaderPackage) bool { 699 switch lpkg.color { 700 case black: 701 return lpkg.needsrc 702 case grey: 703 panic("internal error: grey node") 704 } 705 lpkg.color = grey 706 stack = append(stack, lpkg) // push 707 stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports 708 // If NeedImports isn't set, the imports fields will all be zeroed out. 709 if ld.Mode&NeedImports != 0 { 710 lpkg.Imports = make(map[string]*Package, len(stubs)) 711 for importPath, ipkg := range stubs { 712 var importErr error 713 imp := ld.pkgs[ipkg.ID] 714 if imp == nil { 715 // (includes package "C" when DisableCgo) 716 importErr = fmt.Errorf("missing package: %q", ipkg.ID) 717 } else if imp.color == grey { 718 importErr = fmt.Errorf("import cycle: %s", stack) 719 } 720 if importErr != nil { 721 if lpkg.importErrors == nil { 722 lpkg.importErrors = make(map[string]error) 723 } 724 lpkg.importErrors[importPath] = importErr 725 continue 726 } 727 728 if visit(imp) { 729 lpkg.needsrc = true 730 } 731 lpkg.Imports[importPath] = imp.Package 732 } 733 } 734 if lpkg.needsrc { 735 srcPkgs = append(srcPkgs, lpkg) 736 } 737 if ld.Mode&NeedTypesSizes != 0 { 738 lpkg.TypesSizes = ld.sizes 739 } 740 stack = stack[:len(stack)-1] // pop 741 lpkg.color = black 742 743 return lpkg.needsrc 744 } 745 746 if ld.Mode&NeedImports == 0 { 747 // We do this to drop the stub import packages that we are not even going to try to resolve. 748 for _, lpkg := range initial { 749 lpkg.Imports = nil 750 } 751 } else { 752 // For each initial package, create its import DAG. 753 for _, lpkg := range initial { 754 visit(lpkg) 755 } 756 } 757 if ld.Mode&NeedImports != 0 && ld.Mode&NeedTypes != 0 { 758 for _, lpkg := range srcPkgs { 759 // Complete type information is required for the 760 // immediate dependencies of each source package. 761 for _, ipkg := range lpkg.Imports { 762 imp := ld.pkgs[ipkg.ID] 763 imp.needtypes = true 764 } 765 } 766 } 767 // Load type data and syntax if needed, starting at 768 // the initial packages (roots of the import DAG). 769 if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 { 770 var wg sync.WaitGroup 771 for _, lpkg := range initial { 772 wg.Add(1) 773 go func(lpkg *loaderPackage) { 774 ld.loadRecursive(lpkg) 775 wg.Done() 776 }(lpkg) 777 } 778 wg.Wait() 779 } 780 781 result := make([]*Package, len(initial)) 782 for i, lpkg := range initial { 783 result[i] = lpkg.Package 784 } 785 for i := range ld.pkgs { 786 // Clear all unrequested fields, 787 // to catch programs that use more than they request. 788 if ld.requestedMode&NeedName == 0 { 789 ld.pkgs[i].Name = "" 790 ld.pkgs[i].PkgPath = "" 791 } 792 if ld.requestedMode&NeedFiles == 0 { 793 ld.pkgs[i].GoFiles = nil 794 ld.pkgs[i].OtherFiles = nil 795 ld.pkgs[i].IgnoredFiles = nil 796 } 797 if ld.requestedMode&NeedEmbedFiles == 0 { 798 ld.pkgs[i].EmbedFiles = nil 799 } 800 if ld.requestedMode&NeedEmbedPatterns == 0 { 801 ld.pkgs[i].EmbedPatterns = nil 802 } 803 if ld.requestedMode&NeedCompiledGoFiles == 0 { 804 ld.pkgs[i].CompiledGoFiles = nil 805 } 806 if ld.requestedMode&NeedImports == 0 { 807 ld.pkgs[i].Imports = nil 808 } 809 if ld.requestedMode&NeedExportFile == 0 { 810 ld.pkgs[i].ExportFile = "" 811 } 812 if ld.requestedMode&NeedTypes == 0 { 813 ld.pkgs[i].Types = nil 814 ld.pkgs[i].Fset = nil 815 ld.pkgs[i].IllTyped = false 816 } 817 if ld.requestedMode&NeedSyntax == 0 { 818 ld.pkgs[i].Syntax = nil 819 } 820 if ld.requestedMode&NeedTypesInfo == 0 { 821 ld.pkgs[i].TypesInfo = nil 822 } 823 if ld.requestedMode&NeedTypesSizes == 0 { 824 ld.pkgs[i].TypesSizes = nil 825 } 826 if ld.requestedMode&NeedModule == 0 { 827 ld.pkgs[i].Module = nil 828 } 829 } 830 831 return result, nil 832 } 833 834 // loadRecursive loads the specified package and its dependencies, 835 // recursively, in parallel, in topological order. 836 // It is atomic and idempotent. 837 // Precondition: ld.Mode&NeedTypes. 838 func (ld *loader) loadRecursive(lpkg *loaderPackage) { 839 lpkg.loadOnce.Do(func() { 840 // Load the direct dependencies, in parallel. 841 var wg sync.WaitGroup 842 for _, ipkg := range lpkg.Imports { 843 imp := ld.pkgs[ipkg.ID] 844 wg.Add(1) 845 go func(imp *loaderPackage) { 846 ld.loadRecursive(imp) 847 wg.Done() 848 }(imp) 849 } 850 wg.Wait() 851 ld.loadPackage(lpkg) 852 }) 853 } 854 855 // loadPackage loads the specified package. 856 // It must be called only once per Package, 857 // after immediate dependencies are loaded. 858 // Precondition: ld.Mode & NeedTypes. 859 func (ld *loader) loadPackage(lpkg *loaderPackage) { 860 if lpkg.PkgPath == "unsafe" { 861 // Fill in the blanks to avoid surprises. 862 lpkg.Types = types.Unsafe 863 lpkg.Fset = ld.Fset 864 lpkg.Syntax = []*ast.File{} 865 lpkg.TypesInfo = new(types.Info) 866 lpkg.TypesSizes = ld.sizes 867 return 868 } 869 870 // Call NewPackage directly with explicit name. 871 // This avoids skew between golist and go/types when the files' 872 // package declarations are inconsistent. 873 lpkg.Types = types.NewPackage(lpkg.PkgPath, lpkg.Name) 874 lpkg.Fset = ld.Fset 875 876 // Subtle: we populate all Types fields with an empty Package 877 // before loading export data so that export data processing 878 // never has to create a types.Package for an indirect dependency, 879 // which would then require that such created packages be explicitly 880 // inserted back into the Import graph as a final step after export data loading. 881 // (Hence this return is after the Types assignment.) 882 // The Diamond test exercises this case. 883 if !lpkg.needtypes && !lpkg.needsrc { 884 return 885 } 886 if !lpkg.needsrc { 887 if err := ld.loadFromExportData(lpkg); err != nil { 888 lpkg.Errors = append(lpkg.Errors, Error{ 889 Pos: "-", 890 Msg: err.Error(), 891 Kind: UnknownError, // e.g. can't find/open/parse export data 892 }) 893 } 894 return // not a source package, don't get syntax trees 895 } 896 897 appendError := func(err error) { 898 // Convert various error types into the one true Error. 899 var errs []Error 900 switch err := err.(type) { 901 case Error: 902 // from driver 903 errs = append(errs, err) 904 905 case *os.PathError: 906 // from parser 907 errs = append(errs, Error{ 908 Pos: err.Path + ":1", 909 Msg: err.Err.Error(), 910 Kind: ParseError, 911 }) 912 913 case scanner.ErrorList: 914 // from parser 915 for _, err := range err { 916 errs = append(errs, Error{ 917 Pos: err.Pos.String(), 918 Msg: err.Msg, 919 Kind: ParseError, 920 }) 921 } 922 923 case types.Error: 924 // from type checker 925 lpkg.TypeErrors = append(lpkg.TypeErrors, err) 926 errs = append(errs, Error{ 927 Pos: err.Fset.Position(err.Pos).String(), 928 Msg: err.Msg, 929 Kind: TypeError, 930 }) 931 932 default: 933 // unexpected impoverished error from parser? 934 errs = append(errs, Error{ 935 Pos: "-", 936 Msg: err.Error(), 937 Kind: UnknownError, 938 }) 939 940 // If you see this error message, please file a bug. 941 log.Printf("internal error: error %q (%T) without position", err, err) 942 } 943 944 lpkg.Errors = append(lpkg.Errors, errs...) 945 } 946 947 // If the go command on the PATH is newer than the runtime, 948 // then the go/{scanner,ast,parser,types} packages from the 949 // standard library may be unable to process the files 950 // selected by go list. 951 // 952 // There is currently no way to downgrade the effective 953 // version of the go command (see issue 52078), so we proceed 954 // with the newer go command but, in case of parse or type 955 // errors, we emit an additional diagnostic. 956 // 957 // See: 958 // - golang.org/issue/52078 (flag to set release tags) 959 // - golang.org/issue/50825 (gopls legacy version support) 960 // - golang.org/issue/55883 (go/packages confusing error) 961 // 962 // Should we assert a hard minimum of (currently) go1.16 here? 963 var runtimeVersion int 964 if _, err := fmt.Sscanf(runtime.Version(), "go1.%d", &runtimeVersion); err == nil && runtimeVersion < lpkg.goVersion { 965 defer func() { 966 if len(lpkg.Errors) > 0 { 967 appendError(Error{ 968 Pos: "-", 969 Msg: fmt.Sprintf("This application uses version go1.%d of the source-processing packages but runs version go1.%d of 'go list'. It may fail to process source files that rely on newer language features. If so, rebuild the application using a newer version of Go.", runtimeVersion, lpkg.goVersion), 970 Kind: UnknownError, 971 }) 972 } 973 }() 974 } 975 976 if ld.Config.Mode&NeedTypes != 0 && len(lpkg.CompiledGoFiles) == 0 && lpkg.ExportFile != "" { 977 // The config requested loading sources and types, but sources are missing. 978 // Add an error to the package and fall back to loading from export data. 979 appendError(Error{"-", fmt.Sprintf("sources missing for package %s", lpkg.ID), ParseError}) 980 _ = ld.loadFromExportData(lpkg) // ignore any secondary errors 981 982 return // can't get syntax trees for this package 983 } 984 985 files, errs := ld.parseFiles(lpkg.CompiledGoFiles) 986 for _, err := range errs { 987 appendError(err) 988 } 989 990 lpkg.Syntax = files 991 if ld.Config.Mode&NeedTypes == 0 { 992 return 993 } 994 995 lpkg.TypesInfo = &types.Info{ 996 Types: make(map[ast.Expr]types.TypeAndValue), 997 Defs: make(map[*ast.Ident]types.Object), 998 Uses: make(map[*ast.Ident]types.Object), 999 Implicits: make(map[ast.Node]types.Object), 1000 Scopes: make(map[ast.Node]*types.Scope), 1001 Selections: make(map[*ast.SelectorExpr]*types.Selection), 1002 } 1003 typeparams.InitInstanceInfo(lpkg.TypesInfo) 1004 lpkg.TypesSizes = ld.sizes 1005 1006 importer := importerFunc(func(path string) (*types.Package, error) { 1007 if path == "unsafe" { 1008 return types.Unsafe, nil 1009 } 1010 1011 // The imports map is keyed by import path. 1012 ipkg := lpkg.Imports[path] 1013 if ipkg == nil { 1014 if err := lpkg.importErrors[path]; err != nil { 1015 return nil, err 1016 } 1017 // There was skew between the metadata and the 1018 // import declarations, likely due to an edit 1019 // race, or because the ParseFile feature was 1020 // used to supply alternative file contents. 1021 return nil, fmt.Errorf("no metadata for %s", path) 1022 } 1023 1024 if ipkg.Types != nil && ipkg.Types.Complete() { 1025 return ipkg.Types, nil 1026 } 1027 log.Fatalf("internal error: package %q without types was imported from %q", path, lpkg) 1028 panic("unreachable") 1029 }) 1030 1031 // type-check 1032 tc := &types.Config{ 1033 Importer: importer, 1034 1035 // Type-check bodies of functions only in initial packages. 1036 // Example: for import graph A->B->C and initial packages {A,C}, 1037 // we can ignore function bodies in B. 1038 IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial, 1039 1040 Error: appendError, 1041 Sizes: ld.sizes, 1042 } 1043 if (ld.Mode & typecheckCgo) != 0 { 1044 if !typesinternal.SetUsesCgo(tc) { 1045 appendError(Error{ 1046 Msg: "typecheckCgo requires Go 1.15+", 1047 Kind: ListError, 1048 }) 1049 return 1050 } 1051 } 1052 types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax) 1053 1054 lpkg.importErrors = nil // no longer needed 1055 1056 // If !Cgo, the type-checker uses FakeImportC mode, so 1057 // it doesn't invoke the importer for import "C", 1058 // nor report an error for the import, 1059 // or for any undefined C.f reference. 1060 // We must detect this explicitly and correctly 1061 // mark the package as IllTyped (by reporting an error). 1062 // TODO(adonovan): if these errors are annoying, 1063 // we could just set IllTyped quietly. 1064 if tc.FakeImportC { 1065 outer: 1066 for _, f := range lpkg.Syntax { 1067 for _, imp := range f.Imports { 1068 if imp.Path.Value == `"C"` { 1069 err := types.Error{Fset: ld.Fset, Pos: imp.Pos(), Msg: `import "C" ignored`} 1070 appendError(err) 1071 break outer 1072 } 1073 } 1074 } 1075 } 1076 1077 // Record accumulated errors. 1078 illTyped := len(lpkg.Errors) > 0 1079 if !illTyped { 1080 for _, imp := range lpkg.Imports { 1081 if imp.IllTyped { 1082 illTyped = true 1083 break 1084 } 1085 } 1086 } 1087 lpkg.IllTyped = illTyped 1088 } 1089 1090 // An importFunc is an implementation of the single-method 1091 // types.Importer interface based on a function value. 1092 type importerFunc func(path string) (*types.Package, error) 1093 1094 func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) } 1095 1096 // We use a counting semaphore to limit 1097 // the number of parallel I/O calls per process. 1098 var ioLimit = make(chan bool, 20) 1099 1100 func (ld *loader) parseFile(filename string) (*ast.File, error) { 1101 ld.parseCacheMu.Lock() 1102 v, ok := ld.parseCache[filename] 1103 if ok { 1104 // cache hit 1105 ld.parseCacheMu.Unlock() 1106 <-v.ready 1107 } else { 1108 // cache miss 1109 v = &parseValue{ready: make(chan struct{})} 1110 ld.parseCache[filename] = v 1111 ld.parseCacheMu.Unlock() 1112 1113 var src []byte 1114 for f, contents := range ld.Config.Overlay { 1115 if sameFile(f, filename) { 1116 src = contents 1117 } 1118 } 1119 var err error 1120 if src == nil { 1121 ioLimit <- true // wait 1122 src, err = ioutil.ReadFile(filename) 1123 <-ioLimit // signal 1124 } 1125 if err != nil { 1126 v.err = err 1127 } else { 1128 v.f, v.err = ld.ParseFile(ld.Fset, filename, src) 1129 } 1130 1131 close(v.ready) 1132 } 1133 return v.f, v.err 1134 } 1135 1136 // parseFiles reads and parses the Go source files and returns the ASTs 1137 // of the ones that could be at least partially parsed, along with a 1138 // list of I/O and parse errors encountered. 1139 // 1140 // Because files are scanned in parallel, the token.Pos 1141 // positions of the resulting ast.Files are not ordered. 1142 func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) { 1143 var wg sync.WaitGroup 1144 n := len(filenames) 1145 parsed := make([]*ast.File, n) 1146 errors := make([]error, n) 1147 for i, file := range filenames { 1148 if ld.Config.Context.Err() != nil { 1149 parsed[i] = nil 1150 errors[i] = ld.Config.Context.Err() 1151 continue 1152 } 1153 wg.Add(1) 1154 go func(i int, filename string) { 1155 parsed[i], errors[i] = ld.parseFile(filename) 1156 wg.Done() 1157 }(i, file) 1158 } 1159 wg.Wait() 1160 1161 // Eliminate nils, preserving order. 1162 var o int 1163 for _, f := range parsed { 1164 if f != nil { 1165 parsed[o] = f 1166 o++ 1167 } 1168 } 1169 parsed = parsed[:o] 1170 1171 o = 0 1172 for _, err := range errors { 1173 if err != nil { 1174 errors[o] = err 1175 o++ 1176 } 1177 } 1178 errors = errors[:o] 1179 1180 return parsed, errors 1181 } 1182 1183 // sameFile returns true if x and y have the same basename and denote 1184 // the same file. 1185 func sameFile(x, y string) bool { 1186 if x == y { 1187 // It could be the case that y doesn't exist. 1188 // For instance, it may be an overlay file that 1189 // hasn't been written to disk. To handle that case 1190 // let x == y through. (We added the exact absolute path 1191 // string to the CompiledGoFiles list, so the unwritten 1192 // overlay case implies x==y.) 1193 return true 1194 } 1195 if strings.EqualFold(filepath.Base(x), filepath.Base(y)) { // (optimisation) 1196 if xi, err := os.Stat(x); err == nil { 1197 if yi, err := os.Stat(y); err == nil { 1198 return os.SameFile(xi, yi) 1199 } 1200 } 1201 } 1202 return false 1203 } 1204 1205 // loadFromExportData ensures that type information is present for the specified 1206 // package, loading it from an export data file on the first request. 1207 // On success it sets lpkg.Types to a new Package. 1208 func (ld *loader) loadFromExportData(lpkg *loaderPackage) error { 1209 if lpkg.PkgPath == "" { 1210 log.Fatalf("internal error: Package %s has no PkgPath", lpkg) 1211 } 1212 1213 // Because gcexportdata.Read has the potential to create or 1214 // modify the types.Package for each node in the transitive 1215 // closure of dependencies of lpkg, all exportdata operations 1216 // must be sequential. (Finer-grained locking would require 1217 // changes to the gcexportdata API.) 1218 // 1219 // The exportMu lock guards the lpkg.Types field and the 1220 // types.Package it points to, for each loaderPackage in the graph. 1221 // 1222 // Not all accesses to Package.Pkg need to be protected by exportMu: 1223 // graph ordering ensures that direct dependencies of source 1224 // packages are fully loaded before the importer reads their Pkg field. 1225 ld.exportMu.Lock() 1226 defer ld.exportMu.Unlock() 1227 1228 if tpkg := lpkg.Types; tpkg != nil && tpkg.Complete() { 1229 return nil // cache hit 1230 } 1231 1232 lpkg.IllTyped = true // fail safe 1233 1234 if lpkg.ExportFile == "" { 1235 // Errors while building export data will have been printed to stderr. 1236 return fmt.Errorf("no export data file") 1237 } 1238 f, err := os.Open(lpkg.ExportFile) 1239 if err != nil { 1240 return err 1241 } 1242 defer f.Close() 1243 1244 // Read gc export data. 1245 // 1246 // We don't currently support gccgo export data because all 1247 // underlying workspaces use the gc toolchain. (Even build 1248 // systems that support gccgo don't use it for workspace 1249 // queries.) 1250 r, err := gcexportdata.NewReader(f) 1251 if err != nil { 1252 return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err) 1253 } 1254 1255 // Build the view. 1256 // 1257 // The gcexportdata machinery has no concept of package ID. 1258 // It identifies packages by their PkgPath, which although not 1259 // globally unique is unique within the scope of one invocation 1260 // of the linker, type-checker, or gcexportdata. 1261 // 1262 // So, we must build a PkgPath-keyed view of the global 1263 // (conceptually ID-keyed) cache of packages and pass it to 1264 // gcexportdata. The view must contain every existing 1265 // package that might possibly be mentioned by the 1266 // current package---its transitive closure. 1267 // 1268 // In loadPackage, we unconditionally create a types.Package for 1269 // each dependency so that export data loading does not 1270 // create new ones. 1271 // 1272 // TODO(adonovan): it would be simpler and more efficient 1273 // if the export data machinery invoked a callback to 1274 // get-or-create a package instead of a map. 1275 // 1276 view := make(map[string]*types.Package) // view seen by gcexportdata 1277 seen := make(map[*loaderPackage]bool) // all visited packages 1278 var visit func(pkgs map[string]*Package) 1279 visit = func(pkgs map[string]*Package) { 1280 for _, p := range pkgs { 1281 lpkg := ld.pkgs[p.ID] 1282 if !seen[lpkg] { 1283 seen[lpkg] = true 1284 view[lpkg.PkgPath] = lpkg.Types 1285 visit(lpkg.Imports) 1286 } 1287 } 1288 } 1289 visit(lpkg.Imports) 1290 1291 viewLen := len(view) + 1 // adding the self package 1292 // Parse the export data. 1293 // (May modify incomplete packages in view but not create new ones.) 1294 tpkg, err := gcexportdata.Read(r, ld.Fset, view, lpkg.PkgPath) 1295 if err != nil { 1296 return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err) 1297 } 1298 if _, ok := view["go.shape"]; ok { 1299 // Account for the pseudopackage "go.shape" that gets 1300 // created by generic code. 1301 viewLen++ 1302 } 1303 if viewLen != len(view) { 1304 log.Panicf("golang.org/x/tools/go/packages: unexpected new packages during load of %s", lpkg.PkgPath) 1305 } 1306 1307 lpkg.Types = tpkg 1308 lpkg.IllTyped = false 1309 return nil 1310 } 1311 1312 // impliedLoadMode returns loadMode with its dependencies. 1313 func impliedLoadMode(loadMode LoadMode) LoadMode { 1314 if loadMode&(NeedDeps|NeedTypes|NeedTypesInfo) != 0 { 1315 // All these things require knowing the import graph. 1316 loadMode |= NeedImports 1317 } 1318 1319 return loadMode 1320 } 1321 1322 func usesExportData(cfg *Config) bool { 1323 return cfg.Mode&NeedExportFile != 0 || cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedDeps == 0 1324 } 1325 1326 var _ interface{} = io.Discard // assert build toolchain is go1.16 or later