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.
7 // See doc.go for package documentation and implementation notes.
26 "golang.org/x/tools/go/gcexportdata"
27 "golang.org/x/tools/internal/gocommand"
28 "golang.org/x/tools/internal/packagesinternal"
29 "golang.org/x/tools/internal/typesinternal"
32 // A LoadMode controls the amount of detail to return when loading.
33 // The bits below can be combined to specify which fields should be
34 // filled in the result packages.
35 // The zero value is a special case, equivalent to combining
36 // the NeedName, NeedFiles, and NeedCompiledGoFiles bits.
37 // ID and Errors (if present) will always be filled.
38 // Load may return more information than requested.
41 // TODO(matloob): When a V2 of go/packages is released, rename NeedExportsFile to
42 // NeedExportFile to make it consistent with the Package field it's adding.
45 // NeedName adds Name and PkgPath.
46 NeedName LoadMode = 1 << iota
48 // NeedFiles adds GoFiles and OtherFiles.
51 // NeedCompiledGoFiles adds CompiledGoFiles.
54 // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain
55 // "placeholder" Packages with only the ID set.
58 // NeedDeps adds the fields requested by the LoadMode in the packages in Imports.
61 // NeedExportsFile adds ExportFile.
64 // NeedTypes adds Types, Fset, and IllTyped.
67 // NeedSyntax adds Syntax.
70 // NeedTypesInfo adds TypesInfo.
73 // NeedTypesSizes adds TypesSizes.
76 // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+.
77 // Modifies CompiledGoFiles and Types, and has no effect on its own.
80 // NeedModule adds Module.
85 // Deprecated: LoadFiles exists for historical compatibility
86 // and should not be used. Please directly specify the needed fields using the Need values.
87 LoadFiles = NeedName | NeedFiles | NeedCompiledGoFiles
89 // Deprecated: LoadImports exists for historical compatibility
90 // and should not be used. Please directly specify the needed fields using the Need values.
91 LoadImports = LoadFiles | NeedImports
93 // Deprecated: LoadTypes exists for historical compatibility
94 // and should not be used. Please directly specify the needed fields using the Need values.
95 LoadTypes = LoadImports | NeedTypes | NeedTypesSizes
97 // Deprecated: LoadSyntax exists for historical compatibility
98 // and should not be used. Please directly specify the needed fields using the Need values.
99 LoadSyntax = LoadTypes | NeedSyntax | NeedTypesInfo
101 // Deprecated: LoadAllSyntax exists for historical compatibility
102 // and should not be used. Please directly specify the needed fields using the Need values.
103 LoadAllSyntax = LoadSyntax | NeedDeps
106 // A Config specifies details about how packages should be loaded.
107 // The zero value is a valid configuration.
108 // Calls to Load do not modify this struct.
110 // Mode controls the level of information returned for each package.
113 // Context specifies the context for the load operation.
114 // If the context is cancelled, the loader may stop early
115 // and return an ErrCancelled error.
116 // If Context is nil, the load cannot be cancelled.
117 Context context.Context
119 // Logf is the logger for the config.
120 // If the user provides a logger, debug logging is enabled.
121 // If the GOPACKAGESDEBUG environment variable is set to true,
122 // but the logger is nil, default to log.Printf.
123 Logf func(format string, args ...interface{})
125 // Dir is the directory in which to run the build system's query tool
126 // that provides information about the packages.
127 // If Dir is empty, the tool is run in the current directory.
130 // Env is the environment to use when invoking the build system's query tool.
131 // If Env is nil, the current environment is used.
132 // As in os/exec's Cmd, only the last value in the slice for
133 // each environment key is used. To specify the setting of only
134 // a few variables, append to the current environment, as in:
136 // opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386")
140 // gocmdRunner guards go command calls from concurrency errors.
141 gocmdRunner *gocommand.Runner
143 // BuildFlags is a list of command-line flags to be passed through to
144 // the build system's query tool.
147 // modFile will be used for -modfile in go command invocations.
150 // modFlag will be used for -modfile in go command invocations.
153 // Fset provides source position information for syntax trees and types.
154 // If Fset is nil, Load will use a new fileset, but preserve Fset's value.
157 // ParseFile is called to read and parse each file
158 // when preparing a package's type-checked syntax tree.
159 // It must be safe to call ParseFile simultaneously from multiple goroutines.
160 // If ParseFile is nil, the loader will uses parser.ParseFile.
162 // ParseFile should parse the source from src and use filename only for
163 // recording position information.
165 // An application may supply a custom implementation of ParseFile
166 // to change the effective file contents or the behavior of the parser,
167 // or to modify the syntax tree. For example, selectively eliminating
168 // unwanted function bodies can significantly accelerate type checking.
169 ParseFile func(fset *token.FileSet, filename string, src []byte) (*ast.File, error)
171 // If Tests is set, the loader includes not just the packages
172 // matching a particular pattern but also any related test packages,
173 // including test-only variants of the package and the test executable.
175 // For example, when using the go command, loading "fmt" with Tests=true
176 // returns four packages, with IDs "fmt" (the standard package),
177 // "fmt [fmt.test]" (the package as compiled for the test),
178 // "fmt_test" (the test functions from source files in package fmt_test),
179 // and "fmt.test" (the test binary).
181 // In build systems with explicit names for tests,
182 // setting Tests may have no effect.
185 // Overlay provides a mapping of absolute file paths to file contents.
186 // If the file with the given path already exists, the parser will use the
187 // alternative file contents provided by the map.
189 // Overlays provide incomplete support for when a given file doesn't
190 // already exist on disk. See the package doc above for more details.
191 Overlay map[string][]byte
194 // driver is the type for functions that query the build system for the
195 // packages named by the patterns.
196 type driver func(cfg *Config, patterns ...string) (*driverResponse, error)
198 // driverResponse contains the results for a driver query.
199 type driverResponse struct {
200 // NotHandled is returned if the request can't be handled by the current
201 // driver. If an external driver returns a response with NotHandled, the
202 // rest of the driverResponse is ignored, and go/packages will fallback
203 // to the next driver. If go/packages is extended in the future to support
204 // lists of multiple drivers, go/packages will fall back to the next driver.
207 // Sizes, if not nil, is the types.Sizes to use when type checking.
208 Sizes *types.StdSizes
210 // Roots is the set of package IDs that make up the root packages.
211 // We have to encode this separately because when we encode a single package
212 // we cannot know if it is one of the roots as that requires knowledge of the
213 // graph it is part of.
214 Roots []string `json:",omitempty"`
216 // Packages is the full set of packages in the graph.
217 // The packages are not connected into a graph.
218 // The Imports if populated will be stubs that only have their ID set.
219 // Imports will be connected and then type and syntax information added in a
220 // later pass (see refine).
224 // Load loads and returns the Go packages named by the given patterns.
226 // Config specifies loading options;
227 // nil behaves the same as an empty Config.
229 // Load returns an error if any of the patterns was invalid
230 // as defined by the underlying build system.
231 // It may return an empty list of packages without an error,
232 // for instance for an empty expansion of a valid wildcard.
233 // Errors associated with a particular package are recorded in the
234 // corresponding Package's Errors list, and do not cause Load to
235 // return an error. Clients may need to handle such errors before
236 // proceeding with further analysis. The PrintErrors function is
237 // provided for convenient display of all errors.
238 func Load(cfg *Config, patterns ...string) ([]*Package, error) {
240 response, err := defaultDriver(&l.Config, patterns...)
244 l.sizes = response.Sizes
245 return l.refine(response.Roots, response.Packages...)
248 // defaultDriver is a driver that implements go/packages' fallback behavior.
249 // It will try to request to an external driver, if one exists. If there's
250 // no external driver, or the driver returns a response with NotHandled set,
251 // defaultDriver will fall back to the go list driver.
252 func defaultDriver(cfg *Config, patterns ...string) (*driverResponse, error) {
253 driver := findExternalDriver(cfg)
255 driver = goListDriver
257 response, err := driver(cfg, patterns...)
260 } else if response.NotHandled {
261 return goListDriver(cfg, patterns...)
266 // A Package describes a loaded Go package.
267 type Package struct {
268 // ID is a unique identifier for a package,
269 // in a syntax provided by the underlying build system.
271 // Because the syntax varies based on the build system,
272 // clients should treat IDs as opaque and not attempt to
276 // Name is the package name as it appears in the package source code.
279 // PkgPath is the package path as used by the go/types package.
282 // Errors contains any errors encountered querying the metadata
283 // of the package, or while parsing or type-checking its files.
286 // GoFiles lists the absolute file paths of the package's Go source files.
289 // CompiledGoFiles lists the absolute file paths of the package's source
290 // files that are suitable for type checking.
291 // This may differ from GoFiles if files are processed before compilation.
292 CompiledGoFiles []string
294 // OtherFiles lists the absolute file paths of the package's non-Go source files,
295 // including assembly, C, C++, Fortran, Objective-C, SWIG, and so on.
298 // IgnoredFiles lists source files that are not part of the package
299 // using the current build configuration but that might be part of
300 // the package using other build configurations.
301 IgnoredFiles []string
303 // ExportFile is the absolute path to a file containing type
304 // information for the package as provided by the build system.
307 // Imports maps import paths appearing in the package's Go source files
308 // to corresponding loaded Packages.
309 Imports map[string]*Package
311 // Types provides type information for the package.
312 // The NeedTypes LoadMode bit sets this field for packages matching the
313 // patterns; type information for dependencies may be missing or incomplete,
314 // unless NeedDeps and NeedImports are also set.
317 // Fset provides position information for Types, TypesInfo, and Syntax.
318 // It is set only when Types is set.
321 // IllTyped indicates whether the package or any dependency contains errors.
322 // It is set only when Types is set.
325 // Syntax is the package's syntax trees, for the files listed in CompiledGoFiles.
327 // The NeedSyntax LoadMode bit populates this field for packages matching the patterns.
328 // If NeedDeps and NeedImports are also set, this field will also be populated
332 // TypesInfo provides type information about the package's syntax trees.
333 // It is set only when Syntax is set.
334 TypesInfo *types.Info
336 // TypesSizes provides the effective size function for types in TypesInfo.
337 TypesSizes types.Sizes
339 // forTest is the package under test, if any.
342 // module is the module information for the package if it exists.
346 // Module provides module information for a package.
348 Path string // module path
349 Version string // module version
350 Replace *Module // replaced by this module
351 Time *time.Time // time version was created
352 Main bool // is this the main module?
353 Indirect bool // is this module only an indirect dependency of main module?
354 Dir string // directory holding files for this module, if any
355 GoMod string // path to go.mod file used when loading this module, if any
356 GoVersion string // go version used in module
357 Error *ModuleError // error loading module
360 // ModuleError holds errors loading a module.
361 type ModuleError struct {
362 Err string // the error itself
366 packagesinternal.GetForTest = func(p interface{}) string {
367 return p.(*Package).forTest
369 packagesinternal.GetGoCmdRunner = func(config interface{}) *gocommand.Runner {
370 return config.(*Config).gocmdRunner
372 packagesinternal.SetGoCmdRunner = func(config interface{}, runner *gocommand.Runner) {
373 config.(*Config).gocmdRunner = runner
375 packagesinternal.SetModFile = func(config interface{}, value string) {
376 config.(*Config).modFile = value
378 packagesinternal.SetModFlag = func(config interface{}, value string) {
379 config.(*Config).modFlag = value
381 packagesinternal.TypecheckCgo = int(typecheckCgo)
384 // An Error describes a problem with a package's metadata, syntax, or types.
386 Pos string // "file:line:col" or "file:line" or "" or "-"
391 // ErrorKind describes the source of the error, allowing the user to
392 // differentiate between errors generated by the driver, the parser, or the
397 UnknownError ErrorKind = iota
403 func (err Error) Error() string {
406 pos = "-" // like token.Position{}.String()
408 return pos + ": " + err.Msg
411 // flatPackage is the JSON form of Package
412 // It drops all the type and syntax fields, and transforms the Imports
414 // TODO(adonovan): identify this struct with Package, effectively
415 // publishing the JSON protocol.
416 type flatPackage struct {
418 Name string `json:",omitempty"`
419 PkgPath string `json:",omitempty"`
420 Errors []Error `json:",omitempty"`
421 GoFiles []string `json:",omitempty"`
422 CompiledGoFiles []string `json:",omitempty"`
423 OtherFiles []string `json:",omitempty"`
424 IgnoredFiles []string `json:",omitempty"`
425 ExportFile string `json:",omitempty"`
426 Imports map[string]string `json:",omitempty"`
429 // MarshalJSON returns the Package in its JSON form.
430 // For the most part, the structure fields are written out unmodified, and
431 // the type and syntax fields are skipped.
432 // The imports are written out as just a map of path to package id.
433 // The errors are written using a custom type that tries to preserve the
434 // structure of error types we know about.
436 // This method exists to enable support for additional build systems. It is
437 // not intended for use by clients of the API and we may change the format.
438 func (p *Package) MarshalJSON() ([]byte, error) {
439 flat := &flatPackage{
445 CompiledGoFiles: p.CompiledGoFiles,
446 OtherFiles: p.OtherFiles,
447 IgnoredFiles: p.IgnoredFiles,
448 ExportFile: p.ExportFile,
450 if len(p.Imports) > 0 {
451 flat.Imports = make(map[string]string, len(p.Imports))
452 for path, ipkg := range p.Imports {
453 flat.Imports[path] = ipkg.ID
456 return json.Marshal(flat)
459 // UnmarshalJSON reads in a Package from its JSON format.
460 // See MarshalJSON for details about the format accepted.
461 func (p *Package) UnmarshalJSON(b []byte) error {
462 flat := &flatPackage{}
463 if err := json.Unmarshal(b, &flat); err != nil {
469 PkgPath: flat.PkgPath,
471 GoFiles: flat.GoFiles,
472 CompiledGoFiles: flat.CompiledGoFiles,
473 OtherFiles: flat.OtherFiles,
474 ExportFile: flat.ExportFile,
476 if len(flat.Imports) > 0 {
477 p.Imports = make(map[string]*Package, len(flat.Imports))
478 for path, id := range flat.Imports {
479 p.Imports[path] = &Package{ID: id}
485 func (p *Package) String() string { return p.ID }
487 // loaderPackage augments Package with state used during the loading phase
488 type loaderPackage struct {
490 importErrors map[string]error // maps each bad import to its error
492 color uint8 // for cycle detection
493 needsrc bool // load from source (Mode >= LoadTypes)
494 needtypes bool // type information is either requested or depended on
495 initial bool // package was matched by a pattern
498 // loader holds the working state of a single call to load.
500 pkgs map[string]*loaderPackage
503 parseCache map[string]*parseValue
504 parseCacheMu sync.Mutex
505 exportMu sync.Mutex // enforces mutual exclusion of exportdata operations
507 // Config.Mode contains the implied mode (see impliedLoadMode).
508 // Implied mode contains all the fields we need the data for.
509 // In requestedMode there are the actually requested fields.
510 // We'll zero them out before returning packages to the user.
511 // This makes it easier for us to get the conditions where
512 // we need certain modes right.
513 requestedMode LoadMode
516 type parseValue struct {
522 func newLoader(cfg *Config) *loader {
524 parseCache: map[string]*parseValue{},
528 // If the user has provided a logger, use it.
529 ld.Config.Logf = cfg.Logf
531 if ld.Config.Logf == nil {
532 // If the GOPACKAGESDEBUG environment variable is set to true,
533 // but the user has not provided a logger, default to log.Printf.
535 ld.Config.Logf = log.Printf
537 ld.Config.Logf = func(format string, args ...interface{}) {}
540 if ld.Config.Mode == 0 {
541 ld.Config.Mode = NeedName | NeedFiles | NeedCompiledGoFiles // Preserve zero behavior of Mode for backwards compatibility.
543 if ld.Config.Env == nil {
544 ld.Config.Env = os.Environ()
546 if ld.Config.gocmdRunner == nil {
547 ld.Config.gocmdRunner = &gocommand.Runner{}
549 if ld.Context == nil {
550 ld.Context = context.Background()
553 if dir, err := os.Getwd(); err == nil {
558 // Save the actually requested fields. We'll zero them out before returning packages to the user.
559 ld.requestedMode = ld.Mode
560 ld.Mode = impliedLoadMode(ld.Mode)
562 if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
564 ld.Fset = token.NewFileSet()
567 // ParseFile is required even in LoadTypes mode
568 // because we load source if export data is missing.
569 if ld.ParseFile == nil {
570 ld.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) {
571 const mode = parser.AllErrors | parser.ParseComments
572 return parser.ParseFile(fset, filename, src, mode)
580 // refine connects the supplied packages into a graph and then adds type and
581 // and syntax information as requested by the LoadMode.
582 func (ld *loader) refine(roots []string, list ...*Package) ([]*Package, error) {
583 rootMap := make(map[string]int, len(roots))
584 for i, root := range roots {
587 ld.pkgs = make(map[string]*loaderPackage)
588 // first pass, fixup and build the map and roots
589 var initial = make([]*loaderPackage, len(roots))
590 for _, pkg := range list {
592 if i, found := rootMap[pkg.ID]; found {
596 // Overlays can invalidate export data.
597 // TODO(matloob): make this check fine-grained based on dependencies on overlaid files
598 exportDataInvalid := len(ld.Overlay) > 0 || pkg.ExportFile == "" && pkg.PkgPath != "unsafe"
599 // This package needs type information if the caller requested types and the package is
600 // either a root, or it's a non-root and the user requested dependencies ...
601 needtypes := (ld.Mode&NeedTypes|NeedTypesInfo != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0))
602 // This package needs source if the call requested source (or types info, which implies source)
603 // and the package is either a root, or itas a non- root and the user requested dependencies...
604 needsrc := ((ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) ||
605 // ... or if we need types and the exportData is invalid. We fall back to (incompletely)
606 // typechecking packages from source if they fail to compile.
607 (ld.Mode&NeedTypes|NeedTypesInfo != 0 && exportDataInvalid)) && pkg.PkgPath != "unsafe"
608 lpkg := &loaderPackage{
610 needtypes: needtypes,
613 ld.pkgs[lpkg.ID] = lpkg
615 initial[rootIndex] = lpkg
619 for i, root := range roots {
620 if initial[i] == nil {
621 return nil, fmt.Errorf("root package %v is missing", root)
625 // Materialize the import graph.
629 grey = 1 // in progress
630 black = 2 // complete
633 // visit traverses the import graph, depth-first,
634 // and materializes the graph as Packages.Imports.
636 // Valid imports are saved in the Packages.Import map.
637 // Invalid imports (cycles and missing nodes) are saved in the importErrors map.
638 // Thus, even in the presence of both kinds of errors, the Import graph remains a DAG.
640 // visit returns whether the package needs src or has a transitive
641 // dependency on a package that does. These are the only packages
642 // for which we load source code.
643 var stack []*loaderPackage
644 var visit func(lpkg *loaderPackage) bool
645 var srcPkgs []*loaderPackage
646 visit = func(lpkg *loaderPackage) bool {
651 panic("internal error: grey node")
654 stack = append(stack, lpkg) // push
655 stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports
656 // If NeedImports isn't set, the imports fields will all be zeroed out.
657 if ld.Mode&NeedImports != 0 {
658 lpkg.Imports = make(map[string]*Package, len(stubs))
659 for importPath, ipkg := range stubs {
661 imp := ld.pkgs[ipkg.ID]
663 // (includes package "C" when DisableCgo)
664 importErr = fmt.Errorf("missing package: %q", ipkg.ID)
665 } else if imp.color == grey {
666 importErr = fmt.Errorf("import cycle: %s", stack)
668 if importErr != nil {
669 if lpkg.importErrors == nil {
670 lpkg.importErrors = make(map[string]error)
672 lpkg.importErrors[importPath] = importErr
679 lpkg.Imports[importPath] = imp.Package
683 srcPkgs = append(srcPkgs, lpkg)
685 if ld.Mode&NeedTypesSizes != 0 {
686 lpkg.TypesSizes = ld.sizes
688 stack = stack[:len(stack)-1] // pop
694 if ld.Mode&NeedImports == 0 {
695 // We do this to drop the stub import packages that we are not even going to try to resolve.
696 for _, lpkg := range initial {
700 // For each initial package, create its import DAG.
701 for _, lpkg := range initial {
705 if ld.Mode&NeedImports != 0 && ld.Mode&NeedTypes != 0 {
706 for _, lpkg := range srcPkgs {
707 // Complete type information is required for the
708 // immediate dependencies of each source package.
709 for _, ipkg := range lpkg.Imports {
710 imp := ld.pkgs[ipkg.ID]
715 // Load type data and syntax if needed, starting at
716 // the initial packages (roots of the import DAG).
717 if ld.Mode&NeedTypes != 0 || ld.Mode&NeedSyntax != 0 {
718 var wg sync.WaitGroup
719 for _, lpkg := range initial {
721 go func(lpkg *loaderPackage) {
722 ld.loadRecursive(lpkg)
729 result := make([]*Package, len(initial))
730 for i, lpkg := range initial {
731 result[i] = lpkg.Package
733 for i := range ld.pkgs {
734 // Clear all unrequested fields,
735 // to catch programs that use more than they request.
736 if ld.requestedMode&NeedName == 0 {
738 ld.pkgs[i].PkgPath = ""
740 if ld.requestedMode&NeedFiles == 0 {
741 ld.pkgs[i].GoFiles = nil
742 ld.pkgs[i].OtherFiles = nil
743 ld.pkgs[i].IgnoredFiles = nil
745 if ld.requestedMode&NeedCompiledGoFiles == 0 {
746 ld.pkgs[i].CompiledGoFiles = nil
748 if ld.requestedMode&NeedImports == 0 {
749 ld.pkgs[i].Imports = nil
751 if ld.requestedMode&NeedExportsFile == 0 {
752 ld.pkgs[i].ExportFile = ""
754 if ld.requestedMode&NeedTypes == 0 {
755 ld.pkgs[i].Types = nil
756 ld.pkgs[i].Fset = nil
757 ld.pkgs[i].IllTyped = false
759 if ld.requestedMode&NeedSyntax == 0 {
760 ld.pkgs[i].Syntax = nil
762 if ld.requestedMode&NeedTypesInfo == 0 {
763 ld.pkgs[i].TypesInfo = nil
765 if ld.requestedMode&NeedTypesSizes == 0 {
766 ld.pkgs[i].TypesSizes = nil
768 if ld.requestedMode&NeedModule == 0 {
769 ld.pkgs[i].Module = nil
776 // loadRecursive loads the specified package and its dependencies,
777 // recursively, in parallel, in topological order.
778 // It is atomic and idempotent.
779 // Precondition: ld.Mode&NeedTypes.
780 func (ld *loader) loadRecursive(lpkg *loaderPackage) {
781 lpkg.loadOnce.Do(func() {
782 // Load the direct dependencies, in parallel.
783 var wg sync.WaitGroup
784 for _, ipkg := range lpkg.Imports {
785 imp := ld.pkgs[ipkg.ID]
787 go func(imp *loaderPackage) {
788 ld.loadRecursive(imp)
797 // loadPackage loads the specified package.
798 // It must be called only once per Package,
799 // after immediate dependencies are loaded.
800 // Precondition: ld.Mode & NeedTypes.
801 func (ld *loader) loadPackage(lpkg *loaderPackage) {
802 if lpkg.PkgPath == "unsafe" {
803 // Fill in the blanks to avoid surprises.
804 lpkg.Types = types.Unsafe
806 lpkg.Syntax = []*ast.File{}
807 lpkg.TypesInfo = new(types.Info)
808 lpkg.TypesSizes = ld.sizes
812 // Call NewPackage directly with explicit name.
813 // This avoids skew between golist and go/types when the files'
814 // package declarations are inconsistent.
815 lpkg.Types = types.NewPackage(lpkg.PkgPath, lpkg.Name)
818 // Subtle: we populate all Types fields with an empty Package
819 // before loading export data so that export data processing
820 // never has to create a types.Package for an indirect dependency,
821 // which would then require that such created packages be explicitly
822 // inserted back into the Import graph as a final step after export data loading.
823 // The Diamond test exercises this case.
824 if !lpkg.needtypes && !lpkg.needsrc {
828 ld.loadFromExportData(lpkg)
829 return // not a source package, don't get syntax trees
832 appendError := func(err error) {
833 // Convert various error types into the one true Error.
835 switch err := err.(type) {
838 errs = append(errs, err)
842 errs = append(errs, Error{
843 Pos: err.Path + ":1",
844 Msg: err.Err.Error(),
848 case scanner.ErrorList:
850 for _, err := range err {
851 errs = append(errs, Error{
852 Pos: err.Pos.String(),
860 errs = append(errs, Error{
861 Pos: err.Fset.Position(err.Pos).String(),
867 // unexpected impoverished error from parser?
868 errs = append(errs, Error{
874 // If you see this error message, please file a bug.
875 log.Printf("internal error: error %q (%T) without position", err, err)
878 lpkg.Errors = append(lpkg.Errors, errs...)
881 if ld.Config.Mode&NeedTypes != 0 && len(lpkg.CompiledGoFiles) == 0 && lpkg.ExportFile != "" {
882 // The config requested loading sources and types, but sources are missing.
883 // Add an error to the package and fall back to loading from export data.
884 appendError(Error{"-", fmt.Sprintf("sources missing for package %s", lpkg.ID), ParseError})
885 ld.loadFromExportData(lpkg)
886 return // can't get syntax trees for this package
889 files, errs := ld.parseFiles(lpkg.CompiledGoFiles)
890 for _, err := range errs {
895 if ld.Config.Mode&NeedTypes == 0 {
899 lpkg.TypesInfo = &types.Info{
900 Types: make(map[ast.Expr]types.TypeAndValue),
901 Defs: make(map[*ast.Ident]types.Object),
902 Uses: make(map[*ast.Ident]types.Object),
903 Implicits: make(map[ast.Node]types.Object),
904 Scopes: make(map[ast.Node]*types.Scope),
905 Selections: make(map[*ast.SelectorExpr]*types.Selection),
907 lpkg.TypesSizes = ld.sizes
909 importer := importerFunc(func(path string) (*types.Package, error) {
910 if path == "unsafe" {
911 return types.Unsafe, nil
914 // The imports map is keyed by import path.
915 ipkg := lpkg.Imports[path]
917 if err := lpkg.importErrors[path]; err != nil {
920 // There was skew between the metadata and the
921 // import declarations, likely due to an edit
922 // race, or because the ParseFile feature was
923 // used to supply alternative file contents.
924 return nil, fmt.Errorf("no metadata for %s", path)
927 if ipkg.Types != nil && ipkg.Types.Complete() {
928 return ipkg.Types, nil
930 log.Fatalf("internal error: package %q without types was imported from %q", path, lpkg)
938 // Type-check bodies of functions only in non-initial packages.
939 // Example: for import graph A->B->C and initial packages {A,C},
940 // we can ignore function bodies in B.
941 IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial,
946 if (ld.Mode & typecheckCgo) != 0 {
947 if !typesinternal.SetUsesCgo(tc) {
949 Msg: "typecheckCgo requires Go 1.15+",
955 types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax)
957 lpkg.importErrors = nil // no longer needed
959 // If !Cgo, the type-checker uses FakeImportC mode, so
960 // it doesn't invoke the importer for import "C",
961 // nor report an error for the import,
962 // or for any undefined C.f reference.
963 // We must detect this explicitly and correctly
964 // mark the package as IllTyped (by reporting an error).
965 // TODO(adonovan): if these errors are annoying,
966 // we could just set IllTyped quietly.
969 for _, f := range lpkg.Syntax {
970 for _, imp := range f.Imports {
971 if imp.Path.Value == `"C"` {
972 err := types.Error{Fset: ld.Fset, Pos: imp.Pos(), Msg: `import "C" ignored`}
980 // Record accumulated errors.
981 illTyped := len(lpkg.Errors) > 0
983 for _, imp := range lpkg.Imports {
990 lpkg.IllTyped = illTyped
993 // An importFunc is an implementation of the single-method
994 // types.Importer interface based on a function value.
995 type importerFunc func(path string) (*types.Package, error)
997 func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) }
999 // We use a counting semaphore to limit
1000 // the number of parallel I/O calls per process.
1001 var ioLimit = make(chan bool, 20)
1003 func (ld *loader) parseFile(filename string) (*ast.File, error) {
1004 ld.parseCacheMu.Lock()
1005 v, ok := ld.parseCache[filename]
1008 ld.parseCacheMu.Unlock()
1012 v = &parseValue{ready: make(chan struct{})}
1013 ld.parseCache[filename] = v
1014 ld.parseCacheMu.Unlock()
1017 for f, contents := range ld.Config.Overlay {
1018 if sameFile(f, filename) {
1024 ioLimit <- true // wait
1025 src, err = ioutil.ReadFile(filename)
1031 v.f, v.err = ld.ParseFile(ld.Fset, filename, src)
1039 // parseFiles reads and parses the Go source files and returns the ASTs
1040 // of the ones that could be at least partially parsed, along with a
1041 // list of I/O and parse errors encountered.
1043 // Because files are scanned in parallel, the token.Pos
1044 // positions of the resulting ast.Files are not ordered.
1046 func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) {
1047 var wg sync.WaitGroup
1049 parsed := make([]*ast.File, n)
1050 errors := make([]error, n)
1051 for i, file := range filenames {
1052 if ld.Config.Context.Err() != nil {
1054 errors[i] = ld.Config.Context.Err()
1058 go func(i int, filename string) {
1059 parsed[i], errors[i] = ld.parseFile(filename)
1065 // Eliminate nils, preserving order.
1067 for _, f := range parsed {
1076 for _, err := range errors {
1084 return parsed, errors
1087 // sameFile returns true if x and y have the same basename and denote
1090 func sameFile(x, y string) bool {
1092 // It could be the case that y doesn't exist.
1093 // For instance, it may be an overlay file that
1094 // hasn't been written to disk. To handle that case
1095 // let x == y through. (We added the exact absolute path
1096 // string to the CompiledGoFiles list, so the unwritten
1097 // overlay case implies x==y.)
1100 if strings.EqualFold(filepath.Base(x), filepath.Base(y)) { // (optimisation)
1101 if xi, err := os.Stat(x); err == nil {
1102 if yi, err := os.Stat(y); err == nil {
1103 return os.SameFile(xi, yi)
1110 // loadFromExportData returns type information for the specified
1111 // package, loading it from an export data file on the first request.
1112 func (ld *loader) loadFromExportData(lpkg *loaderPackage) (*types.Package, error) {
1113 if lpkg.PkgPath == "" {
1114 log.Fatalf("internal error: Package %s has no PkgPath", lpkg)
1117 // Because gcexportdata.Read has the potential to create or
1118 // modify the types.Package for each node in the transitive
1119 // closure of dependencies of lpkg, all exportdata operations
1120 // must be sequential. (Finer-grained locking would require
1121 // changes to the gcexportdata API.)
1123 // The exportMu lock guards the Package.Pkg field and the
1124 // types.Package it points to, for each Package in the graph.
1126 // Not all accesses to Package.Pkg need to be protected by exportMu:
1127 // graph ordering ensures that direct dependencies of source
1128 // packages are fully loaded before the importer reads their Pkg field.
1130 defer ld.exportMu.Unlock()
1132 if tpkg := lpkg.Types; tpkg != nil && tpkg.Complete() {
1133 return tpkg, nil // cache hit
1136 lpkg.IllTyped = true // fail safe
1138 if lpkg.ExportFile == "" {
1139 // Errors while building export data will have been printed to stderr.
1140 return nil, fmt.Errorf("no export data file")
1142 f, err := os.Open(lpkg.ExportFile)
1148 // Read gc export data.
1150 // We don't currently support gccgo export data because all
1151 // underlying workspaces use the gc toolchain. (Even build
1152 // systems that support gccgo don't use it for workspace
1154 r, err := gcexportdata.NewReader(f)
1156 return nil, fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
1161 // The gcexportdata machinery has no concept of package ID.
1162 // It identifies packages by their PkgPath, which although not
1163 // globally unique is unique within the scope of one invocation
1164 // of the linker, type-checker, or gcexportdata.
1166 // So, we must build a PkgPath-keyed view of the global
1167 // (conceptually ID-keyed) cache of packages and pass it to
1168 // gcexportdata. The view must contain every existing
1169 // package that might possibly be mentioned by the
1170 // current package---its transitive closure.
1172 // In loadPackage, we unconditionally create a types.Package for
1173 // each dependency so that export data loading does not
1176 // TODO(adonovan): it would be simpler and more efficient
1177 // if the export data machinery invoked a callback to
1178 // get-or-create a package instead of a map.
1180 view := make(map[string]*types.Package) // view seen by gcexportdata
1181 seen := make(map[*loaderPackage]bool) // all visited packages
1182 var visit func(pkgs map[string]*Package)
1183 visit = func(pkgs map[string]*Package) {
1184 for _, p := range pkgs {
1185 lpkg := ld.pkgs[p.ID]
1188 view[lpkg.PkgPath] = lpkg.Types
1195 viewLen := len(view) + 1 // adding the self package
1196 // Parse the export data.
1197 // (May modify incomplete packages in view but not create new ones.)
1198 tpkg, err := gcexportdata.Read(r, ld.Fset, view, lpkg.PkgPath)
1200 return nil, fmt.Errorf("reading %s: %v", lpkg.ExportFile, err)
1202 if viewLen != len(view) {
1203 log.Fatalf("Unexpected package creation during export data loading")
1207 lpkg.IllTyped = false
1212 // impliedLoadMode returns loadMode with its dependencies.
1213 func impliedLoadMode(loadMode LoadMode) LoadMode {
1214 if loadMode&NeedTypesInfo != 0 && loadMode&NeedImports == 0 {
1215 // If NeedTypesInfo, go/packages needs to do typechecking itself so it can
1216 // associate type info with the AST. To do so, we need the export data
1217 // for dependencies, which means we need to ask for the direct dependencies.
1218 // NeedImports is used to ask for the direct dependencies.
1219 loadMode |= NeedImports
1222 if loadMode&NeedDeps != 0 && loadMode&NeedImports == 0 {
1223 // With NeedDeps we need to load at least direct dependencies.
1224 // NeedImports is used to ask for the direct dependencies.
1225 loadMode |= NeedImports
1231 func usesExportData(cfg *Config) bool {
1232 return cfg.Mode&NeedExportsFile != 0 || cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedDeps == 0