| 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 | "errors" |
| 13 | "fmt" |
| 14 | "go/ast" |
| 15 | "go/parser" |
| 16 | "go/scanner" |
| 17 | "go/token" |
| 18 | "go/types" |
| 19 | "log" |
| 20 | "os" |
| 21 | "path/filepath" |
| 22 | "runtime" |
| 23 | "strings" |
| 24 | "sync" |
| 25 | "sync/atomic" |
| 26 | "time" |
| 27 | |
| 28 | "golang.org/x/sync/errgroup" |
| 29 | |
| 30 | "golang.org/x/tools/go/gcexportdata" |
| 31 | "golang.org/x/tools/internal/gocommand" |
| 32 | "golang.org/x/tools/internal/packagesinternal" |
| 33 | "golang.org/x/tools/internal/typesinternal" |
| 34 | ) |
| 35 | |
| 36 | // A LoadMode controls the amount of detail to return when loading. |
| 37 | // The bits below can be combined to specify which fields should be |
| 38 | // filled in the result packages. |
| 39 | // |
| 40 | // The zero value is a special case, equivalent to combining |
| 41 | // the NeedName, NeedFiles, and NeedCompiledGoFiles bits. |
| 42 | // |
| 43 | // ID and Errors (if present) will always be filled. |
| 44 | // [Load] may return more information than requested. |
| 45 | // |
| 46 | // The Mode flag is a union of several bits named NeedName, |
| 47 | // NeedFiles, and so on, each of which determines whether |
| 48 | // a given field of Package (Name, Files, etc) should be |
| 49 | // populated. |
| 50 | // |
| 51 | // For convenience, we provide named constants for the most |
| 52 | // common combinations of Need flags: |
| 53 | // |
| 54 | // [LoadFiles] lists of files in each package |
| 55 | // [LoadImports] ... plus imports |
| 56 | // [LoadTypes] ... plus type information |
| 57 | // [LoadSyntax] ... plus type-annotated syntax |
| 58 | // [LoadAllSyntax] ... for all dependencies |
| 59 | // |
| 60 | // Unfortunately there are a number of open bugs related to |
| 61 | // interactions among the LoadMode bits: |
| 62 | // - https://go.dev/issue/56633 |
| 63 | // - https://go.dev/issue/56677 |
| 64 | // - https://go.dev/issue/58726 |
| 65 | // - https://go.dev/issue/63517 |
| 66 | type LoadMode int |
| 67 | |
| 68 | const ( |
| 69 | // NeedName adds Name and PkgPath. |
| 70 | NeedName LoadMode = 1 << iota |
| 71 | |
| 72 | // NeedFiles adds Dir, GoFiles, OtherFiles, and IgnoredFiles |
| 73 | NeedFiles |
| 74 | |
| 75 | // NeedCompiledGoFiles adds CompiledGoFiles. |
| 76 | NeedCompiledGoFiles |
| 77 | |
| 78 | // NeedImports adds Imports. If NeedDeps is not set, the Imports field will contain |
| 79 | // "placeholder" Packages with only the ID set. |
| 80 | NeedImports |
| 81 | |
| 82 | // NeedDeps adds the fields requested by the LoadMode in the packages in Imports. |
| 83 | NeedDeps |
| 84 | |
| 85 | // NeedExportFile adds ExportFile. |
| 86 | NeedExportFile |
| 87 | |
| 88 | // NeedTypes adds Types, Fset, and IllTyped. |
| 89 | NeedTypes |
| 90 | |
| 91 | // NeedSyntax adds Syntax and Fset. |
| 92 | NeedSyntax |
| 93 | |
| 94 | // NeedTypesInfo adds TypesInfo and Fset. |
| 95 | NeedTypesInfo |
| 96 | |
| 97 | // NeedTypesSizes adds TypesSizes. |
| 98 | NeedTypesSizes |
| 99 | |
| 100 | // needInternalDepsErrors adds the internal deps errors field for use by gopls. |
| 101 | needInternalDepsErrors |
| 102 | |
| 103 | // NeedForTest adds ForTest. |
| 104 | // |
| 105 | // Tests must also be set on the context for this field to be populated. |
| 106 | NeedForTest |
| 107 | |
| 108 | // typecheckCgo enables full support for type checking cgo. Requires Go 1.15+. |
| 109 | // Modifies CompiledGoFiles and Types, and has no effect on its own. |
| 110 | typecheckCgo |
| 111 | |
| 112 | // NeedModule adds Module. |
| 113 | NeedModule |
| 114 | |
| 115 | // NeedEmbedFiles adds EmbedFiles. |
| 116 | NeedEmbedFiles |
| 117 | |
| 118 | // NeedEmbedPatterns adds EmbedPatterns. |
| 119 | NeedEmbedPatterns |
| 120 | |
| 121 | // NeedTarget adds Target. |
| 122 | NeedTarget |
| 123 | |
| 124 | // Be sure to update loadmode_string.go when adding new items! |
| 125 | ) |
| 126 | |
| 127 | const ( |
| 128 | // LoadFiles loads the name and file names for the initial packages. |
| 129 | LoadFiles = NeedName | NeedFiles | NeedCompiledGoFiles |
| 130 | |
| 131 | // LoadImports loads the name, file names, and import mapping for the initial packages. |
| 132 | LoadImports = LoadFiles | NeedImports |
| 133 | |
| 134 | // LoadTypes loads exported type information for the initial packages. |
| 135 | LoadTypes = LoadImports | NeedTypes | NeedTypesSizes |
| 136 | |
| 137 | // LoadSyntax loads typed syntax for the initial packages. |
| 138 | LoadSyntax = LoadTypes | NeedSyntax | NeedTypesInfo |
| 139 | |
| 140 | // LoadAllSyntax loads typed syntax for the initial packages and all dependencies. |
| 141 | LoadAllSyntax = LoadSyntax | NeedDeps |
| 142 | |
| 143 | // Deprecated: NeedExportsFile is a historical misspelling of NeedExportFile. |
| 144 | // |
| 145 | //go:fix inline |
| 146 | NeedExportsFile = NeedExportFile |
| 147 | ) |
| 148 | |
| 149 | // A Config specifies details about how packages should be loaded. |
| 150 | // The zero value is a valid configuration. |
| 151 | // |
| 152 | // Calls to [Load] do not modify this struct. |
| 153 | type Config struct { |
| 154 | // Mode controls the level of information returned for each package. |
| 155 | Mode LoadMode |
| 156 | |
| 157 | // Context specifies the context for the load operation. |
| 158 | // Cancelling the context may cause [Load] to abort and |
| 159 | // return an error. |
| 160 | Context context.Context |
| 161 | |
| 162 | // Logf is the logger for the config. |
| 163 | // If the user provides a logger, debug logging is enabled. |
| 164 | // If the GOPACKAGESDEBUG environment variable is set to true, |
| 165 | // but the logger is nil, default to log.Printf. |
| 166 | Logf func(format string, args ...any) |
| 167 | |
| 168 | // Dir is the directory in which to run the build system's query tool |
| 169 | // that provides information about the packages. |
| 170 | // If Dir is empty, the tool is run in the current directory. |
| 171 | Dir string |
| 172 | |
| 173 | // Env is the environment to use when invoking the build system's query tool. |
| 174 | // If Env is nil, the current environment is used. |
| 175 | // As in os/exec's Cmd, only the last value in the slice for |
| 176 | // each environment key is used. To specify the setting of only |
| 177 | // a few variables, append to the current environment, as in: |
| 178 | // |
| 179 | // opt.Env = append(os.Environ(), "GOOS=plan9", "GOARCH=386") |
| 180 | // |
| 181 | Env []string |
| 182 | |
| 183 | // BuildFlags is a list of command-line flags to be passed through to |
| 184 | // the build system's query tool. |
| 185 | BuildFlags []string |
| 186 | |
| 187 | // Fset provides source position information for syntax trees and types. |
| 188 | // If Fset is nil, Load will use a new fileset, but preserve Fset's value. |
| 189 | Fset *token.FileSet |
| 190 | |
| 191 | // ParseFile is called to read and parse each file |
| 192 | // when preparing a package's type-checked syntax tree. |
| 193 | // It must be safe to call ParseFile simultaneously from multiple goroutines. |
| 194 | // If ParseFile is nil, the loader will uses parser.ParseFile. |
| 195 | // |
| 196 | // ParseFile should parse the source from src and use filename only for |
| 197 | // recording position information. |
| 198 | // |
| 199 | // An application may supply a custom implementation of ParseFile |
| 200 | // to change the effective file contents or the behavior of the parser, |
| 201 | // or to modify the syntax tree. For example, selectively eliminating |
| 202 | // unwanted function bodies can significantly accelerate type checking. |
| 203 | ParseFile func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) |
| 204 | |
| 205 | // If Tests is set, the loader includes not just the packages |
| 206 | // matching a particular pattern but also any related test packages, |
| 207 | // including test-only variants of the package and the test executable. |
| 208 | // |
| 209 | // For example, when using the go command, loading "fmt" with Tests=true |
| 210 | // returns four packages, with IDs "fmt" (the standard package), |
| 211 | // "fmt [fmt.test]" (the package as compiled for the test), |
| 212 | // "fmt_test" (the test functions from source files in package fmt_test), |
| 213 | // and "fmt.test" (the test binary). |
| 214 | // |
| 215 | // In build systems with explicit names for tests, |
| 216 | // setting Tests may have no effect. |
| 217 | Tests bool |
| 218 | |
| 219 | // Overlay is a mapping from absolute file paths to file contents. |
| 220 | // |
| 221 | // For each map entry, [Load] uses the alternative file |
| 222 | // contents provided by the overlay mapping instead of reading |
| 223 | // from the file system. This mechanism can be used to enable |
| 224 | // editor-integrated tools to correctly analyze the contents |
| 225 | // of modified but unsaved buffers, for example. |
| 226 | // |
| 227 | // The overlay mapping is passed to the build system's driver |
| 228 | // (see "The driver protocol") so that it too can report |
| 229 | // consistent package metadata about unsaved files. However, |
| 230 | // drivers may vary in their level of support for overlays. |
| 231 | Overlay map[string][]byte |
| 232 | } |
| 233 | |
| 234 | // Load loads and returns the Go packages named by the given patterns. |
| 235 | // |
| 236 | // The cfg parameter specifies loading options; nil behaves the same as an empty [Config]. |
| 237 | // |
| 238 | // The [Config.Mode] field is a set of bits that determine what kinds |
| 239 | // of information should be computed and returned. Modes that require |
| 240 | // more information tend to be slower. See [LoadMode] for details |
| 241 | // and important caveats. Its zero value is equivalent to |
| 242 | // [NeedName] | [NeedFiles] | [NeedCompiledGoFiles]. |
| 243 | // |
| 244 | // Each call to Load returns a new set of [Package] instances. |
| 245 | // The Packages and their Imports form a directed acyclic graph. |
| 246 | // |
| 247 | // If the [NeedTypes] mode flag was set, each call to Load uses a new |
| 248 | // [types.Importer], so [types.Object] and [types.Type] values from |
| 249 | // different calls to Load must not be mixed as they will have |
| 250 | // inconsistent notions of type identity. |
| 251 | // |
| 252 | // If any of the patterns was invalid as defined by the |
| 253 | // underlying build system, Load returns an error. |
| 254 | // It may return an empty list of packages without an error, |
| 255 | // for instance for an empty expansion of a valid wildcard. |
| 256 | // Errors associated with a particular package are recorded in the |
| 257 | // corresponding Package's Errors list, and do not cause Load to |
| 258 | // return an error. Clients may need to handle such errors before |
| 259 | // proceeding with further analysis. The [PrintErrors] function is |
| 260 | // provided for convenient display of all errors. |
| 261 | func Load(cfg *Config, patterns ...string) ([]*Package, error) { |
| 262 | ld := newLoader(cfg) |
| 263 | response, external, err := defaultDriver(&ld.Config, patterns...) |
| 264 | if err != nil { |
| 265 | return nil, err |
| 266 | } |
| 267 | |
| 268 | ld.sizes = types.SizesFor(response.Compiler, response.Arch) |
| 269 | if ld.sizes == nil && ld.Config.Mode&(NeedTypes|NeedTypesSizes|NeedTypesInfo) != 0 { |
| 270 | // Type size information is needed but unavailable. |
| 271 | if external { |
| 272 | // An external driver may fail to populate the Compiler/GOARCH fields, |
| 273 | // especially since they are relatively new (see #63700). |
| 274 | // Provide a sensible fallback in this case. |
| 275 | ld.sizes = types.SizesFor("gc", runtime.GOARCH) |
| 276 | if ld.sizes == nil { // gccgo-only arch |
| 277 | ld.sizes = types.SizesFor("gc", "amd64") |
| 278 | } |
| 279 | } else { |
| 280 | // Go list should never fail to deliver accurate size information. |
| 281 | // Reject the whole Load since the error is the same for every package. |
| 282 | return nil, fmt.Errorf("can't determine type sizes for compiler %q on GOARCH %q", |
| 283 | response.Compiler, response.Arch) |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | ld.externalDriver = external |
| 288 | |
| 289 | return ld.refine(response) |
| 290 | } |
| 291 | |
| 292 | // defaultDriver is a driver that implements go/packages' fallback behavior. |
| 293 | // It will try to request to an external driver, if one exists. If there's |
| 294 | // no external driver, or the driver returns a response with NotHandled set, |
| 295 | // defaultDriver will fall back to the go list driver. |
| 296 | // The boolean result indicates that an external driver handled the request. |
| 297 | func defaultDriver(cfg *Config, patterns ...string) (*DriverResponse, bool, error) { |
| 298 | const ( |
| 299 | // windowsArgMax specifies the maximum command line length for |
| 300 | // the Windows' CreateProcess function. |
| 301 | windowsArgMax = 32767 |
| 302 | // maxEnvSize is a very rough estimation of the maximum environment |
| 303 | // size of a user. |
| 304 | maxEnvSize = 16384 |
| 305 | // safeArgMax specifies the maximum safe command line length to use |
| 306 | // by the underlying driver excl. the environment. We choose the Windows' |
| 307 | // ARG_MAX as the starting point because it's one of the lowest ARG_MAX |
| 308 | // constants out of the different supported platforms, |
| 309 | // e.g., https://www.in-ulm.de/~mascheck/various/argmax/#results. |
| 310 | safeArgMax = windowsArgMax - maxEnvSize |
| 311 | ) |
| 312 | chunks, err := splitIntoChunks(patterns, safeArgMax) |
| 313 | if err != nil { |
| 314 | return nil, false, err |
| 315 | } |
| 316 | |
| 317 | if driver := findExternalDriver(cfg); driver != nil { |
| 318 | response, err := callDriverOnChunks(driver, cfg, chunks) |
| 319 | if err != nil { |
| 320 | return nil, false, err |
| 321 | } else if !response.NotHandled { |
| 322 | return response, true, nil |
| 323 | } |
| 324 | // not handled: fall through |
| 325 | } |
| 326 | |
| 327 | // go list fallback |
| 328 | |
| 329 | // Write overlays once, as there are many calls |
| 330 | // to 'go list' (one per chunk plus others too). |
| 331 | overlayFile, cleanupOverlay, err := gocommand.WriteOverlays(cfg.Overlay) |
| 332 | if err != nil { |
| 333 | return nil, false, err |
| 334 | } |
| 335 | defer cleanupOverlay() |
| 336 | |
| 337 | var runner gocommand.Runner // (shared across many 'go list' calls) |
| 338 | driver := func(cfg *Config, patterns []string) (*DriverResponse, error) { |
| 339 | return goListDriver(cfg, &runner, overlayFile, patterns) |
| 340 | } |
| 341 | response, err := callDriverOnChunks(driver, cfg, chunks) |
| 342 | if err != nil { |
| 343 | return nil, false, err |
| 344 | } |
| 345 | return response, false, err |
| 346 | } |
| 347 | |
| 348 | // splitIntoChunks chunks the slice so that the total number of characters |
| 349 | // in a chunk is no longer than argMax. |
| 350 | func splitIntoChunks(patterns []string, argMax int) ([][]string, error) { |
| 351 | if argMax <= 0 { |
| 352 | return nil, errors.New("failed to split patterns into chunks, negative safe argMax value") |
| 353 | } |
| 354 | var chunks [][]string |
| 355 | charsInChunk := 0 |
| 356 | nextChunkStart := 0 |
| 357 | for i, v := range patterns { |
| 358 | vChars := len(v) |
| 359 | if vChars > argMax { |
| 360 | // a single pattern is longer than the maximum safe ARG_MAX, hardly should happen |
| 361 | return nil, errors.New("failed to split patterns into chunks, a pattern is too long") |
| 362 | } |
| 363 | charsInChunk += vChars + 1 // +1 is for a whitespace between patterns that has to be counted too |
| 364 | if charsInChunk > argMax { |
| 365 | chunks = append(chunks, patterns[nextChunkStart:i]) |
| 366 | nextChunkStart = i |
| 367 | charsInChunk = vChars |
| 368 | } |
| 369 | } |
| 370 | // add the last chunk |
| 371 | if nextChunkStart < len(patterns) { |
| 372 | chunks = append(chunks, patterns[nextChunkStart:]) |
| 373 | } |
| 374 | return chunks, nil |
| 375 | } |
| 376 | |
| 377 | func callDriverOnChunks(driver driver, cfg *Config, chunks [][]string) (*DriverResponse, error) { |
| 378 | if len(chunks) == 0 { |
| 379 | return driver(cfg, nil) |
| 380 | } |
| 381 | responses := make([]*DriverResponse, len(chunks)) |
| 382 | errNotHandled := errors.New("driver returned NotHandled") |
| 383 | var g errgroup.Group |
| 384 | for i, chunk := range chunks { |
| 385 | g.Go(func() (err error) { |
| 386 | responses[i], err = driver(cfg, chunk) |
| 387 | if responses[i] != nil && responses[i].NotHandled { |
| 388 | err = errNotHandled |
| 389 | } |
| 390 | return err |
| 391 | }) |
| 392 | } |
| 393 | if err := g.Wait(); err != nil { |
| 394 | if errors.Is(err, errNotHandled) { |
| 395 | return &DriverResponse{NotHandled: true}, nil |
| 396 | } |
| 397 | return nil, err |
| 398 | } |
| 399 | return mergeResponses(responses...), nil |
| 400 | } |
| 401 | |
| 402 | func mergeResponses(responses ...*DriverResponse) *DriverResponse { |
| 403 | if len(responses) == 0 { |
| 404 | return nil |
| 405 | } |
| 406 | // No dedup needed |
| 407 | if len(responses) == 1 { |
| 408 | return responses[0] |
| 409 | } |
| 410 | response := newDeduper() |
| 411 | response.dr.NotHandled = false |
| 412 | response.dr.Compiler = responses[0].Compiler |
| 413 | response.dr.Arch = responses[0].Arch |
| 414 | response.dr.GoVersion = responses[0].GoVersion |
| 415 | for _, v := range responses { |
| 416 | response.addAll(v) |
| 417 | } |
| 418 | return response.dr |
| 419 | } |
| 420 | |
| 421 | // A Package describes a loaded Go package. |
| 422 | // |
| 423 | // It also defines part of the JSON schema of [DriverResponse]. |
| 424 | // See the package documentation for an overview. |
| 425 | type Package struct { |
| 426 | // ID is a unique identifier for a package, |
| 427 | // in a syntax provided by the underlying build system. |
| 428 | // |
| 429 | // Because the syntax varies based on the build system, |
| 430 | // clients should treat IDs as opaque and not attempt to |
| 431 | // interpret them. |
| 432 | ID string |
| 433 | |
| 434 | // Name is the package name as it appears in the package source code. |
| 435 | Name string |
| 436 | |
| 437 | // PkgPath is the package path as used by the go/types package. |
| 438 | PkgPath string |
| 439 | |
| 440 | // Dir is the directory associated with the package, if it exists. |
| 441 | // |
| 442 | // For packages listed by the go command, this is the directory containing |
| 443 | // the package files. |
| 444 | Dir string |
| 445 | |
| 446 | // Errors contains any errors encountered querying the metadata |
| 447 | // of the package, or while parsing or type-checking its files. |
| 448 | Errors []Error |
| 449 | |
| 450 | // TypeErrors contains the subset of errors produced during type checking. |
| 451 | TypeErrors []types.Error |
| 452 | |
| 453 | // GoFiles lists the absolute file paths of the package's Go source files. |
| 454 | // It may include files that should not be compiled, for example because |
| 455 | // they contain non-matching build tags, are documentary pseudo-files such as |
| 456 | // unsafe/unsafe.go or builtin/builtin.go, or are subject to cgo preprocessing. |
| 457 | GoFiles []string |
| 458 | |
| 459 | // CompiledGoFiles lists the absolute file paths of the package's source |
| 460 | // files that are suitable for type checking. |
| 461 | // This may differ from GoFiles if files are processed before compilation. |
| 462 | CompiledGoFiles []string |
| 463 | |
| 464 | // OtherFiles lists the absolute file paths of the package's non-Go source files, |
| 465 | // including assembly, C, C++, Fortran, Objective-C, SWIG, and so on. |
| 466 | OtherFiles []string |
| 467 | |
| 468 | // EmbedFiles lists the absolute file paths of the package's files |
| 469 | // embedded with go:embed. |
| 470 | EmbedFiles []string |
| 471 | |
| 472 | // EmbedPatterns lists the absolute file patterns of the package's |
| 473 | // files embedded with go:embed. |
| 474 | EmbedPatterns []string |
| 475 | |
| 476 | // IgnoredFiles lists source files that are not part of the package |
| 477 | // using the current build configuration but that might be part of |
| 478 | // the package using other build configurations. |
| 479 | IgnoredFiles []string |
| 480 | |
| 481 | // ExportFile is the absolute path to a file containing type |
| 482 | // information for the package as provided by the build system. |
| 483 | ExportFile string |
| 484 | |
| 485 | // Target is the absolute install path of the .a file, for libraries, |
| 486 | // and of the executable file, for binaries. |
| 487 | Target string |
| 488 | |
| 489 | // Imports maps import paths appearing in the package's Go source files |
| 490 | // to corresponding loaded Packages. |
| 491 | Imports map[string]*Package |
| 492 | |
| 493 | // Module is the module information for the package if it exists. |
| 494 | // |
| 495 | // Note: it may be missing for std and cmd; see Go issue #65816. |
| 496 | Module *Module |
| 497 | |
| 498 | // -- The following fields are not part of the driver JSON schema. -- |
| 499 | |
| 500 | // Types provides type information for the package. |
| 501 | // The NeedTypes LoadMode bit sets this field for packages matching the |
| 502 | // patterns; type information for dependencies may be missing or incomplete, |
| 503 | // unless NeedDeps and NeedImports are also set. |
| 504 | // |
| 505 | // Each call to [Load] returns a consistent set of type |
| 506 | // symbols, as defined by the comment at [types.Identical]. |
| 507 | // Avoid mixing type information from two or more calls to [Load]. |
| 508 | Types *types.Package `json:"-"` |
| 509 | |
| 510 | // Fset provides position information for Types, TypesInfo, and Syntax. |
| 511 | // It is set only when Types is set. |
| 512 | Fset *token.FileSet `json:"-"` |
| 513 | |
| 514 | // IllTyped indicates whether the package or any dependency contains errors. |
| 515 | // It is set only when Types is set. |
| 516 | IllTyped bool `json:"-"` |
| 517 | |
| 518 | // Syntax is the package's syntax trees, for the files listed in CompiledGoFiles. |
| 519 | // |
| 520 | // The NeedSyntax LoadMode bit populates this field for packages matching the patterns. |
| 521 | // If NeedDeps and NeedImports are also set, this field will also be populated |
| 522 | // for dependencies. |
| 523 | // |
| 524 | // Syntax is kept in the same order as CompiledGoFiles, with the caveat that nils are |
| 525 | // removed. If parsing returned nil, Syntax may be shorter than CompiledGoFiles. |
| 526 | Syntax []*ast.File `json:"-"` |
| 527 | |
| 528 | // TypesInfo provides type information about the package's syntax trees. |
| 529 | // It is set only when Syntax is set. |
| 530 | TypesInfo *types.Info `json:"-"` |
| 531 | |
| 532 | // TypesSizes provides the effective size function for types in TypesInfo. |
| 533 | TypesSizes types.Sizes `json:"-"` |
| 534 | |
| 535 | // -- internal -- |
| 536 | |
| 537 | // ForTest is the package under test, if any. |
| 538 | ForTest string |
| 539 | |
| 540 | // depsErrors is the DepsErrors field from the go list response, if any. |
| 541 | depsErrors []*packagesinternal.PackageError |
| 542 | |
| 543 | // exportDataError is the error encountered reading export data, if any. |
| 544 | // Decoding export data should ordinarily be infallible, so this typically |
| 545 | // indicates a producer/consumer version skew. |
| 546 | exportDataError error |
| 547 | } |
| 548 | |
| 549 | // Module provides module information for a package. |
| 550 | // |
| 551 | // It also defines part of the JSON schema of [DriverResponse]. |
| 552 | // See the package documentation for an overview. |
| 553 | type Module struct { |
| 554 | Path string // module path |
| 555 | Version string // module version |
| 556 | Replace *Module // replaced by this module |
| 557 | Time *time.Time // time version was created |
| 558 | Main bool // is this the main module? |
| 559 | Indirect bool // is this module only an indirect dependency of main module? |
| 560 | Dir string // directory holding files for this module, if any |
| 561 | GoMod string // path to go.mod file used when loading this module, if any |
| 562 | GoVersion string // go version used in module |
| 563 | Error *ModuleError // error loading module |
| 564 | } |
| 565 | |
| 566 | // ModuleError holds errors loading a module. |
| 567 | type ModuleError struct { |
| 568 | Err string // the error itself |
| 569 | } |
| 570 | |
| 571 | func init() { |
| 572 | packagesinternal.GetDepsErrors = func(p any) []*packagesinternal.PackageError { |
| 573 | return p.(*Package).depsErrors |
| 574 | } |
| 575 | packagesinternal.TypecheckCgo = int(typecheckCgo) |
| 576 | packagesinternal.DepsErrors = int(needInternalDepsErrors) |
| 577 | } |
| 578 | |
| 579 | // An Error describes a problem with a package's metadata, syntax, or types. |
| 580 | type Error struct { |
| 581 | Pos string // "file:line:col" or "file:line" or "" or "-" |
| 582 | Msg string |
| 583 | Kind ErrorKind |
| 584 | } |
| 585 | |
| 586 | // ErrorKind describes the source of the error, allowing the user to |
| 587 | // differentiate between errors generated by the driver, the parser, or the |
| 588 | // type-checker. |
| 589 | type ErrorKind int |
| 590 | |
| 591 | const ( |
| 592 | UnknownError ErrorKind = iota |
| 593 | ListError |
| 594 | ParseError |
| 595 | TypeError |
| 596 | ) |
| 597 | |
| 598 | func (err Error) Error() string { |
| 599 | pos := err.Pos |
| 600 | if pos == "" { |
| 601 | pos = "-" // like token.Position{}.String() |
| 602 | } |
| 603 | return pos + ": " + err.Msg |
| 604 | } |
| 605 | |
| 606 | // flatPackage is the JSON form of Package |
| 607 | // It drops all the type and syntax fields, and transforms the Imports |
| 608 | // |
| 609 | // TODO(adonovan): identify this struct with Package, effectively |
| 610 | // publishing the JSON protocol. |
| 611 | type flatPackage struct { |
| 612 | ID string |
| 613 | Name string `json:",omitempty"` |
| 614 | PkgPath string `json:",omitempty"` |
| 615 | Errors []Error `json:",omitempty"` |
| 616 | GoFiles []string `json:",omitempty"` |
| 617 | CompiledGoFiles []string `json:",omitempty"` |
| 618 | OtherFiles []string `json:",omitempty"` |
| 619 | EmbedFiles []string `json:",omitempty"` |
| 620 | EmbedPatterns []string `json:",omitempty"` |
| 621 | IgnoredFiles []string `json:",omitempty"` |
| 622 | ExportFile string `json:",omitempty"` |
| 623 | Imports map[string]string `json:",omitempty"` |
| 624 | } |
| 625 | |
| 626 | // MarshalJSON returns the Package in its JSON form. |
| 627 | // For the most part, the structure fields are written out unmodified, and |
| 628 | // the type and syntax fields are skipped. |
| 629 | // The imports are written out as just a map of path to package id. |
| 630 | // The errors are written using a custom type that tries to preserve the |
| 631 | // structure of error types we know about. |
| 632 | // |
| 633 | // This method exists to enable support for additional build systems. It is |
| 634 | // not intended for use by clients of the API and we may change the format. |
| 635 | func (p *Package) MarshalJSON() ([]byte, error) { |
| 636 | flat := &flatPackage{ |
| 637 | ID: p.ID, |
| 638 | Name: p.Name, |
| 639 | PkgPath: p.PkgPath, |
| 640 | Errors: p.Errors, |
| 641 | GoFiles: p.GoFiles, |
| 642 | CompiledGoFiles: p.CompiledGoFiles, |
| 643 | OtherFiles: p.OtherFiles, |
| 644 | EmbedFiles: p.EmbedFiles, |
| 645 | EmbedPatterns: p.EmbedPatterns, |
| 646 | IgnoredFiles: p.IgnoredFiles, |
| 647 | ExportFile: p.ExportFile, |
| 648 | } |
| 649 | if len(p.Imports) > 0 { |
| 650 | flat.Imports = make(map[string]string, len(p.Imports)) |
| 651 | for path, ipkg := range p.Imports { |
| 652 | flat.Imports[path] = ipkg.ID |
| 653 | } |
| 654 | } |
| 655 | return json.Marshal(flat) |
| 656 | } |
| 657 | |
| 658 | // UnmarshalJSON reads in a Package from its JSON format. |
| 659 | // See MarshalJSON for details about the format accepted. |
| 660 | func (p *Package) UnmarshalJSON(b []byte) error { |
| 661 | flat := &flatPackage{} |
| 662 | if err := json.Unmarshal(b, &flat); err != nil { |
| 663 | return err |
| 664 | } |
| 665 | *p = Package{ |
| 666 | ID: flat.ID, |
| 667 | Name: flat.Name, |
| 668 | PkgPath: flat.PkgPath, |
| 669 | Errors: flat.Errors, |
| 670 | GoFiles: flat.GoFiles, |
| 671 | CompiledGoFiles: flat.CompiledGoFiles, |
| 672 | OtherFiles: flat.OtherFiles, |
| 673 | EmbedFiles: flat.EmbedFiles, |
| 674 | EmbedPatterns: flat.EmbedPatterns, |
| 675 | IgnoredFiles: flat.IgnoredFiles, |
| 676 | ExportFile: flat.ExportFile, |
| 677 | } |
| 678 | if len(flat.Imports) > 0 { |
| 679 | p.Imports = make(map[string]*Package, len(flat.Imports)) |
| 680 | for path, id := range flat.Imports { |
| 681 | p.Imports[path] = &Package{ID: id} |
| 682 | } |
| 683 | } |
| 684 | return nil |
| 685 | } |
| 686 | |
| 687 | func (p *Package) String() string { return p.ID } |
| 688 | |
| 689 | // loaderPackage augments Package with state used during the loading phase |
| 690 | type loaderPackage struct { |
| 691 | *Package |
| 692 | importErrors map[string]error // maps each bad import to its error |
| 693 | preds []*loaderPackage // packages that import this one |
| 694 | unfinishedSuccs atomic.Int32 // number of direct imports not yet loaded |
| 695 | color uint8 // for cycle detection |
| 696 | needsrc bool // load from source (Mode >= LoadTypes) |
| 697 | needtypes bool // type information is either requested or depended on |
| 698 | initial bool // package was matched by a pattern |
| 699 | goVersion int // minor version number of go command on PATH |
| 700 | } |
| 701 | |
| 702 | // loader holds the working state of a single call to load. |
| 703 | type loader struct { |
| 704 | pkgs map[string]*loaderPackage // keyed by Package.ID |
| 705 | Config |
| 706 | sizes types.Sizes // non-nil if needed by mode |
| 707 | parseCache map[string]*parseValue |
| 708 | parseCacheMu sync.Mutex |
| 709 | exportMu sync.Mutex // enforces mutual exclusion of exportdata operations |
| 710 | externalDriver bool // true if an external GOPACKAGESDRIVER handled the request |
| 711 | |
| 712 | // Config.Mode contains the implied mode (see impliedLoadMode). |
| 713 | // Implied mode contains all the fields we need the data for. |
| 714 | // In requestedMode there are the actually requested fields. |
| 715 | // We'll zero them out before returning packages to the user. |
| 716 | // This makes it easier for us to get the conditions where |
| 717 | // we need certain modes right. |
| 718 | requestedMode LoadMode |
| 719 | } |
| 720 | |
| 721 | type parseValue struct { |
| 722 | f *ast.File |
| 723 | err error |
| 724 | ready chan struct{} |
| 725 | } |
| 726 | |
| 727 | func newLoader(cfg *Config) *loader { |
| 728 | ld := &loader{ |
| 729 | parseCache: map[string]*parseValue{}, |
| 730 | } |
| 731 | if cfg != nil { |
| 732 | ld.Config = *cfg |
| 733 | // If the user has provided a logger, use it. |
| 734 | ld.Config.Logf = cfg.Logf |
| 735 | } |
| 736 | if ld.Config.Logf == nil { |
| 737 | // If the GOPACKAGESDEBUG environment variable is set to true, |
| 738 | // but the user has not provided a logger, default to log.Printf. |
| 739 | if debug { |
| 740 | ld.Config.Logf = log.Printf |
| 741 | } else { |
| 742 | ld.Config.Logf = func(format string, args ...any) {} |
| 743 | } |
| 744 | } |
| 745 | if ld.Config.Mode == 0 { |
| 746 | ld.Config.Mode = NeedName | NeedFiles | NeedCompiledGoFiles // Preserve zero behavior of Mode for backwards compatibility. |
| 747 | } |
| 748 | if ld.Config.Env == nil { |
| 749 | ld.Config.Env = os.Environ() |
| 750 | } |
| 751 | if ld.Context == nil { |
| 752 | ld.Context = context.Background() |
| 753 | } |
| 754 | if ld.Dir == "" { |
| 755 | if dir, err := os.Getwd(); err == nil { |
| 756 | ld.Dir = dir |
| 757 | } |
| 758 | } |
| 759 | |
| 760 | // Save the actually requested fields. We'll zero them out before returning packages to the user. |
| 761 | ld.requestedMode = ld.Mode |
| 762 | ld.Mode = impliedLoadMode(ld.Mode) |
| 763 | |
| 764 | if ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0 { |
| 765 | if ld.Fset == nil { |
| 766 | ld.Fset = token.NewFileSet() |
| 767 | } |
| 768 | |
| 769 | // ParseFile is required even in LoadTypes mode |
| 770 | // because we load source if export data is missing. |
| 771 | if ld.ParseFile == nil { |
| 772 | ld.ParseFile = func(fset *token.FileSet, filename string, src []byte) (*ast.File, error) { |
| 773 | // We implicitly promise to keep doing ast.Object resolution. :( |
| 774 | const mode = parser.AllErrors | parser.ParseComments |
| 775 | return parser.ParseFile(fset, filename, src, mode) |
| 776 | } |
| 777 | } |
| 778 | } |
| 779 | |
| 780 | return ld |
| 781 | } |
| 782 | |
| 783 | // refine connects the supplied packages into a graph and then adds type |
| 784 | // and syntax information as requested by the LoadMode. |
| 785 | func (ld *loader) refine(response *DriverResponse) ([]*Package, error) { |
| 786 | roots := response.Roots |
| 787 | rootMap := make(map[string]int, len(roots)) |
| 788 | for i, root := range roots { |
| 789 | rootMap[root] = i |
| 790 | } |
| 791 | ld.pkgs = make(map[string]*loaderPackage) |
| 792 | // first pass, fixup and build the map and roots |
| 793 | var initial = make([]*loaderPackage, len(roots)) |
| 794 | for _, pkg := range response.Packages { |
| 795 | rootIndex := -1 |
| 796 | if i, found := rootMap[pkg.ID]; found { |
| 797 | rootIndex = i |
| 798 | } |
| 799 | |
| 800 | // Overlays can invalidate export data. |
| 801 | // TODO(matloob): make this check fine-grained based on dependencies on overlaid files |
| 802 | exportDataInvalid := len(ld.Overlay) > 0 || pkg.ExportFile == "" && pkg.PkgPath != "unsafe" |
| 803 | // This package needs type information if the caller requested types and the package is |
| 804 | // either a root, or it's a non-root and the user requested dependencies ... |
| 805 | needtypes := (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) |
| 806 | // This package needs source if the call requested source (or types info, which implies source) |
| 807 | // and the package is either a root, or itas a non- root and the user requested dependencies... |
| 808 | needsrc := ((ld.Mode&(NeedSyntax|NeedTypesInfo) != 0 && (rootIndex >= 0 || ld.Mode&NeedDeps != 0)) || |
| 809 | // ... or if we need types and the exportData is invalid. We fall back to (incompletely) |
| 810 | // typechecking packages from source if they fail to compile. |
| 811 | (ld.Mode&(NeedTypes|NeedTypesInfo) != 0 && exportDataInvalid)) && pkg.PkgPath != "unsafe" |
| 812 | lpkg := &loaderPackage{ |
| 813 | Package: pkg, |
| 814 | needtypes: needtypes, |
| 815 | needsrc: needsrc, |
| 816 | goVersion: response.GoVersion, |
| 817 | } |
| 818 | ld.pkgs[lpkg.ID] = lpkg |
| 819 | if rootIndex >= 0 { |
| 820 | initial[rootIndex] = lpkg |
| 821 | lpkg.initial = true |
| 822 | } |
| 823 | } |
| 824 | for i, root := range roots { |
| 825 | if initial[i] == nil { |
| 826 | return nil, fmt.Errorf("root package %v is missing", root) |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | // Materialize the import graph if it is needed (NeedImports), |
| 831 | // or if we'll be using loadPackages (Need{Syntax|Types|TypesInfo}). |
| 832 | var leaves []*loaderPackage // packages with no unfinished successors |
| 833 | if ld.Mode&(NeedImports|NeedSyntax|NeedTypes|NeedTypesInfo) != 0 { |
| 834 | const ( |
| 835 | white = 0 // new |
| 836 | grey = 1 // in progress |
| 837 | black = 2 // complete |
| 838 | ) |
| 839 | |
| 840 | // visit traverses the import graph, depth-first, |
| 841 | // and materializes the graph as Packages.Imports. |
| 842 | // |
| 843 | // Valid imports are saved in the Packages.Import map. |
| 844 | // Invalid imports (cycles and missing nodes) are saved in the importErrors map. |
| 845 | // Thus, even in the presence of both kinds of errors, |
| 846 | // the Import graph remains a DAG. |
| 847 | // |
| 848 | // visit returns whether the package needs src or has a transitive |
| 849 | // dependency on a package that does. These are the only packages |
| 850 | // for which we load source code. |
| 851 | var stack []*loaderPackage |
| 852 | var visit func(from, lpkg *loaderPackage) bool |
| 853 | visit = func(from, lpkg *loaderPackage) bool { |
| 854 | if lpkg.color == grey { |
| 855 | panic("internal error: grey node") |
| 856 | } |
| 857 | if lpkg.color == white { |
| 858 | lpkg.color = grey |
| 859 | stack = append(stack, lpkg) // push |
| 860 | stubs := lpkg.Imports // the structure form has only stubs with the ID in the Imports |
| 861 | lpkg.Imports = make(map[string]*Package, len(stubs)) |
| 862 | for importPath, ipkg := range stubs { |
| 863 | var importErr error |
| 864 | imp := ld.pkgs[ipkg.ID] |
| 865 | if imp == nil { |
| 866 | // (includes package "C" when DisableCgo) |
| 867 | importErr = fmt.Errorf("missing package: %q", ipkg.ID) |
| 868 | } else if imp.color == grey { |
| 869 | importErr = fmt.Errorf("import cycle: %s", stack) |
| 870 | } |
| 871 | if importErr != nil { |
| 872 | if lpkg.importErrors == nil { |
| 873 | lpkg.importErrors = make(map[string]error) |
| 874 | } |
| 875 | lpkg.importErrors[importPath] = importErr |
| 876 | continue |
| 877 | } |
| 878 | |
| 879 | if visit(lpkg, imp) { |
| 880 | lpkg.needsrc = true |
| 881 | } |
| 882 | lpkg.Imports[importPath] = imp.Package |
| 883 | } |
| 884 | |
| 885 | // -- postorder -- |
| 886 | |
| 887 | // Complete type information is required for the |
| 888 | // immediate dependencies of each source package. |
| 889 | if lpkg.needsrc && ld.Mode&NeedTypes != 0 { |
| 890 | for _, ipkg := range lpkg.Imports { |
| 891 | ld.pkgs[ipkg.ID].needtypes = true |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | // NeedTypeSizes causes TypeSizes to be set even |
| 896 | // on packages for which types aren't needed. |
| 897 | if ld.Mode&NeedTypesSizes != 0 { |
| 898 | lpkg.TypesSizes = ld.sizes |
| 899 | } |
| 900 | |
| 901 | // Add packages with no imports directly to the queue of leaves. |
| 902 | if len(lpkg.Imports) == 0 { |
| 903 | leaves = append(leaves, lpkg) |
| 904 | } |
| 905 | |
| 906 | stack = stack[:len(stack)-1] // pop |
| 907 | lpkg.color = black |
| 908 | } |
| 909 | |
| 910 | // Add edge from predecessor. |
| 911 | if from != nil { |
| 912 | from.unfinishedSuccs.Add(+1) // incref |
| 913 | lpkg.preds = append(lpkg.preds, from) |
| 914 | } |
| 915 | |
| 916 | return lpkg.needsrc |
| 917 | } |
| 918 | |
| 919 | // For each initial package, create its import DAG. |
| 920 | for _, lpkg := range initial { |
| 921 | visit(nil, lpkg) |
| 922 | } |
| 923 | |
| 924 | } else { |
| 925 | // !NeedImports: drop the stub (ID-only) import packages |
| 926 | // that we are not even going to try to resolve. |
| 927 | for _, lpkg := range initial { |
| 928 | lpkg.Imports = nil |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | // Load type data and syntax if needed, starting at |
| 933 | // the initial packages (roots of the import DAG). |
| 934 | if ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0 { |
| 935 | |
| 936 | // We avoid using g.SetLimit to limit concurrency as |
| 937 | // it makes g.Go stop accepting work, which prevents |
| 938 | // workers from enqeuing, and thus finishing, and thus |
| 939 | // allowing the group to make progress: deadlock. |
| 940 | // |
| 941 | // Instead we use the ioLimit and cpuLimit semaphores. |
| 942 | g, _ := errgroup.WithContext(ld.Context) |
| 943 | |
| 944 | // enqueues adds a package to the type-checking queue. |
| 945 | // It must have no unfinished successors. |
| 946 | var enqueue func(*loaderPackage) |
| 947 | enqueue = func(lpkg *loaderPackage) { |
| 948 | g.Go(func() error { |
| 949 | // Parse and type-check. |
| 950 | ld.loadPackage(lpkg) |
| 951 | |
| 952 | // Notify each waiting predecessor, |
| 953 | // and enqueue it when it becomes a leaf. |
| 954 | for _, pred := range lpkg.preds { |
| 955 | if pred.unfinishedSuccs.Add(-1) == 0 { // decref |
| 956 | enqueue(pred) |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | return nil |
| 961 | }) |
| 962 | } |
| 963 | |
| 964 | // Load leaves first, adding new packages |
| 965 | // to the queue as they become leaves. |
| 966 | for _, leaf := range leaves { |
| 967 | enqueue(leaf) |
| 968 | } |
| 969 | |
| 970 | if err := g.Wait(); err != nil { |
| 971 | return nil, err // cancelled |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // If the context is done, return its error and |
| 976 | // throw out [likely] incomplete packages. |
| 977 | if err := ld.Context.Err(); err != nil { |
| 978 | return nil, err |
| 979 | } |
| 980 | |
| 981 | result := make([]*Package, len(initial)) |
| 982 | for i, lpkg := range initial { |
| 983 | result[i] = lpkg.Package |
| 984 | } |
| 985 | for i := range ld.pkgs { |
| 986 | // Clear all unrequested fields, |
| 987 | // to catch programs that use more than they request. |
| 988 | if ld.requestedMode&NeedName == 0 { |
| 989 | ld.pkgs[i].Name = "" |
| 990 | ld.pkgs[i].PkgPath = "" |
| 991 | } |
| 992 | if ld.requestedMode&NeedFiles == 0 { |
| 993 | ld.pkgs[i].GoFiles = nil |
| 994 | ld.pkgs[i].OtherFiles = nil |
| 995 | ld.pkgs[i].IgnoredFiles = nil |
| 996 | } |
| 997 | if ld.requestedMode&NeedEmbedFiles == 0 { |
| 998 | ld.pkgs[i].EmbedFiles = nil |
| 999 | } |
| 1000 | if ld.requestedMode&NeedEmbedPatterns == 0 { |
| 1001 | ld.pkgs[i].EmbedPatterns = nil |
| 1002 | } |
| 1003 | if ld.requestedMode&NeedCompiledGoFiles == 0 { |
| 1004 | ld.pkgs[i].CompiledGoFiles = nil |
| 1005 | } |
| 1006 | if ld.requestedMode&NeedImports == 0 { |
| 1007 | ld.pkgs[i].Imports = nil |
| 1008 | } |
| 1009 | if ld.requestedMode&NeedExportFile == 0 { |
| 1010 | ld.pkgs[i].ExportFile = "" |
| 1011 | } |
| 1012 | if ld.requestedMode&NeedTypes == 0 { |
| 1013 | ld.pkgs[i].Types = nil |
| 1014 | ld.pkgs[i].IllTyped = false |
| 1015 | } |
| 1016 | if ld.requestedMode&NeedSyntax == 0 { |
| 1017 | ld.pkgs[i].Syntax = nil |
| 1018 | } |
| 1019 | if ld.requestedMode&(NeedSyntax|NeedTypes|NeedTypesInfo) == 0 { |
| 1020 | ld.pkgs[i].Fset = nil |
| 1021 | } |
| 1022 | if ld.requestedMode&NeedTypesInfo == 0 { |
| 1023 | ld.pkgs[i].TypesInfo = nil |
| 1024 | } |
| 1025 | if ld.requestedMode&NeedTypesSizes == 0 { |
| 1026 | ld.pkgs[i].TypesSizes = nil |
| 1027 | } |
| 1028 | if ld.requestedMode&NeedModule == 0 { |
| 1029 | ld.pkgs[i].Module = nil |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | return result, nil |
| 1034 | } |
| 1035 | |
| 1036 | // loadPackage loads/parses/typechecks the specified package. |
| 1037 | // It must be called only once per Package, |
| 1038 | // after immediate dependencies are loaded. |
| 1039 | // Precondition: ld.Mode&(NeedSyntax|NeedTypes|NeedTypesInfo) != 0. |
| 1040 | func (ld *loader) loadPackage(lpkg *loaderPackage) { |
| 1041 | if lpkg.PkgPath == "unsafe" { |
| 1042 | // To avoid surprises, fill in the blanks consistent |
| 1043 | // with other packages. (For example, some analyzers |
| 1044 | // assert that each needed types.Info map is non-nil |
| 1045 | // even when there is no syntax that would cause them |
| 1046 | // to consult the map.) |
| 1047 | lpkg.Types = types.Unsafe |
| 1048 | lpkg.Fset = ld.Fset |
| 1049 | lpkg.Syntax = []*ast.File{} |
| 1050 | lpkg.TypesInfo = ld.newTypesInfo() |
| 1051 | lpkg.TypesSizes = ld.sizes |
| 1052 | return |
| 1053 | } |
| 1054 | |
| 1055 | // Call NewPackage directly with explicit name. |
| 1056 | // This avoids skew between golist and go/types when the files' |
| 1057 | // package declarations are inconsistent. |
| 1058 | lpkg.Types = types.NewPackage(lpkg.PkgPath, lpkg.Name) |
| 1059 | lpkg.Fset = ld.Fset |
| 1060 | |
| 1061 | // Start shutting down if the context is done and do not load |
| 1062 | // source or export data files. |
| 1063 | // Packages that import this one will have ld.Context.Err() != nil. |
| 1064 | // ld.Context.Err() will be returned later by refine. |
| 1065 | if ld.Context.Err() != nil { |
| 1066 | return |
| 1067 | } |
| 1068 | |
| 1069 | // Subtle: we populate all Types fields with an empty Package |
| 1070 | // before loading export data so that export data processing |
| 1071 | // never has to create a types.Package for an indirect dependency, |
| 1072 | // which would then require that such created packages be explicitly |
| 1073 | // inserted back into the Import graph as a final step after export data loading. |
| 1074 | // (Hence this return is after the Types assignment.) |
| 1075 | // The Diamond test exercises this case. |
| 1076 | if !lpkg.needtypes && !lpkg.needsrc { |
| 1077 | return |
| 1078 | } |
| 1079 | |
| 1080 | // TODO(adonovan): this condition looks wrong: |
| 1081 | // I think it should be lpkg.needtypes && !lpkg.needsrc, |
| 1082 | // so that NeedSyntax without NeedTypes can be satisfied by export data. |
| 1083 | if !lpkg.needsrc { |
| 1084 | if err := ld.loadFromExportData(lpkg); err != nil { |
| 1085 | lpkg.exportDataError = err |
| 1086 | lpkg.Errors = append(lpkg.Errors, Error{ |
| 1087 | Pos: "-", |
| 1088 | Msg: err.Error(), |
| 1089 | Kind: UnknownError, // e.g. can't find/open/parse export data |
| 1090 | }) |
| 1091 | } |
| 1092 | return // not a source package, don't get syntax trees |
| 1093 | } |
| 1094 | |
| 1095 | appendError := func(err error) { |
| 1096 | // Convert various error types into the one true Error. |
| 1097 | var errs []Error |
| 1098 | switch err := err.(type) { |
| 1099 | case Error: |
| 1100 | // from driver |
| 1101 | errs = append(errs, err) |
| 1102 | |
| 1103 | case *os.PathError: |
| 1104 | // from parser |
| 1105 | errs = append(errs, Error{ |
| 1106 | Pos: err.Path + ":1", |
| 1107 | Msg: err.Err.Error(), |
| 1108 | Kind: ParseError, |
| 1109 | }) |
| 1110 | |
| 1111 | case scanner.ErrorList: |
| 1112 | // from parser |
| 1113 | for _, err := range err { |
| 1114 | errs = append(errs, Error{ |
| 1115 | Pos: err.Pos.String(), |
| 1116 | Msg: err.Msg, |
| 1117 | Kind: ParseError, |
| 1118 | }) |
| 1119 | } |
| 1120 | |
| 1121 | case types.Error: |
| 1122 | // from type checker |
| 1123 | lpkg.TypeErrors = append(lpkg.TypeErrors, err) |
| 1124 | errs = append(errs, Error{ |
| 1125 | Pos: err.Fset.Position(err.Pos).String(), |
| 1126 | Msg: err.Msg, |
| 1127 | Kind: TypeError, |
| 1128 | }) |
| 1129 | |
| 1130 | default: |
| 1131 | // unexpected impoverished error from parser? |
| 1132 | errs = append(errs, Error{ |
| 1133 | Pos: "-", |
| 1134 | Msg: err.Error(), |
| 1135 | Kind: UnknownError, |
| 1136 | }) |
| 1137 | |
| 1138 | // If you see this error message, please file a bug. |
| 1139 | log.Printf("internal error: error %q (%T) without position", err, err) |
| 1140 | } |
| 1141 | |
| 1142 | lpkg.Errors = append(lpkg.Errors, errs...) |
| 1143 | } |
| 1144 | |
| 1145 | // If the go command on the PATH is newer than the runtime, |
| 1146 | // then the go/{scanner,ast,parser,types} packages from the |
| 1147 | // standard library may be unable to process the files |
| 1148 | // selected by go list. |
| 1149 | // |
| 1150 | // There is currently no way to downgrade the effective |
| 1151 | // version of the go command (see issue 52078), so we proceed |
| 1152 | // with the newer go command but, in case of parse or type |
| 1153 | // errors, we emit an additional diagnostic. |
| 1154 | // |
| 1155 | // See: |
| 1156 | // - golang.org/issue/52078 (flag to set release tags) |
| 1157 | // - golang.org/issue/50825 (gopls legacy version support) |
| 1158 | // - golang.org/issue/55883 (go/packages confusing error) |
| 1159 | // |
| 1160 | // Should we assert a hard minimum of (currently) go1.16 here? |
| 1161 | var runtimeVersion int |
| 1162 | if _, err := fmt.Sscanf(runtime.Version(), "go1.%d", &runtimeVersion); err == nil && runtimeVersion < lpkg.goVersion { |
| 1163 | defer func() { |
| 1164 | if len(lpkg.Errors) > 0 { |
| 1165 | appendError(Error{ |
| 1166 | Pos: "-", |
| 1167 | 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), |
| 1168 | Kind: UnknownError, |
| 1169 | }) |
| 1170 | } |
| 1171 | }() |
| 1172 | } |
| 1173 | |
| 1174 | if ld.Config.Mode&NeedTypes != 0 && len(lpkg.CompiledGoFiles) == 0 && lpkg.ExportFile != "" { |
| 1175 | // The config requested loading sources and types, but sources are missing. |
| 1176 | // Add an error to the package and fall back to loading from export data. |
| 1177 | appendError(Error{"-", fmt.Sprintf("sources missing for package %s", lpkg.ID), ParseError}) |
| 1178 | _ = ld.loadFromExportData(lpkg) // ignore any secondary errors |
| 1179 | |
| 1180 | return // can't get syntax trees for this package |
| 1181 | } |
| 1182 | |
| 1183 | files, errs := ld.parseFiles(lpkg.CompiledGoFiles) |
| 1184 | for _, err := range errs { |
| 1185 | appendError(err) |
| 1186 | } |
| 1187 | |
| 1188 | lpkg.Syntax = files |
| 1189 | if ld.Config.Mode&(NeedTypes|NeedTypesInfo) == 0 { |
| 1190 | return |
| 1191 | } |
| 1192 | |
| 1193 | // Start shutting down if the context is done and do not type check. |
| 1194 | // Packages that import this one will have ld.Context.Err() != nil. |
| 1195 | // ld.Context.Err() will be returned later by refine. |
| 1196 | if ld.Context.Err() != nil { |
| 1197 | return |
| 1198 | } |
| 1199 | |
| 1200 | lpkg.TypesInfo = ld.newTypesInfo() |
| 1201 | lpkg.TypesSizes = ld.sizes |
| 1202 | |
| 1203 | importer := importerFunc(func(path string) (*types.Package, error) { |
| 1204 | if path == "unsafe" { |
| 1205 | return types.Unsafe, nil |
| 1206 | } |
| 1207 | |
| 1208 | // The imports map is keyed by import path. |
| 1209 | ipkg := lpkg.Imports[path] |
| 1210 | if ipkg == nil { |
| 1211 | if err := lpkg.importErrors[path]; err != nil { |
| 1212 | return nil, err |
| 1213 | } |
| 1214 | // There was skew between the metadata and the |
| 1215 | // import declarations, likely due to an edit |
| 1216 | // race, or because the ParseFile feature was |
| 1217 | // used to supply alternative file contents. |
| 1218 | return nil, fmt.Errorf("no metadata for %s", path) |
| 1219 | } |
| 1220 | |
| 1221 | if ipkg.Types != nil && ipkg.Types.Complete() { |
| 1222 | return ipkg.Types, nil |
| 1223 | } |
| 1224 | |
| 1225 | // If types are unavailable, there must be an export data error. |
| 1226 | if ipkg.exportDataError != nil { |
| 1227 | return nil, ipkg.exportDataError |
| 1228 | } |
| 1229 | |
| 1230 | log.Fatalf("internal error: expected complete types for package %q", path) |
| 1231 | panic("unreachable") |
| 1232 | }) |
| 1233 | |
| 1234 | // type-check |
| 1235 | tc := &types.Config{ |
| 1236 | Importer: importer, |
| 1237 | |
| 1238 | // Type-check bodies of functions only in initial packages. |
| 1239 | // Example: for import graph A->B->C and initial packages {A,C}, |
| 1240 | // we can ignore function bodies in B. |
| 1241 | IgnoreFuncBodies: ld.Mode&NeedDeps == 0 && !lpkg.initial, |
| 1242 | |
| 1243 | Error: appendError, |
| 1244 | Sizes: ld.sizes, // may be nil |
| 1245 | } |
| 1246 | if lpkg.Module != nil && lpkg.Module.GoVersion != "" { |
| 1247 | tc.GoVersion = "go" + lpkg.Module.GoVersion |
| 1248 | } else if ld.externalDriver && lpkg.goVersion != 0 { |
| 1249 | // Module information is missing when GOPACKAGESDRIVER is used, |
| 1250 | // so use the go version from the driver response. |
| 1251 | tc.GoVersion = fmt.Sprintf("go1.%d", lpkg.goVersion) |
| 1252 | } |
| 1253 | if (ld.Mode & typecheckCgo) != 0 { |
| 1254 | if !typesinternal.SetUsesCgo(tc) { |
| 1255 | appendError(Error{ |
| 1256 | Msg: "typecheckCgo requires Go 1.15+", |
| 1257 | Kind: ListError, |
| 1258 | }) |
| 1259 | return |
| 1260 | } |
| 1261 | } |
| 1262 | |
| 1263 | // Type-checking is CPU intensive. |
| 1264 | cpuLimit <- unit{} // acquire a token |
| 1265 | defer func() { <-cpuLimit }() // release a token |
| 1266 | |
| 1267 | typErr := types.NewChecker(tc, ld.Fset, lpkg.Types, lpkg.TypesInfo).Files(lpkg.Syntax) |
| 1268 | lpkg.importErrors = nil // no longer needed |
| 1269 | |
| 1270 | // In go/types go1.21 and go1.22, Checker.Files failed fast with a |
| 1271 | // a "too new" error, without calling tc.Error and without |
| 1272 | // proceeding to type-check the package (#66525). |
| 1273 | // We rely on the runtimeVersion error to give the suggested remedy. |
| 1274 | if typErr != nil && len(lpkg.Errors) == 0 && len(lpkg.Syntax) > 0 { |
| 1275 | if msg := typErr.Error(); strings.HasPrefix(msg, "package requires newer Go version") { |
| 1276 | appendError(types.Error{ |
| 1277 | Fset: ld.Fset, |
| 1278 | Pos: lpkg.Syntax[0].Package, |
| 1279 | Msg: msg, |
| 1280 | }) |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | // If !Cgo, the type-checker uses FakeImportC mode, so |
| 1285 | // it doesn't invoke the importer for import "C", |
| 1286 | // nor report an error for the import, |
| 1287 | // or for any undefined C.f reference. |
| 1288 | // We must detect this explicitly and correctly |
| 1289 | // mark the package as IllTyped (by reporting an error). |
| 1290 | // TODO(adonovan): if these errors are annoying, |
| 1291 | // we could just set IllTyped quietly. |
| 1292 | if tc.FakeImportC { |
| 1293 | outer: |
| 1294 | for _, f := range lpkg.Syntax { |
| 1295 | for _, imp := range f.Imports { |
| 1296 | if imp.Path.Value == `"C"` { |
| 1297 | err := types.Error{Fset: ld.Fset, Pos: imp.Pos(), Msg: `import "C" ignored`} |
| 1298 | appendError(err) |
| 1299 | break outer |
| 1300 | } |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | // If types.Checker.Files had an error that was unreported, |
| 1306 | // make sure to report the unknown error so the package is illTyped. |
| 1307 | if typErr != nil && len(lpkg.Errors) == 0 { |
| 1308 | appendError(typErr) |
| 1309 | } |
| 1310 | |
| 1311 | // Record accumulated errors. |
| 1312 | illTyped := len(lpkg.Errors) > 0 |
| 1313 | if !illTyped { |
| 1314 | for _, imp := range lpkg.Imports { |
| 1315 | if imp.IllTyped { |
| 1316 | illTyped = true |
| 1317 | break |
| 1318 | } |
| 1319 | } |
| 1320 | } |
| 1321 | lpkg.IllTyped = illTyped |
| 1322 | } |
| 1323 | |
| 1324 | func (ld *loader) newTypesInfo() *types.Info { |
| 1325 | // Populate TypesInfo only if needed, as it |
| 1326 | // causes the type checker to work much harder. |
| 1327 | if ld.Config.Mode&NeedTypesInfo == 0 { |
| 1328 | return nil |
| 1329 | } |
| 1330 | return &types.Info{ |
| 1331 | Types: make(map[ast.Expr]types.TypeAndValue), |
| 1332 | Defs: make(map[*ast.Ident]types.Object), |
| 1333 | Uses: make(map[*ast.Ident]types.Object), |
| 1334 | Implicits: make(map[ast.Node]types.Object), |
| 1335 | Instances: make(map[*ast.Ident]types.Instance), |
| 1336 | Scopes: make(map[ast.Node]*types.Scope), |
| 1337 | Selections: make(map[*ast.SelectorExpr]*types.Selection), |
| 1338 | FileVersions: make(map[*ast.File]string), |
| 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | // An importFunc is an implementation of the single-method |
| 1343 | // types.Importer interface based on a function value. |
| 1344 | type importerFunc func(path string) (*types.Package, error) |
| 1345 | |
| 1346 | func (f importerFunc) Import(path string) (*types.Package, error) { return f(path) } |
| 1347 | |
| 1348 | // We use a counting semaphore to limit |
| 1349 | // the number of parallel I/O calls or CPU threads per process. |
| 1350 | var ( |
| 1351 | ioLimit = make(chan unit, 20) |
| 1352 | cpuLimit = make(chan unit, runtime.GOMAXPROCS(0)) |
| 1353 | ) |
| 1354 | |
| 1355 | func (ld *loader) parseFile(filename string) (*ast.File, error) { |
| 1356 | ld.parseCacheMu.Lock() |
| 1357 | v, ok := ld.parseCache[filename] |
| 1358 | if ok { |
| 1359 | // cache hit |
| 1360 | ld.parseCacheMu.Unlock() |
| 1361 | <-v.ready |
| 1362 | } else { |
| 1363 | // cache miss |
| 1364 | v = &parseValue{ready: make(chan struct{})} |
| 1365 | ld.parseCache[filename] = v |
| 1366 | ld.parseCacheMu.Unlock() |
| 1367 | |
| 1368 | var src []byte |
| 1369 | for f, contents := range ld.Config.Overlay { |
| 1370 | // TODO(adonovan): Inefficient for large overlays. |
| 1371 | // Do an exact name-based map lookup |
| 1372 | // (for nonexistent files) followed by a |
| 1373 | // FileID-based map lookup (for existing ones). |
| 1374 | if sameFile(f, filename) { |
| 1375 | src = contents |
| 1376 | break |
| 1377 | } |
| 1378 | } |
| 1379 | var err error |
| 1380 | if src == nil { |
| 1381 | ioLimit <- unit{} // acquire a token |
| 1382 | src, err = os.ReadFile(filename) |
| 1383 | <-ioLimit // release a token |
| 1384 | } |
| 1385 | if err != nil { |
| 1386 | v.err = err |
| 1387 | } else { |
| 1388 | // Parsing is CPU intensive. |
| 1389 | cpuLimit <- unit{} // acquire a token |
| 1390 | v.f, v.err = ld.ParseFile(ld.Fset, filename, src) |
| 1391 | <-cpuLimit // release a token |
| 1392 | } |
| 1393 | |
| 1394 | close(v.ready) |
| 1395 | } |
| 1396 | return v.f, v.err |
| 1397 | } |
| 1398 | |
| 1399 | // parseFiles reads and parses the Go source files and returns the ASTs |
| 1400 | // of the ones that could be at least partially parsed, along with a |
| 1401 | // list of I/O and parse errors encountered. |
| 1402 | // |
| 1403 | // Because files are scanned in parallel, the token.Pos |
| 1404 | // positions of the resulting ast.Files are not ordered. |
| 1405 | func (ld *loader) parseFiles(filenames []string) ([]*ast.File, []error) { |
| 1406 | var ( |
| 1407 | n = len(filenames) |
| 1408 | parsed = make([]*ast.File, n) |
| 1409 | errors = make([]error, n) |
| 1410 | ) |
| 1411 | var g errgroup.Group |
| 1412 | for i, filename := range filenames { |
| 1413 | // This creates goroutines unnecessarily in the |
| 1414 | // cache-hit case, but that case is uncommon. |
| 1415 | g.Go(func() error { |
| 1416 | parsed[i], errors[i] = ld.parseFile(filename) |
| 1417 | return nil |
| 1418 | }) |
| 1419 | } |
| 1420 | g.Wait() |
| 1421 | |
| 1422 | // Eliminate nils, preserving order. |
| 1423 | var o int |
| 1424 | for _, f := range parsed { |
| 1425 | if f != nil { |
| 1426 | parsed[o] = f |
| 1427 | o++ |
| 1428 | } |
| 1429 | } |
| 1430 | parsed = parsed[:o] |
| 1431 | |
| 1432 | o = 0 |
| 1433 | for _, err := range errors { |
| 1434 | if err != nil { |
| 1435 | errors[o] = err |
| 1436 | o++ |
| 1437 | } |
| 1438 | } |
| 1439 | errors = errors[:o] |
| 1440 | |
| 1441 | return parsed, errors |
| 1442 | } |
| 1443 | |
| 1444 | // sameFile returns true if x and y have the same basename and denote |
| 1445 | // the same file. |
| 1446 | func sameFile(x, y string) bool { |
| 1447 | if x == y { |
| 1448 | // It could be the case that y doesn't exist. |
| 1449 | // For instance, it may be an overlay file that |
| 1450 | // hasn't been written to disk. To handle that case |
| 1451 | // let x == y through. (We added the exact absolute path |
| 1452 | // string to the CompiledGoFiles list, so the unwritten |
| 1453 | // overlay case implies x==y.) |
| 1454 | return true |
| 1455 | } |
| 1456 | if strings.EqualFold(filepath.Base(x), filepath.Base(y)) { // (optimisation) |
| 1457 | if xi, err := os.Stat(x); err == nil { |
| 1458 | if yi, err := os.Stat(y); err == nil { |
| 1459 | return os.SameFile(xi, yi) |
| 1460 | } |
| 1461 | } |
| 1462 | } |
| 1463 | return false |
| 1464 | } |
| 1465 | |
| 1466 | // loadFromExportData ensures that type information is present for the specified |
| 1467 | // package, loading it from an export data file on the first request. |
| 1468 | // On success it sets lpkg.Types to a new Package. |
| 1469 | func (ld *loader) loadFromExportData(lpkg *loaderPackage) error { |
| 1470 | if lpkg.PkgPath == "" { |
| 1471 | log.Fatalf("internal error: Package %s has no PkgPath", lpkg) |
| 1472 | } |
| 1473 | |
| 1474 | // Because gcexportdata.Read has the potential to create or |
| 1475 | // modify the types.Package for each node in the transitive |
| 1476 | // closure of dependencies of lpkg, all exportdata operations |
| 1477 | // must be sequential. (Finer-grained locking would require |
| 1478 | // changes to the gcexportdata API.) |
| 1479 | // |
| 1480 | // The exportMu lock guards the lpkg.Types field and the |
| 1481 | // types.Package it points to, for each loaderPackage in the graph. |
| 1482 | // |
| 1483 | // Not all accesses to Package.Pkg need to be protected by exportMu: |
| 1484 | // graph ordering ensures that direct dependencies of source |
| 1485 | // packages are fully loaded before the importer reads their Pkg field. |
| 1486 | ld.exportMu.Lock() |
| 1487 | defer ld.exportMu.Unlock() |
| 1488 | |
| 1489 | if tpkg := lpkg.Types; tpkg != nil && tpkg.Complete() { |
| 1490 | return nil // cache hit |
| 1491 | } |
| 1492 | |
| 1493 | lpkg.IllTyped = true // fail safe |
| 1494 | |
| 1495 | if lpkg.ExportFile == "" { |
| 1496 | // Errors while building export data will have been printed to stderr. |
| 1497 | return fmt.Errorf("no export data file") |
| 1498 | } |
| 1499 | f, err := os.Open(lpkg.ExportFile) |
| 1500 | if err != nil { |
| 1501 | return err |
| 1502 | } |
| 1503 | defer f.Close() |
| 1504 | |
| 1505 | // Read gc export data. |
| 1506 | // |
| 1507 | // We don't currently support gccgo export data because all |
| 1508 | // underlying workspaces use the gc toolchain. (Even build |
| 1509 | // systems that support gccgo don't use it for workspace |
| 1510 | // queries.) |
| 1511 | r, err := gcexportdata.NewReader(f) |
| 1512 | if err != nil { |
| 1513 | return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err) |
| 1514 | } |
| 1515 | |
| 1516 | // Build the view. |
| 1517 | // |
| 1518 | // The gcexportdata machinery has no concept of package ID. |
| 1519 | // It identifies packages by their PkgPath, which although not |
| 1520 | // globally unique is unique within the scope of one invocation |
| 1521 | // of the linker, type-checker, or gcexportdata. |
| 1522 | // |
| 1523 | // So, we must build a PkgPath-keyed view of the global |
| 1524 | // (conceptually ID-keyed) cache of packages and pass it to |
| 1525 | // gcexportdata. The view must contain every existing |
| 1526 | // package that might possibly be mentioned by the |
| 1527 | // current package---its transitive closure. |
| 1528 | // |
| 1529 | // In loadPackage, we unconditionally create a types.Package for |
| 1530 | // each dependency so that export data loading does not |
| 1531 | // create new ones. |
| 1532 | // |
| 1533 | // TODO(adonovan): it would be simpler and more efficient |
| 1534 | // if the export data machinery invoked a callback to |
| 1535 | // get-or-create a package instead of a map. |
| 1536 | // |
| 1537 | view := make(map[string]*types.Package) // view seen by gcexportdata |
| 1538 | seen := make(map[*loaderPackage]bool) // all visited packages |
| 1539 | var visit func(pkgs map[string]*Package) |
| 1540 | visit = func(pkgs map[string]*Package) { |
| 1541 | for _, p := range pkgs { |
| 1542 | lpkg := ld.pkgs[p.ID] |
| 1543 | if !seen[lpkg] { |
| 1544 | seen[lpkg] = true |
| 1545 | view[lpkg.PkgPath] = lpkg.Types |
| 1546 | visit(lpkg.Imports) |
| 1547 | } |
| 1548 | } |
| 1549 | } |
| 1550 | visit(lpkg.Imports) |
| 1551 | |
| 1552 | viewLen := len(view) + 1 // adding the self package |
| 1553 | // Parse the export data. |
| 1554 | // (May modify incomplete packages in view but not create new ones.) |
| 1555 | tpkg, err := gcexportdata.Read(r, ld.Fset, view, lpkg.PkgPath) |
| 1556 | if err != nil { |
| 1557 | return fmt.Errorf("reading %s: %v", lpkg.ExportFile, err) |
| 1558 | } |
| 1559 | if _, ok := view["go.shape"]; ok { |
| 1560 | // Account for the pseudopackage "go.shape" that gets |
| 1561 | // created by generic code. |
| 1562 | viewLen++ |
| 1563 | } |
| 1564 | if viewLen != len(view) { |
| 1565 | log.Panicf("golang.org/x/tools/go/packages: unexpected new packages during load of %s", lpkg.PkgPath) |
| 1566 | } |
| 1567 | |
| 1568 | lpkg.Types = tpkg |
| 1569 | lpkg.IllTyped = false |
| 1570 | return nil |
| 1571 | } |
| 1572 | |
| 1573 | // impliedLoadMode returns loadMode with its dependencies. |
| 1574 | func impliedLoadMode(loadMode LoadMode) LoadMode { |
| 1575 | if loadMode&(NeedDeps|NeedTypes|NeedTypesInfo) != 0 { |
| 1576 | // All these things require knowing the import graph. |
| 1577 | loadMode |= NeedImports |
| 1578 | } |
| 1579 | if loadMode&NeedTypes != 0 { |
| 1580 | // Types require the GoVersion from Module. |
| 1581 | loadMode |= NeedModule |
| 1582 | } |
| 1583 | |
| 1584 | return loadMode |
| 1585 | } |
| 1586 | |
| 1587 | func usesExportData(cfg *Config) bool { |
| 1588 | return cfg.Mode&NeedExportFile != 0 || cfg.Mode&NeedTypes != 0 && cfg.Mode&NeedDeps == 0 |
| 1589 | } |
| 1590 | |
| 1591 | type unit struct{} |