@@ -41,8 +41,6 @@ import (
41
41
"golang.org/x/mod/module"
42
42
)
43
43
44
- var IgnoreImports bool // control whether we ignore imports in packages
45
-
46
44
// A Package describes a single package found in a directory.
47
45
type Package struct {
48
46
PackagePublic // visible in 'go list'
@@ -345,7 +343,7 @@ type CoverVar struct {
345
343
Var string // name of count struct
346
344
}
347
345
348
- func (p * Package ) copyBuild (pp * build.Package ) {
346
+ func (p * Package ) copyBuild (opts PackageOpts , pp * build.Package ) {
349
347
p .Internal .Build = pp
350
348
351
349
if pp .PkgTargetRoot != "" && cfg .BuildPkgdir != "" {
@@ -394,7 +392,7 @@ func (p *Package) copyBuild(pp *build.Package) {
394
392
p .TestImports = pp .TestImports
395
393
p .XTestGoFiles = pp .XTestGoFiles
396
394
p .XTestImports = pp .XTestImports
397
- if IgnoreImports {
395
+ if opts . IgnoreImports {
398
396
p .Imports = nil
399
397
p .Internal .RawImports = nil
400
398
p .TestImports = nil
@@ -601,7 +599,7 @@ func ReloadPackageNoFlags(arg string, stk *ImportStack) *Package {
601
599
})
602
600
packageDataCache .Delete (p .ImportPath )
603
601
}
604
- return LoadImport (context .TODO (), arg , base .Cwd , nil , stk , nil , 0 )
602
+ return LoadImport (context .TODO (), PackageOpts {}, arg , base .Cwd , nil , stk , nil , 0 )
605
603
}
606
604
607
605
// dirToImportPath returns the pseudo-import path we use for a package
@@ -653,11 +651,11 @@ const (
653
651
// LoadImport does not set tool flags and should only be used by
654
652
// this package, as part of a bigger load operation, and by GOPATH-based "go get".
655
653
// TODO(rsc): When GOPATH-based "go get" is removed, unexport this function.
656
- func LoadImport (ctx context.Context , path , srcDir string , parent * Package , stk * ImportStack , importPos []token.Position , mode int ) * Package {
657
- return loadImport (ctx , nil , path , srcDir , parent , stk , importPos , mode )
654
+ func LoadImport (ctx context.Context , opts PackageOpts , path , srcDir string , parent * Package , stk * ImportStack , importPos []token.Position , mode int ) * Package {
655
+ return loadImport (ctx , opts , nil , path , srcDir , parent , stk , importPos , mode )
658
656
}
659
657
660
- func loadImport (ctx context.Context , pre * preload , path , srcDir string , parent * Package , stk * ImportStack , importPos []token.Position , mode int ) * Package {
658
+ func loadImport (ctx context.Context , opts PackageOpts , pre * preload , path , srcDir string , parent * Package , stk * ImportStack , importPos []token.Position , mode int ) * Package {
661
659
if path == "" {
662
660
panic ("LoadImport called with empty package path" )
663
661
}
@@ -670,8 +668,8 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
670
668
parentIsStd = parent .Standard
671
669
}
672
670
bp , loaded , err := loadPackageData (ctx , path , parentPath , srcDir , parentRoot , parentIsStd , mode )
673
- if loaded && pre != nil && ! IgnoreImports {
674
- pre .preloadImports (ctx , bp .Imports , bp )
671
+ if loaded && pre != nil && ! opts . IgnoreImports {
672
+ pre .preloadImports (ctx , opts , bp .Imports , bp )
675
673
}
676
674
if bp == nil {
677
675
p := & Package {
@@ -710,7 +708,7 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
710
708
// Load package.
711
709
// loadPackageData may return bp != nil even if an error occurs,
712
710
// in order to return partial information.
713
- p .load (ctx , path , stk , importPos , bp , err )
711
+ p .load (ctx , opts , path , stk , importPos , bp , err )
714
712
715
713
if ! cfg .ModulesEnabled && path != cleanImport (path ) {
716
714
p .Error = & PackageError {
@@ -980,7 +978,7 @@ func newPreload() *preload {
980
978
// preloadMatches loads data for package paths matched by patterns.
981
979
// When preloadMatches returns, some packages may not be loaded yet, but
982
980
// loadPackageData and loadImport are always safe to call.
983
- func (pre * preload ) preloadMatches (ctx context.Context , matches []* search.Match ) {
981
+ func (pre * preload ) preloadMatches (ctx context.Context , opts PackageOpts , matches []* search.Match ) {
984
982
for _ , m := range matches {
985
983
for _ , pkg := range m .Pkgs {
986
984
select {
@@ -991,8 +989,8 @@ func (pre *preload) preloadMatches(ctx context.Context, matches []*search.Match)
991
989
mode := 0 // don't use vendoring or module import resolution
992
990
bp , loaded , err := loadPackageData (ctx , pkg , "" , base .Cwd , "" , false , mode )
993
991
<- pre .sema
994
- if bp != nil && loaded && err == nil && ! IgnoreImports {
995
- pre .preloadImports (ctx , bp .Imports , bp )
992
+ if bp != nil && loaded && err == nil && ! opts . IgnoreImports {
993
+ pre .preloadImports (ctx , opts , bp .Imports , bp )
996
994
}
997
995
}(pkg )
998
996
}
@@ -1003,7 +1001,7 @@ func (pre *preload) preloadMatches(ctx context.Context, matches []*search.Match)
1003
1001
// preloadImports queues a list of imports for preloading.
1004
1002
// When preloadImports returns, some packages may not be loaded yet,
1005
1003
// but loadPackageData and loadImport are always safe to call.
1006
- func (pre * preload ) preloadImports (ctx context.Context , imports []string , parent * build.Package ) {
1004
+ func (pre * preload ) preloadImports (ctx context.Context , opts PackageOpts , imports []string , parent * build.Package ) {
1007
1005
parentIsStd := parent .Goroot && parent .ImportPath != "" && search .IsStandardImportPath (parent .ImportPath )
1008
1006
for _ , path := range imports {
1009
1007
if path == "C" || path == "unsafe" {
@@ -1016,8 +1014,8 @@ func (pre *preload) preloadImports(ctx context.Context, imports []string, parent
1016
1014
go func (path string ) {
1017
1015
bp , loaded , err := loadPackageData (ctx , path , parent .ImportPath , parent .Dir , parent .Root , parentIsStd , ResolveImport )
1018
1016
<- pre .sema
1019
- if bp != nil && loaded && err == nil && ! IgnoreImports {
1020
- pre .preloadImports (ctx , bp .Imports , bp )
1017
+ if bp != nil && loaded && err == nil && ! opts . IgnoreImports {
1018
+ pre .preloadImports (ctx , opts , bp .Imports , bp )
1021
1019
}
1022
1020
}(path )
1023
1021
}
@@ -1667,8 +1665,8 @@ func (p *Package) DefaultExecName() string {
1667
1665
// load populates p using information from bp, err, which should
1668
1666
// be the result of calling build.Context.Import.
1669
1667
// stk contains the import stack, not including path itself.
1670
- func (p * Package ) load (ctx context.Context , path string , stk * ImportStack , importPos []token.Position , bp * build.Package , err error ) {
1671
- p .copyBuild (bp )
1668
+ func (p * Package ) load (ctx context.Context , opts PackageOpts , path string , stk * ImportStack , importPos []token.Position , bp * build.Package , err error ) {
1669
+ p .copyBuild (opts , bp )
1672
1670
1673
1671
// The localPrefix is the path we interpret ./ imports relative to.
1674
1672
// Synthesized main packages sometimes override this.
@@ -1887,7 +1885,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
1887
1885
if path == "C" {
1888
1886
continue
1889
1887
}
1890
- p1 := LoadImport (ctx , path , p .Dir , p , stk , p .Internal .Build .ImportPos [path ], ResolveImport )
1888
+ p1 := LoadImport (ctx , opts , path , p .Dir , p , stk , p .Internal .Build .ImportPos [path ], ResolveImport )
1891
1889
1892
1890
path = p1 .ImportPath
1893
1891
importPaths [i ] = path
@@ -2334,7 +2332,7 @@ func PackageList(roots []*Package) []*Package {
2334
2332
// TestPackageList returns the list of packages in the dag rooted at roots
2335
2333
// as visited in a depth-first post-order traversal, including the test
2336
2334
// imports of the roots. This ignores errors in test packages.
2337
- func TestPackageList (ctx context.Context , roots []* Package ) []* Package {
2335
+ func TestPackageList (ctx context.Context , opts PackageOpts , roots []* Package ) []* Package {
2338
2336
seen := map [* Package ]bool {}
2339
2337
all := []* Package {}
2340
2338
var walk func (* Package )
@@ -2350,7 +2348,7 @@ func TestPackageList(ctx context.Context, roots []*Package) []*Package {
2350
2348
}
2351
2349
walkTest := func (root * Package , path string ) {
2352
2350
var stk ImportStack
2353
- p1 := LoadImport (ctx , path , root .Dir , root , & stk , root .Internal .Build .TestImportPos [path ], ResolveImport )
2351
+ p1 := LoadImport (ctx , opts , path , root .Dir , root , & stk , root .Internal .Build .TestImportPos [path ], ResolveImport )
2354
2352
if p1 .Error == nil {
2355
2353
walk (p1 )
2356
2354
}
@@ -2373,22 +2371,26 @@ func TestPackageList(ctx context.Context, roots []*Package) []*Package {
2373
2371
// TODO(jayconrod): delete this function and set flags automatically
2374
2372
// in LoadImport instead.
2375
2373
func LoadImportWithFlags (path , srcDir string , parent * Package , stk * ImportStack , importPos []token.Position , mode int ) * Package {
2376
- p := LoadImport (context .TODO (), path , srcDir , parent , stk , importPos , mode )
2374
+ p := LoadImport (context .TODO (), PackageOpts {}, path , srcDir , parent , stk , importPos , mode )
2377
2375
setToolFlags (p )
2378
2376
return p
2379
2377
}
2380
2378
2381
- // ModResolveTests indicates whether calls to the module loader should also
2382
- // resolve test dependencies of the requested packages.
2383
- //
2384
- // If ModResolveTests is true, then the module loader needs to resolve test
2385
- // dependencies at the same time as packages; otherwise, the test dependencies
2386
- // of those packages could be missing, and resolving those missing dependencies
2387
- // could change the selected versions of modules that provide other packages.
2388
- //
2389
- // TODO(#40775): Change this from a global variable to an explicit function
2390
- // argument where needed.
2391
- var ModResolveTests bool
2379
+ // PackageOpts control the behavior of PackagesAndErrors and other package
2380
+ // loading functions.
2381
+ type PackageOpts struct {
2382
+ // IgnoreImports controls whether we ignore imports when loading packages.
2383
+ IgnoreImports bool
2384
+
2385
+ // ModResolveTests indicates whether calls to the module loader should also
2386
+ // resolve test dependencies of the requested packages.
2387
+ //
2388
+ // If ModResolveTests is true, then the module loader needs to resolve test
2389
+ // dependencies at the same time as packages; otherwise, the test dependencies
2390
+ // of those packages could be missing, and resolving those missing dependencies
2391
+ // could change the selected versions of modules that provide other packages.
2392
+ ModResolveTests bool
2393
+ }
2392
2394
2393
2395
// PackagesAndErrors returns the packages named by the command line arguments
2394
2396
// 'patterns'. If a named package cannot be loaded, PackagesAndErrors returns
@@ -2398,7 +2400,7 @@ var ModResolveTests bool
2398
2400
//
2399
2401
// To obtain a flat list of packages, use PackageList.
2400
2402
// To report errors loading packages, use ReportPackageErrors.
2401
- func PackagesAndErrors (ctx context.Context , patterns []string ) []* Package {
2403
+ func PackagesAndErrors (ctx context.Context , opts PackageOpts , patterns []string ) []* Package {
2402
2404
ctx , span := trace .StartSpan (ctx , "load.PackagesAndErrors" )
2403
2405
defer span .Done ()
2404
2406
@@ -2410,19 +2412,19 @@ func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
2410
2412
// We need to test whether the path is an actual Go file and not a
2411
2413
// package path or pattern ending in '.go' (see golang.org/issue/34653).
2412
2414
if fi , err := fsys .Stat (p ); err == nil && ! fi .IsDir () {
2413
- return []* Package {GoFilesPackage (ctx , patterns )}
2415
+ return []* Package {GoFilesPackage (ctx , opts , patterns )}
2414
2416
}
2415
2417
}
2416
2418
}
2417
2419
2418
2420
var matches []* search.Match
2419
2421
if modload .Init (); cfg .ModulesEnabled {
2420
- loadOpts := modload.PackageOpts {
2422
+ modOpts := modload.PackageOpts {
2421
2423
ResolveMissingImports : true ,
2422
- LoadTests : ModResolveTests ,
2424
+ LoadTests : opts . ModResolveTests ,
2423
2425
SilenceErrors : true ,
2424
2426
}
2425
- matches , _ = modload .LoadPackages (ctx , loadOpts , patterns ... )
2427
+ matches , _ = modload .LoadPackages (ctx , modOpts , patterns ... )
2426
2428
} else {
2427
2429
matches = search .ImportPaths (patterns )
2428
2430
}
@@ -2435,14 +2437,14 @@ func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
2435
2437
2436
2438
pre := newPreload ()
2437
2439
defer pre .flush ()
2438
- pre .preloadMatches (ctx , matches )
2440
+ pre .preloadMatches (ctx , opts , matches )
2439
2441
2440
2442
for _ , m := range matches {
2441
2443
for _ , pkg := range m .Pkgs {
2442
2444
if pkg == "" {
2443
2445
panic (fmt .Sprintf ("ImportPaths returned empty package for pattern %s" , m .Pattern ()))
2444
2446
}
2445
- p := loadImport (ctx , pre , pkg , base .Cwd , nil , & stk , nil , 0 )
2447
+ p := loadImport (ctx , opts , pre , pkg , base .Cwd , nil , & stk , nil , 0 )
2446
2448
p .Match = append (p .Match , m .Pattern ())
2447
2449
p .Internal .CmdlinePkg = true
2448
2450
if m .IsLiteral () {
@@ -2538,7 +2540,7 @@ func setToolFlags(pkgs ...*Package) {
2538
2540
// GoFilesPackage creates a package for building a collection of Go files
2539
2541
// (typically named on the command line). The target is named p.a for
2540
2542
// package p or named after the first Go file for package main.
2541
- func GoFilesPackage (ctx context.Context , gofiles []string ) * Package {
2543
+ func GoFilesPackage (ctx context.Context , opts PackageOpts , gofiles []string ) * Package {
2542
2544
modload .Init ()
2543
2545
2544
2546
for _ , f := range gofiles {
@@ -2602,7 +2604,7 @@ func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
2602
2604
pkg := new (Package )
2603
2605
pkg .Internal .Local = true
2604
2606
pkg .Internal .CmdlineFiles = true
2605
- pkg .load (ctx , "command-line-arguments" , & stk , nil , bp , err )
2607
+ pkg .load (ctx , opts , "command-line-arguments" , & stk , nil , bp , err )
2606
2608
pkg .Internal .LocalPrefix = dirToImportPath (dir )
2607
2609
pkg .ImportPath = "command-line-arguments"
2608
2610
pkg .Target = ""
0 commit comments