mirror of
https://github.com/golang/go
synced 2024-11-26 08:27:56 -07:00
cmd/go/internal/load: convert two global flags to an options struct
PackageOpts is a new struct type accepted by package loading functions. It initially has two fields: IgnoreImports, and ModResolveTests. Previously, these were global variables set by clients. We'll add more to this in the future. For #40775 Change-Id: I6956e56502de836d3815ce788bdf16fc5f3e5338 Reviewed-on: https://go-review.googlesource.com/c/go/+/310669 Trust: Jay Conrod <jayconrod@google.com> Run-TryBot: Jay Conrod <jayconrod@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Bryan C. Mills <bcmills@google.com>
This commit is contained in:
parent
cde92846e2
commit
dc76c47565
@ -117,7 +117,7 @@ func runClean(ctx context.Context, cmd *base.Command, args []string) {
|
||||
}
|
||||
|
||||
if cleanPkg {
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, args) {
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, load.PackageOpts{}, args) {
|
||||
clean(pkg)
|
||||
}
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ See also: go fmt, go vet.
|
||||
}
|
||||
|
||||
func runFix(ctx context.Context, cmd *base.Command, args []string) {
|
||||
pkgs := load.PackagesAndErrors(ctx, args)
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args)
|
||||
w := 0
|
||||
for _, pkg := range pkgs {
|
||||
if pkg.Error != nil {
|
||||
|
@ -65,7 +65,7 @@ func runFmt(ctx context.Context, cmd *base.Command, args []string) {
|
||||
}
|
||||
}()
|
||||
}
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, args) {
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, load.PackageOpts{}, args) {
|
||||
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
|
||||
if !printed {
|
||||
fmt.Fprintf(os.Stderr, "go: not formatting packages in dependency modules\n")
|
||||
|
@ -161,8 +161,6 @@ func init() {
|
||||
}
|
||||
|
||||
func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
|
||||
load.IgnoreImports = true
|
||||
|
||||
if generateRunFlag != "" {
|
||||
var err error
|
||||
generateRunRE, err = regexp.Compile(generateRunFlag)
|
||||
@ -175,7 +173,8 @@ func runGenerate(ctx context.Context, cmd *base.Command, args []string) {
|
||||
|
||||
// Even if the arguments are .go files, this loop suffices.
|
||||
printed := false
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, args) {
|
||||
pkgOpts := load.PackageOpts{IgnoreImports: true}
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, pkgOpts, args) {
|
||||
if modload.Enabled() && pkg.Module != nil && !pkg.Module.Main {
|
||||
if !printed {
|
||||
fmt.Fprintf(os.Stderr, "go: not generating in packages in dependency modules\n")
|
||||
|
@ -173,7 +173,7 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
// everything.
|
||||
load.ClearPackageCache()
|
||||
|
||||
pkgs := load.PackagesAndErrors(ctx, args)
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
|
||||
// Phase 3. Install.
|
||||
@ -248,9 +248,9 @@ func download(arg string, parent *load.Package, stk *load.ImportStack, mode int)
|
||||
load1 := func(path string, mode int) *load.Package {
|
||||
if parent == nil {
|
||||
mode := 0 // don't do module or vendor resolution
|
||||
return load.LoadImport(context.TODO(), path, base.Cwd, nil, stk, nil, mode)
|
||||
return load.LoadImport(context.TODO(), load.PackageOpts{}, path, base.Cwd, nil, stk, nil, mode)
|
||||
}
|
||||
return load.LoadImport(context.TODO(), path, parent.Dir, parent, stk, nil, mode|load.ResolveModule)
|
||||
return load.LoadImport(context.TODO(), load.PackageOpts{}, path, parent.Dir, parent, stk, nil, mode|load.ResolveModule)
|
||||
}
|
||||
|
||||
p := load1(arg, mode)
|
||||
|
@ -339,7 +339,6 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
|
||||
base.Fatalf("go list -f cannot be used with -json")
|
||||
}
|
||||
|
||||
load.ModResolveTests = *listTest
|
||||
work.BuildInit()
|
||||
out := newTrackingWriter(os.Stdout)
|
||||
defer out.w.Flush()
|
||||
@ -498,8 +497,11 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
|
||||
base.Fatalf("go list -test cannot be used with -find")
|
||||
}
|
||||
|
||||
load.IgnoreImports = *listFind
|
||||
pkgs := load.PackagesAndErrors(ctx, args)
|
||||
pkgOpts := load.PackageOpts{
|
||||
IgnoreImports: *listFind,
|
||||
ModResolveTests: *listTest,
|
||||
}
|
||||
pkgs := load.PackagesAndErrors(ctx, pkgOpts, args)
|
||||
if !*listE {
|
||||
w := 0
|
||||
for _, pkg := range pkgs {
|
||||
@ -536,9 +538,9 @@ func runList(ctx context.Context, cmd *base.Command, args []string) {
|
||||
var pmain, ptest, pxtest *load.Package
|
||||
var err error
|
||||
if *listE {
|
||||
pmain, ptest, pxtest = load.TestPackagesAndErrors(ctx, p, nil)
|
||||
pmain, ptest, pxtest = load.TestPackagesAndErrors(ctx, pkgOpts, p, nil)
|
||||
} else {
|
||||
pmain, ptest, pxtest, err = load.TestPackagesFor(ctx, p, nil)
|
||||
pmain, ptest, pxtest, err = load.TestPackagesFor(ctx, pkgOpts, p, nil)
|
||||
if err != nil {
|
||||
base.Errorf("can't load test package: %s", err)
|
||||
}
|
||||
|
@ -41,8 +41,6 @@ import (
|
||||
"golang.org/x/mod/module"
|
||||
)
|
||||
|
||||
var IgnoreImports bool // control whether we ignore imports in packages
|
||||
|
||||
// A Package describes a single package found in a directory.
|
||||
type Package struct {
|
||||
PackagePublic // visible in 'go list'
|
||||
@ -345,7 +343,7 @@ type CoverVar struct {
|
||||
Var string // name of count struct
|
||||
}
|
||||
|
||||
func (p *Package) copyBuild(pp *build.Package) {
|
||||
func (p *Package) copyBuild(opts PackageOpts, pp *build.Package) {
|
||||
p.Internal.Build = pp
|
||||
|
||||
if pp.PkgTargetRoot != "" && cfg.BuildPkgdir != "" {
|
||||
@ -394,7 +392,7 @@ func (p *Package) copyBuild(pp *build.Package) {
|
||||
p.TestImports = pp.TestImports
|
||||
p.XTestGoFiles = pp.XTestGoFiles
|
||||
p.XTestImports = pp.XTestImports
|
||||
if IgnoreImports {
|
||||
if opts.IgnoreImports {
|
||||
p.Imports = nil
|
||||
p.Internal.RawImports = nil
|
||||
p.TestImports = nil
|
||||
@ -601,7 +599,7 @@ func ReloadPackageNoFlags(arg string, stk *ImportStack) *Package {
|
||||
})
|
||||
packageDataCache.Delete(p.ImportPath)
|
||||
}
|
||||
return LoadImport(context.TODO(), arg, base.Cwd, nil, stk, nil, 0)
|
||||
return LoadImport(context.TODO(), PackageOpts{}, arg, base.Cwd, nil, stk, nil, 0)
|
||||
}
|
||||
|
||||
// dirToImportPath returns the pseudo-import path we use for a package
|
||||
@ -653,11 +651,11 @@ const (
|
||||
// LoadImport does not set tool flags and should only be used by
|
||||
// this package, as part of a bigger load operation, and by GOPATH-based "go get".
|
||||
// TODO(rsc): When GOPATH-based "go get" is removed, unexport this function.
|
||||
func LoadImport(ctx context.Context, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
|
||||
return loadImport(ctx, nil, path, srcDir, parent, stk, importPos, mode)
|
||||
func LoadImport(ctx context.Context, opts PackageOpts, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
|
||||
return loadImport(ctx, opts, nil, path, srcDir, parent, stk, importPos, mode)
|
||||
}
|
||||
|
||||
func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
|
||||
func loadImport(ctx context.Context, opts PackageOpts, pre *preload, path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
|
||||
if path == "" {
|
||||
panic("LoadImport called with empty package path")
|
||||
}
|
||||
@ -670,8 +668,8 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
|
||||
parentIsStd = parent.Standard
|
||||
}
|
||||
bp, loaded, err := loadPackageData(ctx, path, parentPath, srcDir, parentRoot, parentIsStd, mode)
|
||||
if loaded && pre != nil && !IgnoreImports {
|
||||
pre.preloadImports(ctx, bp.Imports, bp)
|
||||
if loaded && pre != nil && !opts.IgnoreImports {
|
||||
pre.preloadImports(ctx, opts, bp.Imports, bp)
|
||||
}
|
||||
if bp == nil {
|
||||
p := &Package{
|
||||
@ -710,7 +708,7 @@ func loadImport(ctx context.Context, pre *preload, path, srcDir string, parent *
|
||||
// Load package.
|
||||
// loadPackageData may return bp != nil even if an error occurs,
|
||||
// in order to return partial information.
|
||||
p.load(ctx, path, stk, importPos, bp, err)
|
||||
p.load(ctx, opts, path, stk, importPos, bp, err)
|
||||
|
||||
if !cfg.ModulesEnabled && path != cleanImport(path) {
|
||||
p.Error = &PackageError{
|
||||
@ -980,7 +978,7 @@ func newPreload() *preload {
|
||||
// preloadMatches loads data for package paths matched by patterns.
|
||||
// When preloadMatches returns, some packages may not be loaded yet, but
|
||||
// loadPackageData and loadImport are always safe to call.
|
||||
func (pre *preload) preloadMatches(ctx context.Context, matches []*search.Match) {
|
||||
func (pre *preload) preloadMatches(ctx context.Context, opts PackageOpts, matches []*search.Match) {
|
||||
for _, m := range matches {
|
||||
for _, pkg := range m.Pkgs {
|
||||
select {
|
||||
@ -991,8 +989,8 @@ func (pre *preload) preloadMatches(ctx context.Context, matches []*search.Match)
|
||||
mode := 0 // don't use vendoring or module import resolution
|
||||
bp, loaded, err := loadPackageData(ctx, pkg, "", base.Cwd, "", false, mode)
|
||||
<-pre.sema
|
||||
if bp != nil && loaded && err == nil && !IgnoreImports {
|
||||
pre.preloadImports(ctx, bp.Imports, bp)
|
||||
if bp != nil && loaded && err == nil && !opts.IgnoreImports {
|
||||
pre.preloadImports(ctx, opts, bp.Imports, bp)
|
||||
}
|
||||
}(pkg)
|
||||
}
|
||||
@ -1003,7 +1001,7 @@ func (pre *preload) preloadMatches(ctx context.Context, matches []*search.Match)
|
||||
// preloadImports queues a list of imports for preloading.
|
||||
// When preloadImports returns, some packages may not be loaded yet,
|
||||
// but loadPackageData and loadImport are always safe to call.
|
||||
func (pre *preload) preloadImports(ctx context.Context, imports []string, parent *build.Package) {
|
||||
func (pre *preload) preloadImports(ctx context.Context, opts PackageOpts, imports []string, parent *build.Package) {
|
||||
parentIsStd := parent.Goroot && parent.ImportPath != "" && search.IsStandardImportPath(parent.ImportPath)
|
||||
for _, path := range imports {
|
||||
if path == "C" || path == "unsafe" {
|
||||
@ -1016,8 +1014,8 @@ func (pre *preload) preloadImports(ctx context.Context, imports []string, parent
|
||||
go func(path string) {
|
||||
bp, loaded, err := loadPackageData(ctx, path, parent.ImportPath, parent.Dir, parent.Root, parentIsStd, ResolveImport)
|
||||
<-pre.sema
|
||||
if bp != nil && loaded && err == nil && !IgnoreImports {
|
||||
pre.preloadImports(ctx, bp.Imports, bp)
|
||||
if bp != nil && loaded && err == nil && !opts.IgnoreImports {
|
||||
pre.preloadImports(ctx, opts, bp.Imports, bp)
|
||||
}
|
||||
}(path)
|
||||
}
|
||||
@ -1667,8 +1665,8 @@ func (p *Package) DefaultExecName() string {
|
||||
// load populates p using information from bp, err, which should
|
||||
// be the result of calling build.Context.Import.
|
||||
// stk contains the import stack, not including path itself.
|
||||
func (p *Package) load(ctx context.Context, path string, stk *ImportStack, importPos []token.Position, bp *build.Package, err error) {
|
||||
p.copyBuild(bp)
|
||||
func (p *Package) load(ctx context.Context, opts PackageOpts, path string, stk *ImportStack, importPos []token.Position, bp *build.Package, err error) {
|
||||
p.copyBuild(opts, bp)
|
||||
|
||||
// The localPrefix is the path we interpret ./ imports relative to.
|
||||
// Synthesized main packages sometimes override this.
|
||||
@ -1887,7 +1885,7 @@ func (p *Package) load(ctx context.Context, path string, stk *ImportStack, impor
|
||||
if path == "C" {
|
||||
continue
|
||||
}
|
||||
p1 := LoadImport(ctx, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
|
||||
p1 := LoadImport(ctx, opts, path, p.Dir, p, stk, p.Internal.Build.ImportPos[path], ResolveImport)
|
||||
|
||||
path = p1.ImportPath
|
||||
importPaths[i] = path
|
||||
@ -2334,7 +2332,7 @@ func PackageList(roots []*Package) []*Package {
|
||||
// TestPackageList returns the list of packages in the dag rooted at roots
|
||||
// as visited in a depth-first post-order traversal, including the test
|
||||
// imports of the roots. This ignores errors in test packages.
|
||||
func TestPackageList(ctx context.Context, roots []*Package) []*Package {
|
||||
func TestPackageList(ctx context.Context, opts PackageOpts, roots []*Package) []*Package {
|
||||
seen := map[*Package]bool{}
|
||||
all := []*Package{}
|
||||
var walk func(*Package)
|
||||
@ -2350,7 +2348,7 @@ func TestPackageList(ctx context.Context, roots []*Package) []*Package {
|
||||
}
|
||||
walkTest := func(root *Package, path string) {
|
||||
var stk ImportStack
|
||||
p1 := LoadImport(ctx, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
|
||||
p1 := LoadImport(ctx, opts, path, root.Dir, root, &stk, root.Internal.Build.TestImportPos[path], ResolveImport)
|
||||
if p1.Error == nil {
|
||||
walk(p1)
|
||||
}
|
||||
@ -2373,22 +2371,26 @@ func TestPackageList(ctx context.Context, roots []*Package) []*Package {
|
||||
// TODO(jayconrod): delete this function and set flags automatically
|
||||
// in LoadImport instead.
|
||||
func LoadImportWithFlags(path, srcDir string, parent *Package, stk *ImportStack, importPos []token.Position, mode int) *Package {
|
||||
p := LoadImport(context.TODO(), path, srcDir, parent, stk, importPos, mode)
|
||||
p := LoadImport(context.TODO(), PackageOpts{}, path, srcDir, parent, stk, importPos, mode)
|
||||
setToolFlags(p)
|
||||
return p
|
||||
}
|
||||
|
||||
// ModResolveTests indicates whether calls to the module loader should also
|
||||
// resolve test dependencies of the requested packages.
|
||||
//
|
||||
// If ModResolveTests is true, then the module loader needs to resolve test
|
||||
// dependencies at the same time as packages; otherwise, the test dependencies
|
||||
// of those packages could be missing, and resolving those missing dependencies
|
||||
// could change the selected versions of modules that provide other packages.
|
||||
//
|
||||
// TODO(#40775): Change this from a global variable to an explicit function
|
||||
// argument where needed.
|
||||
var ModResolveTests bool
|
||||
// PackageOpts control the behavior of PackagesAndErrors and other package
|
||||
// loading functions.
|
||||
type PackageOpts struct {
|
||||
// IgnoreImports controls whether we ignore imports when loading packages.
|
||||
IgnoreImports bool
|
||||
|
||||
// ModResolveTests indicates whether calls to the module loader should also
|
||||
// resolve test dependencies of the requested packages.
|
||||
//
|
||||
// If ModResolveTests is true, then the module loader needs to resolve test
|
||||
// dependencies at the same time as packages; otherwise, the test dependencies
|
||||
// of those packages could be missing, and resolving those missing dependencies
|
||||
// could change the selected versions of modules that provide other packages.
|
||||
ModResolveTests bool
|
||||
}
|
||||
|
||||
// PackagesAndErrors returns the packages named by the command line arguments
|
||||
// 'patterns'. If a named package cannot be loaded, PackagesAndErrors returns
|
||||
@ -2398,7 +2400,7 @@ var ModResolveTests bool
|
||||
//
|
||||
// To obtain a flat list of packages, use PackageList.
|
||||
// To report errors loading packages, use ReportPackageErrors.
|
||||
func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
|
||||
func PackagesAndErrors(ctx context.Context, opts PackageOpts, patterns []string) []*Package {
|
||||
ctx, span := trace.StartSpan(ctx, "load.PackagesAndErrors")
|
||||
defer span.Done()
|
||||
|
||||
@ -2410,19 +2412,19 @@ func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
|
||||
// We need to test whether the path is an actual Go file and not a
|
||||
// package path or pattern ending in '.go' (see golang.org/issue/34653).
|
||||
if fi, err := fsys.Stat(p); err == nil && !fi.IsDir() {
|
||||
return []*Package{GoFilesPackage(ctx, patterns)}
|
||||
return []*Package{GoFilesPackage(ctx, opts, patterns)}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var matches []*search.Match
|
||||
if modload.Init(); cfg.ModulesEnabled {
|
||||
loadOpts := modload.PackageOpts{
|
||||
modOpts := modload.PackageOpts{
|
||||
ResolveMissingImports: true,
|
||||
LoadTests: ModResolveTests,
|
||||
LoadTests: opts.ModResolveTests,
|
||||
SilenceErrors: true,
|
||||
}
|
||||
matches, _ = modload.LoadPackages(ctx, loadOpts, patterns...)
|
||||
matches, _ = modload.LoadPackages(ctx, modOpts, patterns...)
|
||||
} else {
|
||||
matches = search.ImportPaths(patterns)
|
||||
}
|
||||
@ -2435,14 +2437,14 @@ func PackagesAndErrors(ctx context.Context, patterns []string) []*Package {
|
||||
|
||||
pre := newPreload()
|
||||
defer pre.flush()
|
||||
pre.preloadMatches(ctx, matches)
|
||||
pre.preloadMatches(ctx, opts, matches)
|
||||
|
||||
for _, m := range matches {
|
||||
for _, pkg := range m.Pkgs {
|
||||
if pkg == "" {
|
||||
panic(fmt.Sprintf("ImportPaths returned empty package for pattern %s", m.Pattern()))
|
||||
}
|
||||
p := loadImport(ctx, pre, pkg, base.Cwd, nil, &stk, nil, 0)
|
||||
p := loadImport(ctx, opts, pre, pkg, base.Cwd, nil, &stk, nil, 0)
|
||||
p.Match = append(p.Match, m.Pattern())
|
||||
p.Internal.CmdlinePkg = true
|
||||
if m.IsLiteral() {
|
||||
@ -2538,7 +2540,7 @@ func setToolFlags(pkgs ...*Package) {
|
||||
// GoFilesPackage creates a package for building a collection of Go files
|
||||
// (typically named on the command line). The target is named p.a for
|
||||
// package p or named after the first Go file for package main.
|
||||
func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
|
||||
func GoFilesPackage(ctx context.Context, opts PackageOpts, gofiles []string) *Package {
|
||||
modload.Init()
|
||||
|
||||
for _, f := range gofiles {
|
||||
@ -2602,7 +2604,7 @@ func GoFilesPackage(ctx context.Context, gofiles []string) *Package {
|
||||
pkg := new(Package)
|
||||
pkg.Internal.Local = true
|
||||
pkg.Internal.CmdlineFiles = true
|
||||
pkg.load(ctx, "command-line-arguments", &stk, nil, bp, err)
|
||||
pkg.load(ctx, opts, "command-line-arguments", &stk, nil, bp, err)
|
||||
pkg.Internal.LocalPrefix = dirToImportPath(dir)
|
||||
pkg.ImportPath = "command-line-arguments"
|
||||
pkg.Target = ""
|
||||
|
@ -46,8 +46,8 @@ type TestCover struct {
|
||||
// TestPackagesFor is like TestPackagesAndErrors but it returns
|
||||
// an error if the test packages or their dependencies have errors.
|
||||
// Only test packages without errors are returned.
|
||||
func TestPackagesFor(ctx context.Context, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package, err error) {
|
||||
pmain, ptest, pxtest = TestPackagesAndErrors(ctx, p, cover)
|
||||
func TestPackagesFor(ctx context.Context, opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package, err error) {
|
||||
pmain, ptest, pxtest = TestPackagesAndErrors(ctx, opts, p, cover)
|
||||
for _, p1 := range []*Package{ptest, pxtest, pmain} {
|
||||
if p1 == nil {
|
||||
// pxtest may be nil
|
||||
@ -93,7 +93,7 @@ func TestPackagesFor(ctx context.Context, p *Package, cover *TestCover) (pmain,
|
||||
//
|
||||
// The caller is expected to have checked that len(p.TestGoFiles)+len(p.XTestGoFiles) > 0,
|
||||
// or else there's no point in any of this.
|
||||
func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package) {
|
||||
func TestPackagesAndErrors(ctx context.Context, opts PackageOpts, p *Package, cover *TestCover) (pmain, ptest, pxtest *Package) {
|
||||
ctx, span := trace.StartSpan(ctx, "load.TestPackagesAndErrors")
|
||||
defer span.Done()
|
||||
|
||||
@ -101,7 +101,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
||||
defer pre.flush()
|
||||
allImports := append([]string{}, p.TestImports...)
|
||||
allImports = append(allImports, p.XTestImports...)
|
||||
pre.preloadImports(ctx, allImports, p.Internal.Build)
|
||||
pre.preloadImports(ctx, opts, allImports, p.Internal.Build)
|
||||
|
||||
var ptestErr, pxtestErr *PackageError
|
||||
var imports, ximports []*Package
|
||||
@ -110,7 +110,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
||||
stk.Push(p.ImportPath + " (test)")
|
||||
rawTestImports := str.StringList(p.TestImports)
|
||||
for i, path := range p.TestImports {
|
||||
p1 := loadImport(ctx, pre, path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
|
||||
p1 := loadImport(ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.TestImportPos[path], ResolveImport)
|
||||
if str.Contains(p1.Deps, p.ImportPath) || p1.ImportPath == p.ImportPath {
|
||||
// Same error that loadPackage returns (via reusePackage) in pkg.go.
|
||||
// Can't change that code, because that code is only for loading the
|
||||
@ -140,7 +140,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
||||
pxtestNeedsPtest := false
|
||||
rawXTestImports := str.StringList(p.XTestImports)
|
||||
for i, path := range p.XTestImports {
|
||||
p1 := loadImport(ctx, pre, path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
|
||||
p1 := loadImport(ctx, opts, pre, path, p.Dir, p, &stk, p.Internal.Build.XTestImportPos[path], ResolveImport)
|
||||
if p1.ImportPath == p.ImportPath {
|
||||
pxtestNeedsPtest = true
|
||||
} else {
|
||||
@ -281,7 +281,7 @@ func TestPackagesAndErrors(ctx context.Context, p *Package, cover *TestCover) (p
|
||||
if dep == ptest.ImportPath {
|
||||
pmain.Internal.Imports = append(pmain.Internal.Imports, ptest)
|
||||
} else {
|
||||
p1 := loadImport(ctx, pre, dep, "", nil, &stk, nil, 0)
|
||||
p1 := loadImport(ctx, opts, pre, dep, "", nil, &stk, nil, 0)
|
||||
pmain.Internal.Imports = append(pmain.Internal.Imports, p1)
|
||||
}
|
||||
}
|
||||
|
@ -276,7 +276,6 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
if *getInsecure {
|
||||
base.Fatalf("go get: -insecure flag is no longer supported; use GOINSECURE instead")
|
||||
}
|
||||
load.ModResolveTests = *getT
|
||||
|
||||
// Do not allow any updating of go.mod until we've applied
|
||||
// all the requested changes and checked that the result matches
|
||||
@ -368,8 +367,9 @@ func runGet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
if !*getD && len(pkgPatterns) > 0 {
|
||||
work.BuildInit()
|
||||
|
||||
pkgOpts := load.PackageOpts{ModResolveTests: *getT}
|
||||
var pkgs []*load.Package
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, pkgPatterns) {
|
||||
for _, pkg := range load.PackagesAndErrors(ctx, pkgOpts, pkgPatterns) {
|
||||
if pkg.Error != nil {
|
||||
var noGo *load.NoGoError
|
||||
if errors.As(pkg.Error.Err, &noGo) {
|
||||
|
@ -77,9 +77,9 @@ func runRun(ctx context.Context, cmd *base.Command, args []string) {
|
||||
base.Fatalf("go run: cannot run *_test.go files (%s)", file)
|
||||
}
|
||||
}
|
||||
p = load.GoFilesPackage(ctx, files)
|
||||
p = load.GoFilesPackage(ctx, load.PackageOpts{}, files)
|
||||
} else if len(args) > 0 && !strings.HasPrefix(args[0], "-") {
|
||||
pkgs := load.PackagesAndErrors(ctx, args[:1])
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args[:1])
|
||||
if len(pkgs) == 0 {
|
||||
base.Fatalf("go run: no packages loaded from %s", args[0])
|
||||
}
|
||||
|
@ -569,8 +569,6 @@ var defaultVetFlags = []string{
|
||||
}
|
||||
|
||||
func runTest(ctx context.Context, cmd *base.Command, args []string) {
|
||||
load.ModResolveTests = true
|
||||
|
||||
pkgArgs, testArgs = testFlags(args)
|
||||
|
||||
if cfg.DebugTrace != "" {
|
||||
@ -596,7 +594,8 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
|
||||
work.VetFlags = testVet.flags
|
||||
work.VetExplicit = testVet.explicit
|
||||
|
||||
pkgs = load.PackagesAndErrors(ctx, pkgArgs)
|
||||
pkgOpts := load.PackageOpts{ModResolveTests: true}
|
||||
pkgs = load.PackagesAndErrors(ctx, pkgOpts, pkgArgs)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
if len(pkgs) == 0 {
|
||||
base.Fatalf("no packages to test")
|
||||
@ -680,7 +679,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
|
||||
sort.Strings(all)
|
||||
|
||||
a := &work.Action{Mode: "go test -i"}
|
||||
pkgs := load.PackagesAndErrors(ctx, all)
|
||||
pkgs := load.PackagesAndErrors(ctx, pkgOpts, all)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
for _, p := range pkgs {
|
||||
if cfg.BuildToolchainName == "gccgo" && p.Standard {
|
||||
@ -707,7 +706,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
|
||||
}
|
||||
|
||||
// Select for coverage all dependencies matching the testCoverPaths patterns.
|
||||
for _, p := range load.TestPackageList(ctx, pkgs) {
|
||||
for _, p := range load.TestPackageList(ctx, pkgOpts, pkgs) {
|
||||
haveMatch := false
|
||||
for i := range testCoverPaths {
|
||||
if match[i](p) {
|
||||
@ -775,7 +774,7 @@ func runTest(ctx context.Context, cmd *base.Command, args []string) {
|
||||
ensureImport(p, "sync/atomic")
|
||||
}
|
||||
|
||||
buildTest, runTest, printTest, err := builderTest(&b, ctx, p)
|
||||
buildTest, runTest, printTest, err := builderTest(&b, ctx, pkgOpts, p)
|
||||
if err != nil {
|
||||
str := err.Error()
|
||||
str = strings.TrimPrefix(str, "\n")
|
||||
@ -842,7 +841,7 @@ var windowsBadWords = []string{
|
||||
"update",
|
||||
}
|
||||
|
||||
func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
|
||||
func builderTest(b *work.Builder, ctx context.Context, pkgOpts load.PackageOpts, p *load.Package) (buildAction, runAction, printAction *work.Action, err error) {
|
||||
if len(p.TestGoFiles)+len(p.XTestGoFiles) == 0 {
|
||||
build := b.CompileAction(work.ModeBuild, work.ModeBuild, p)
|
||||
run := &work.Action{Mode: "test run", Package: p, Deps: []*work.Action{build}}
|
||||
@ -865,7 +864,7 @@ func builderTest(b *work.Builder, ctx context.Context, p *load.Package) (buildAc
|
||||
DeclVars: declareCoverVars,
|
||||
}
|
||||
}
|
||||
pmain, ptest, pxtest, err := load.TestPackagesFor(ctx, p, cover)
|
||||
pmain, ptest, pxtest, err := load.TestPackagesFor(ctx, pkgOpts, p, cover)
|
||||
if err != nil {
|
||||
return nil, nil, nil, err
|
||||
}
|
||||
|
@ -53,8 +53,6 @@ See also: go fmt, go fix.
|
||||
}
|
||||
|
||||
func runVet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
load.ModResolveTests = true
|
||||
|
||||
vetFlags, pkgArgs := vetFlags(args)
|
||||
|
||||
if cfg.DebugTrace != "" {
|
||||
@ -87,7 +85,8 @@ func runVet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
}
|
||||
}
|
||||
|
||||
pkgs := load.PackagesAndErrors(ctx, pkgArgs)
|
||||
pkgOpts := load.PackageOpts{ModResolveTests: true}
|
||||
pkgs := load.PackagesAndErrors(ctx, pkgOpts, pkgArgs)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
if len(pkgs) == 0 {
|
||||
base.Fatalf("no packages to vet")
|
||||
@ -98,7 +97,7 @@ func runVet(ctx context.Context, cmd *base.Command, args []string) {
|
||||
|
||||
root := &work.Action{Mode: "go vet"}
|
||||
for _, p := range pkgs {
|
||||
_, ptest, pxtest, err := load.TestPackagesFor(ctx, p, nil)
|
||||
_, ptest, pxtest, err := load.TestPackagesFor(ctx, pkgOpts, p, nil)
|
||||
if err != nil {
|
||||
base.Errorf("%v", err)
|
||||
continue
|
||||
|
@ -372,7 +372,7 @@ func runBuild(ctx context.Context, cmd *base.Command, args []string) {
|
||||
var b Builder
|
||||
b.Init()
|
||||
|
||||
pkgs := load.PackagesAndErrors(ctx, args)
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
|
||||
explicitO := len(cfg.BuildO) > 0
|
||||
@ -592,7 +592,7 @@ func runInstall(ctx context.Context, cmd *base.Command, args []string) {
|
||||
}
|
||||
|
||||
BuildInit()
|
||||
pkgs := load.PackagesAndErrors(ctx, args)
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, args)
|
||||
if cfg.ModulesEnabled && !modload.HasModRoot() {
|
||||
haveErrors := false
|
||||
allMissingErrors := true
|
||||
@ -857,7 +857,7 @@ func installOutsideModule(ctx context.Context, args []string) {
|
||||
|
||||
// TODO(golang.org/issue/40276): don't report errors loading non-main packages
|
||||
// matched by a pattern.
|
||||
pkgs := load.PackagesAndErrors(ctx, patterns)
|
||||
pkgs := load.PackagesAndErrors(ctx, load.PackageOpts{}, patterns)
|
||||
load.CheckPackageErrors(pkgs)
|
||||
mainPkgs := make([]*load.Package, 0, len(pkgs))
|
||||
mainCount := make([]int, len(patterns))
|
||||
|
@ -3105,7 +3105,7 @@ func (b *Builder) swigDoIntSize(objdir string) (intsize string, err error) {
|
||||
}
|
||||
srcs := []string{src}
|
||||
|
||||
p := load.GoFilesPackage(context.TODO(), srcs)
|
||||
p := load.GoFilesPackage(context.TODO(), load.PackageOpts{}, srcs)
|
||||
|
||||
if _, _, e := BuildToolchain.gc(b, &Action{Mode: "swigDoIntSize", Package: p, Objdir: objdir}, "", nil, nil, "", false, srcs); e != nil {
|
||||
return "32", nil
|
||||
|
Loading…
Reference in New Issue
Block a user