github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/modload/load.go (about)

     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 modload
     6  
     7  import (
     8  	"github.com/shogo82148/std/context"
     9  
    10  	"github.com/shogo82148/std/cmd/go/internal/gover"
    11  	"github.com/shogo82148/std/cmd/go/internal/search"
    12  
    13  	"golang.org/x/mod/module"
    14  )
    15  
    16  // PackageOpts control the behavior of the LoadPackages function.
    17  type PackageOpts struct {
    18  	// TidyGoVersion is the Go version to which the go.mod file should be updated
    19  	// after packages have been loaded.
    20  	//
    21  	// An empty TidyGoVersion means to use the Go version already specified in the
    22  	// main module's go.mod file, or the latest Go version if there is no main
    23  	// module.
    24  	TidyGoVersion string
    25  
    26  	// Tags are the build tags in effect (as interpreted by the
    27  	// cmd/go/internal/imports package).
    28  	// If nil, treated as equivalent to imports.Tags().
    29  	Tags map[string]bool
    30  
    31  	// Tidy, if true, requests that the build list and go.sum file be reduced to
    32  	// the minimal dependencies needed to reproducibly reload the requested
    33  	// packages.
    34  	Tidy bool
    35  
    36  	// TidyCompatibleVersion is the oldest Go version that must be able to
    37  	// reproducibly reload the requested packages.
    38  	//
    39  	// If empty, the compatible version is the Go version immediately prior to the
    40  	// 'go' version listed in the go.mod file.
    41  	TidyCompatibleVersion string
    42  
    43  	// VendorModulesInGOROOTSrc indicates that if we are within a module in
    44  	// GOROOT/src, packages in the module's vendor directory should be resolved as
    45  	// actual module dependencies (instead of standard-library packages).
    46  	VendorModulesInGOROOTSrc bool
    47  
    48  	// ResolveMissingImports indicates that we should attempt to add module
    49  	// dependencies as needed to resolve imports of packages that are not found.
    50  	//
    51  	// For commands that support the -mod flag, resolving imports may still fail
    52  	// if the flag is set to "readonly" (the default) or "vendor".
    53  	ResolveMissingImports bool
    54  
    55  	// AssumeRootsImported indicates that the transitive dependencies of the root
    56  	// packages should be treated as if those roots will be imported by the main
    57  	// module.
    58  	AssumeRootsImported bool
    59  
    60  	// AllowPackage, if non-nil, is called after identifying the module providing
    61  	// each package. If AllowPackage returns a non-nil error, that error is set
    62  	// for the package, and the imports and test of that package will not be
    63  	// loaded.
    64  	//
    65  	// AllowPackage may be invoked concurrently by multiple goroutines,
    66  	// and may be invoked multiple times for a given package path.
    67  	AllowPackage func(ctx context.Context, path string, mod module.Version) error
    68  
    69  	// LoadTests loads the test dependencies of each package matching a requested
    70  	// pattern. If ResolveMissingImports is also true, test dependencies will be
    71  	// resolved if missing.
    72  	LoadTests bool
    73  
    74  	// UseVendorAll causes the "all" package pattern to be interpreted as if
    75  	// running "go mod vendor" (or building with "-mod=vendor").
    76  	//
    77  	// This is a no-op for modules that declare 'go 1.16' or higher, for which this
    78  	// is the default (and only) interpretation of the "all" pattern in module mode.
    79  	UseVendorAll bool
    80  
    81  	// AllowErrors indicates that LoadPackages should not terminate the process if
    82  	// an error occurs.
    83  	AllowErrors bool
    84  
    85  	// SilencePackageErrors indicates that LoadPackages should not print errors
    86  	// that occur while matching or loading packages, and should not terminate the
    87  	// process if such an error occurs.
    88  	//
    89  	// Errors encountered in the module graph will still be reported.
    90  	//
    91  	// The caller may retrieve the silenced package errors using the Lookup
    92  	// function, and matching errors are still populated in the Errs field of the
    93  	// associated search.Match.)
    94  	SilencePackageErrors bool
    95  
    96  	// SilenceMissingStdImports indicates that LoadPackages should not print
    97  	// errors or terminate the process if an imported package is missing, and the
    98  	// import path looks like it might be in the standard library (perhaps in a
    99  	// future version).
   100  	SilenceMissingStdImports bool
   101  
   102  	// SilenceNoGoErrors indicates that LoadPackages should not print
   103  	// imports.ErrNoGo errors.
   104  	// This allows the caller to invoke LoadPackages (and report other errors)
   105  	// without knowing whether the requested packages exist for the given tags.
   106  	//
   107  	// Note that if a requested package does not exist *at all*, it will fail
   108  	// during module resolution and the error will not be suppressed.
   109  	SilenceNoGoErrors bool
   110  
   111  	// SilenceUnmatchedWarnings suppresses the warnings normally emitted for
   112  	// patterns that did not match any packages.
   113  	SilenceUnmatchedWarnings bool
   114  
   115  	// Resolve the query against this module.
   116  	MainModule module.Version
   117  
   118  	// If Switcher is non-nil, then LoadPackages passes all encountered errors
   119  	// to Switcher.Error and tries Switcher.Switch before base.ExitIfErrors.
   120  	Switcher gover.Switcher
   121  }
   122  
   123  // LoadPackages identifies the set of packages matching the given patterns and
   124  // loads the packages in the import graph rooted at that set.
   125  func LoadPackages(ctx context.Context, opts PackageOpts, patterns ...string) (matches []*search.Match, loadedPackages []string)
   126  
   127  // ImportFromFiles adds modules to the build list as needed
   128  // to satisfy the imports in the named Go source files.
   129  //
   130  // Errors in missing dependencies are silenced.
   131  //
   132  // TODO(bcmills): Silencing errors seems off. Take a closer look at this and
   133  // figure out what the error-reporting actually ought to be.
   134  func ImportFromFiles(ctx context.Context, gofiles []string)
   135  
   136  // DirImportPath returns the effective import path for dir,
   137  // provided it is within a main module, or else returns ".".
   138  func (mms *MainModuleSet) DirImportPath(ctx context.Context, dir string) (path string, m module.Version)
   139  
   140  // PackageModule returns the module providing the package named by the import path.
   141  func PackageModule(path string) module.Version
   142  
   143  // Lookup returns the source directory, import path, and any loading error for
   144  // the package at path as imported from the package in parentDir.
   145  // Lookup requires that one of the Load functions in this package has already
   146  // been called.
   147  func Lookup(parentPath string, parentIsStd bool, path string) (dir, realPath string, err error)
   148  
   149  // Why returns the "go mod why" output stanza for the given package,
   150  // without the leading # comment.
   151  // The package graph must have been loaded already, usually by LoadPackages.
   152  // If there is no reason for the package to be in the current build,
   153  // Why returns an empty string.
   154  func Why(path string) string
   155  
   156  // WhyDepth returns the number of steps in the Why listing.
   157  // If there is no reason for the package to be in the current build,
   158  // WhyDepth returns 0.
   159  func WhyDepth(path string) int