packages.go 42 KB

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