Source file src/cmd/go/internal/modload/import.go

     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  	"context"
     9  	"errors"
    10  	"fmt"
    11  	"go/build"
    12  	"internal/goroot"
    13  	"io/fs"
    14  	"os"
    15  	pathpkg "path"
    16  	"path/filepath"
    17  	"sort"
    18  	"strings"
    19  
    20  	"cmd/go/internal/cfg"
    21  	"cmd/go/internal/fsys"
    22  	"cmd/go/internal/modfetch"
    23  	"cmd/go/internal/par"
    24  	"cmd/go/internal/search"
    25  
    26  	"golang.org/x/mod/module"
    27  	"golang.org/x/mod/semver"
    28  )
    29  
    30  type ImportMissingError struct {
    31  	Path     string
    32  	Module   module.Version
    33  	QueryErr error
    34  
    35  	ImportingMainModule module.Version
    36  
    37  	// isStd indicates whether we would expect to find the package in the standard
    38  	// library. This is normally true for all dotless import paths, but replace
    39  	// directives can cause us to treat the replaced paths as also being in
    40  	// modules.
    41  	isStd bool
    42  
    43  	// replaced the highest replaced version of the module where the replacement
    44  	// contains the package. replaced is only set if the replacement is unused.
    45  	replaced module.Version
    46  
    47  	// newMissingVersion is set to a newer version of Module if one is present
    48  	// in the build list. When set, we can't automatically upgrade.
    49  	newMissingVersion string
    50  }
    51  
    52  func (e *ImportMissingError) Error() string {
    53  	if e.Module.Path == "" {
    54  		if e.isStd {
    55  			return fmt.Sprintf("package %s is not in GOROOT (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path))
    56  		}
    57  		if e.QueryErr != nil && e.QueryErr != ErrNoModRoot {
    58  			return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)
    59  		}
    60  		if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && allowMissingModuleImports) {
    61  			return "cannot find module providing package " + e.Path
    62  		}
    63  
    64  		if e.replaced.Path != "" {
    65  			suggestArg := e.replaced.Path
    66  			if !module.IsZeroPseudoVersion(e.replaced.Version) {
    67  				suggestArg = e.replaced.String()
    68  			}
    69  			return fmt.Sprintf("module %s provides package %s and is replaced but not required; to add it:\n\tgo get %s", e.replaced.Path, e.Path, suggestArg)
    70  		}
    71  
    72  		message := fmt.Sprintf("no required module provides package %s", e.Path)
    73  		if e.QueryErr != nil {
    74  			return fmt.Sprintf("%s: %v", message, e.QueryErr)
    75  		}
    76  		if e.ImportingMainModule.Path != "" && e.ImportingMainModule != MainModules.ModContainingCWD() {
    77  			return fmt.Sprintf("%s; to add it:\n\tcd %s\n\tgo get %s", message, MainModules.ModRoot(e.ImportingMainModule), e.Path)
    78  		}
    79  		return fmt.Sprintf("%s; to add it:\n\tgo get %s", message, e.Path)
    80  	}
    81  
    82  	if e.newMissingVersion != "" {
    83  		return fmt.Sprintf("package %s provided by %s at latest version %s but not at required version %s", e.Path, e.Module.Path, e.Module.Version, e.newMissingVersion)
    84  	}
    85  
    86  	return fmt.Sprintf("missing module for import: %s@%s provides %s", e.Module.Path, e.Module.Version, e.Path)
    87  }
    88  
    89  func (e *ImportMissingError) Unwrap() error {
    90  	return e.QueryErr
    91  }
    92  
    93  func (e *ImportMissingError) ImportPath() string {
    94  	return e.Path
    95  }
    96  
    97  // An AmbiguousImportError indicates an import of a package found in multiple
    98  // modules in the build list, or found in both the main module and its vendor
    99  // directory.
   100  type AmbiguousImportError struct {
   101  	importPath string
   102  	Dirs       []string
   103  	Modules    []module.Version // Either empty or 1:1 with Dirs.
   104  }
   105  
   106  func (e *AmbiguousImportError) ImportPath() string {
   107  	return e.importPath
   108  }
   109  
   110  func (e *AmbiguousImportError) Error() string {
   111  	locType := "modules"
   112  	if len(e.Modules) == 0 {
   113  		locType = "directories"
   114  	}
   115  
   116  	var buf strings.Builder
   117  	fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.importPath, locType)
   118  
   119  	for i, dir := range e.Dirs {
   120  		buf.WriteString("\n\t")
   121  		if i < len(e.Modules) {
   122  			m := e.Modules[i]
   123  			buf.WriteString(m.Path)
   124  			if m.Version != "" {
   125  				fmt.Fprintf(&buf, " %s", m.Version)
   126  			}
   127  			fmt.Fprintf(&buf, " (%s)", dir)
   128  		} else {
   129  			buf.WriteString(dir)
   130  		}
   131  	}
   132  
   133  	return buf.String()
   134  }
   135  
   136  // A DirectImportFromImplicitDependencyError indicates a package directly
   137  // imported by a package or test in the main module that is satisfied by a
   138  // dependency that is not explicit in the main module's go.mod file.
   139  type DirectImportFromImplicitDependencyError struct {
   140  	ImporterPath string
   141  	ImportedPath string
   142  	Module       module.Version
   143  }
   144  
   145  func (e *DirectImportFromImplicitDependencyError) Error() string {
   146  	return fmt.Sprintf("package %s imports %s from implicitly required module; to add missing requirements, run:\n\tgo get %s@%s", e.ImporterPath, e.ImportedPath, e.Module.Path, e.Module.Version)
   147  }
   148  
   149  func (e *DirectImportFromImplicitDependencyError) ImportPath() string {
   150  	return e.ImporterPath
   151  }
   152  
   153  // ImportMissingSumError is reported in readonly mode when we need to check
   154  // if a module contains a package, but we don't have a sum for its .zip file.
   155  // We might need sums for multiple modules to verify the package is unique.
   156  //
   157  // TODO(#43653): consolidate multiple errors of this type into a single error
   158  // that suggests a 'go get' command for root packages that transtively import
   159  // packages from modules with missing sums. load.CheckPackageErrors would be
   160  // a good place to consolidate errors, but we'll need to attach the import
   161  // stack here.
   162  type ImportMissingSumError struct {
   163  	importPath                string
   164  	found                     bool
   165  	mods                      []module.Version
   166  	importer, importerVersion string // optional, but used for additional context
   167  	importerIsTest            bool
   168  }
   169  
   170  func (e *ImportMissingSumError) Error() string {
   171  	var importParen string
   172  	if e.importer != "" {
   173  		importParen = fmt.Sprintf(" (imported by %s)", e.importer)
   174  	}
   175  	var message string
   176  	if e.found {
   177  		message = fmt.Sprintf("missing go.sum entry needed to verify package %s%s is provided by exactly one module", e.importPath, importParen)
   178  	} else {
   179  		message = fmt.Sprintf("missing go.sum entry for module providing package %s%s", e.importPath, importParen)
   180  	}
   181  	var hint string
   182  	if e.importer == "" {
   183  		// Importing package is unknown, or the missing package was named on the
   184  		// command line. Recommend 'go mod download' for the modules that could
   185  		// provide the package, since that shouldn't change go.mod.
   186  		if len(e.mods) > 0 {
   187  			args := make([]string, len(e.mods))
   188  			for i, mod := range e.mods {
   189  				args[i] = mod.Path
   190  			}
   191  			hint = fmt.Sprintf("; to add:\n\tgo mod download %s", strings.Join(args, " "))
   192  		}
   193  	} else {
   194  		// Importing package is known (common case). Recommend 'go get' on the
   195  		// current version of the importing package.
   196  		tFlag := ""
   197  		if e.importerIsTest {
   198  			tFlag = " -t"
   199  		}
   200  		version := ""
   201  		if e.importerVersion != "" {
   202  			version = "@" + e.importerVersion
   203  		}
   204  		hint = fmt.Sprintf("; to add:\n\tgo get%s %s%s", tFlag, e.importer, version)
   205  	}
   206  	return message + hint
   207  }
   208  
   209  func (e *ImportMissingSumError) ImportPath() string {
   210  	return e.importPath
   211  }
   212  
   213  type invalidImportError struct {
   214  	importPath string
   215  	err        error
   216  }
   217  
   218  func (e *invalidImportError) ImportPath() string {
   219  	return e.importPath
   220  }
   221  
   222  func (e *invalidImportError) Error() string {
   223  	return e.err.Error()
   224  }
   225  
   226  func (e *invalidImportError) Unwrap() error {
   227  	return e.err
   228  }
   229  
   230  // importFromModules finds the module and directory in the dependency graph of
   231  // rs containing the package with the given import path. If mg is nil,
   232  // importFromModules attempts to locate the module using only the main module
   233  // and the roots of rs before it loads the full graph.
   234  //
   235  // The answer must be unique: importFromModules returns an error if multiple
   236  // modules are observed to provide the same package.
   237  //
   238  // importFromModules can return a module with an empty m.Path, for packages in
   239  // the standard library.
   240  //
   241  // importFromModules can return an empty directory string, for fake packages
   242  // like "C" and "unsafe".
   243  //
   244  // If the package is not present in any module selected from the requirement
   245  // graph, importFromModules returns an *ImportMissingError.
   246  //
   247  // If the package is present in exactly one module, importFromModules will
   248  // return the module, its root directory, and a list of other modules that
   249  // lexically could have provided the package but did not.
   250  func importFromModules(ctx context.Context, path string, rs *Requirements, mg *ModuleGraph) (m module.Version, dir string, altMods []module.Version, err error) {
   251  	invalidf := func(format string, args ...interface{}) (module.Version, string, []module.Version, error) {
   252  		return module.Version{}, "", nil, &invalidImportError{
   253  			importPath: path,
   254  			err:        fmt.Errorf(format, args...),
   255  		}
   256  	}
   257  
   258  	if strings.Contains(path, "@") {
   259  		return invalidf("import path %q should not have @version", path)
   260  	}
   261  	if build.IsLocalImport(path) {
   262  		return invalidf("%q is relative, but relative import paths are not supported in module mode", path)
   263  	}
   264  	if filepath.IsAbs(path) {
   265  		return invalidf("%q is not a package path; see 'go help packages'", path)
   266  	}
   267  	if search.IsMetaPackage(path) {
   268  		return invalidf("%q is not an importable package; see 'go help packages'", path)
   269  	}
   270  
   271  	if path == "C" {
   272  		// There's no directory for import "C".
   273  		return module.Version{}, "", nil, nil
   274  	}
   275  	// Before any further lookup, check that the path is valid.
   276  	if err := module.CheckImportPath(path); err != nil {
   277  		return module.Version{}, "", nil, &invalidImportError{importPath: path, err: err}
   278  	}
   279  
   280  	// Is the package in the standard library?
   281  	pathIsStd := search.IsStandardImportPath(path)
   282  	if pathIsStd && goroot.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {
   283  		for _, mainModule := range MainModules.Versions() {
   284  			if MainModules.InGorootSrc(mainModule) {
   285  				if dir, ok, err := dirInModule(path, MainModules.PathPrefix(mainModule), MainModules.ModRoot(mainModule), true); err != nil {
   286  					return module.Version{}, dir, nil, err
   287  				} else if ok {
   288  					return mainModule, dir, nil, nil
   289  				}
   290  			}
   291  		}
   292  		dir := filepath.Join(cfg.GOROOT, "src", path)
   293  		return module.Version{}, dir, nil, nil
   294  	}
   295  
   296  	// -mod=vendor is special.
   297  	// Everything must be in the main module or the main module's vendor directory.
   298  	if cfg.BuildMod == "vendor" {
   299  		mainModule := MainModules.mustGetSingleMainModule()
   300  		modRoot := MainModules.ModRoot(mainModule)
   301  		mainDir, mainOK, mainErr := dirInModule(path, MainModules.PathPrefix(mainModule), modRoot, true)
   302  		vendorDir, vendorOK, _ := dirInModule(path, "", filepath.Join(modRoot, "vendor"), false)
   303  		if mainOK && vendorOK {
   304  			return module.Version{}, "", nil, &AmbiguousImportError{importPath: path, Dirs: []string{mainDir, vendorDir}}
   305  		}
   306  		// Prefer to return main directory if there is one,
   307  		// Note that we're not checking that the package exists.
   308  		// We'll leave that for load.
   309  		if !vendorOK && mainDir != "" {
   310  			return mainModule, mainDir, nil, nil
   311  		}
   312  		if mainErr != nil {
   313  			return module.Version{}, "", nil, mainErr
   314  		}
   315  		readVendorList(mainModule)
   316  		return vendorPkgModule[path], vendorDir, nil, nil
   317  	}
   318  
   319  	// Check each module on the build list.
   320  	var dirs []string
   321  	var mods []module.Version
   322  
   323  	// Iterate over possible modules for the path, not all selected modules.
   324  	// Iterating over selected modules would make the overall loading time
   325  	// O(M × P) for M modules providing P imported packages, whereas iterating
   326  	// over path prefixes is only O(P × k) with maximum path depth k. For
   327  	// large projects both M and P may be very large (note that M ≤ P), but k
   328  	// will tend to remain smallish (if for no other reason than filesystem
   329  	// path limitations).
   330  	//
   331  	// We perform this iteration either one or two times. If mg is initially nil,
   332  	// then we first attempt to load the package using only the main module and
   333  	// its root requirements. If that does not identify the package, or if mg is
   334  	// already non-nil, then we attempt to load the package using the full
   335  	// requirements in mg.
   336  	for {
   337  		var sumErrMods, altMods []module.Version
   338  		for prefix := path; prefix != "."; prefix = pathpkg.Dir(prefix) {
   339  			var (
   340  				v  string
   341  				ok bool
   342  			)
   343  			if mg == nil {
   344  				v, ok = rs.rootSelected(prefix)
   345  			} else {
   346  				v, ok = mg.Selected(prefix), true
   347  			}
   348  			if !ok || v == "none" {
   349  				continue
   350  			}
   351  			m := module.Version{Path: prefix, Version: v}
   352  
   353  			needSum := true
   354  			root, isLocal, err := fetch(ctx, m, needSum)
   355  			if err != nil {
   356  				if sumErr := (*sumMissingError)(nil); errors.As(err, &sumErr) {
   357  					// We are missing a sum needed to fetch a module in the build list.
   358  					// We can't verify that the package is unique, and we may not find
   359  					// the package at all. Keep checking other modules to decide which
   360  					// error to report. Multiple sums may be missing if we need to look in
   361  					// multiple nested modules to resolve the import; we'll report them all.
   362  					sumErrMods = append(sumErrMods, m)
   363  					continue
   364  				}
   365  				// Report fetch error.
   366  				// Note that we don't know for sure this module is necessary,
   367  				// but it certainly _could_ provide the package, and even if we
   368  				// continue the loop and find the package in some other module,
   369  				// we need to look at this module to make sure the import is
   370  				// not ambiguous.
   371  				return module.Version{}, "", nil, err
   372  			}
   373  			if dir, ok, err := dirInModule(path, m.Path, root, isLocal); err != nil {
   374  				return module.Version{}, "", nil, err
   375  			} else if ok {
   376  				mods = append(mods, m)
   377  				dirs = append(dirs, dir)
   378  			} else {
   379  				altMods = append(altMods, m)
   380  			}
   381  		}
   382  
   383  		if len(mods) > 1 {
   384  			// We produce the list of directories from longest to shortest candidate
   385  			// module path, but the AmbiguousImportError should report them from
   386  			// shortest to longest. Reverse them now.
   387  			for i := 0; i < len(mods)/2; i++ {
   388  				j := len(mods) - 1 - i
   389  				mods[i], mods[j] = mods[j], mods[i]
   390  				dirs[i], dirs[j] = dirs[j], dirs[i]
   391  			}
   392  			return module.Version{}, "", nil, &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods}
   393  		}
   394  
   395  		if len(sumErrMods) > 0 {
   396  			for i := 0; i < len(sumErrMods)/2; i++ {
   397  				j := len(sumErrMods) - 1 - i
   398  				sumErrMods[i], sumErrMods[j] = sumErrMods[j], sumErrMods[i]
   399  			}
   400  			return module.Version{}, "", nil, &ImportMissingSumError{
   401  				importPath: path,
   402  				mods:       sumErrMods,
   403  				found:      len(mods) > 0,
   404  			}
   405  		}
   406  
   407  		if len(mods) == 1 {
   408  			return mods[0], dirs[0], altMods, nil
   409  		}
   410  
   411  		if mg != nil {
   412  			// We checked the full module graph and still didn't find the
   413  			// requested package.
   414  			var queryErr error
   415  			if !HasModRoot() {
   416  				queryErr = ErrNoModRoot
   417  			}
   418  			return module.Version{}, "", nil, &ImportMissingError{Path: path, QueryErr: queryErr, isStd: pathIsStd}
   419  		}
   420  
   421  		// So far we've checked the root dependencies.
   422  		// Load the full module graph and try again.
   423  		mg, err = rs.Graph(ctx)
   424  		if err != nil {
   425  			// We might be missing one or more transitive (implicit) dependencies from
   426  			// the module graph, so we can't return an ImportMissingError here — one
   427  			// of the missing modules might actually contain the package in question,
   428  			// in which case we shouldn't go looking for it in some new dependency.
   429  			return module.Version{}, "", nil, err
   430  		}
   431  	}
   432  }
   433  
   434  // queryImport attempts to locate a module that can be added to the current
   435  // build list to provide the package with the given import path.
   436  //
   437  // Unlike QueryPattern, queryImport prefers to add a replaced version of a
   438  // module *before* checking the proxies for a version to add.
   439  func queryImport(ctx context.Context, path string, rs *Requirements) (module.Version, error) {
   440  	// To avoid spurious remote fetches, try the latest replacement for each
   441  	// module (golang.org/issue/26241).
   442  	var mods []module.Version
   443  	if MainModules != nil { // TODO(#48912): Ensure MainModules exists at this point, and remove the check.
   444  		for mp, mv := range MainModules.HighestReplaced() {
   445  			if !maybeInModule(path, mp) {
   446  				continue
   447  			}
   448  			if mv == "" {
   449  				// The only replacement is a wildcard that doesn't specify a version, so
   450  				// synthesize a pseudo-version with an appropriate major version and a
   451  				// timestamp below any real timestamp. That way, if the main module is
   452  				// used from within some other module, the user will be able to upgrade
   453  				// the requirement to any real version they choose.
   454  				if _, pathMajor, ok := module.SplitPathVersion(mp); ok && len(pathMajor) > 0 {
   455  					mv = module.ZeroPseudoVersion(pathMajor[1:])
   456  				} else {
   457  					mv = module.ZeroPseudoVersion("v0")
   458  				}
   459  			}
   460  			mg, err := rs.Graph(ctx)
   461  			if err != nil {
   462  				return module.Version{}, err
   463  			}
   464  			if cmpVersion(mg.Selected(mp), mv) >= 0 {
   465  				// We can't resolve the import by adding mp@mv to the module graph,
   466  				// because the selected version of mp is already at least mv.
   467  				continue
   468  			}
   469  			mods = append(mods, module.Version{Path: mp, Version: mv})
   470  		}
   471  	}
   472  
   473  	// Every module path in mods is a prefix of the import path.
   474  	// As in QueryPattern, prefer the longest prefix that satisfies the import.
   475  	sort.Slice(mods, func(i, j int) bool {
   476  		return len(mods[i].Path) > len(mods[j].Path)
   477  	})
   478  	for _, m := range mods {
   479  		needSum := true
   480  		root, isLocal, err := fetch(ctx, m, needSum)
   481  		if err != nil {
   482  			if sumErr := (*sumMissingError)(nil); errors.As(err, &sumErr) {
   483  				return module.Version{}, &ImportMissingSumError{importPath: path}
   484  			}
   485  			return module.Version{}, err
   486  		}
   487  		if _, ok, err := dirInModule(path, m.Path, root, isLocal); err != nil {
   488  			return m, err
   489  		} else if ok {
   490  			if cfg.BuildMod == "readonly" {
   491  				return module.Version{}, &ImportMissingError{Path: path, replaced: m}
   492  			}
   493  			return m, nil
   494  		}
   495  	}
   496  	if len(mods) > 0 && module.CheckPath(path) != nil {
   497  		// The package path is not valid to fetch remotely,
   498  		// so it can only exist in a replaced module,
   499  		// and we know from the above loop that it is not.
   500  		replacement := Replacement(mods[0])
   501  		return module.Version{}, &PackageNotInModuleError{
   502  			Mod:         mods[0],
   503  			Query:       "latest",
   504  			Pattern:     path,
   505  			Replacement: replacement,
   506  		}
   507  	}
   508  
   509  	if search.IsStandardImportPath(path) {
   510  		// This package isn't in the standard library, isn't in any module already
   511  		// in the build list, and isn't in any other module that the user has
   512  		// shimmed in via a "replace" directive.
   513  		// Moreover, the import path is reserved for the standard library, so
   514  		// QueryPattern cannot possibly find a module containing this package.
   515  		//
   516  		// Instead of trying QueryPattern, report an ImportMissingError immediately.
   517  		return module.Version{}, &ImportMissingError{Path: path, isStd: true}
   518  	}
   519  
   520  	if cfg.BuildMod == "readonly" && !allowMissingModuleImports {
   521  		// In readonly mode, we can't write go.mod, so we shouldn't try to look up
   522  		// the module. If readonly mode was enabled explicitly, include that in
   523  		// the error message.
   524  		var queryErr error
   525  		if cfg.BuildModExplicit {
   526  			queryErr = fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)
   527  		} else if cfg.BuildModReason != "" {
   528  			queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)
   529  		}
   530  		return module.Version{}, &ImportMissingError{Path: path, QueryErr: queryErr}
   531  	}
   532  
   533  	// Look up module containing the package, for addition to the build list.
   534  	// Goal is to determine the module, download it to dir,
   535  	// and return m, dir, ImpportMissingError.
   536  	fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)
   537  
   538  	mg, err := rs.Graph(ctx)
   539  	if err != nil {
   540  		return module.Version{}, err
   541  	}
   542  
   543  	candidates, err := QueryPackages(ctx, path, "latest", mg.Selected, CheckAllowed)
   544  	if err != nil {
   545  		if errors.Is(err, fs.ErrNotExist) {
   546  			// Return "cannot find module providing package […]" instead of whatever
   547  			// low-level error QueryPattern produced.
   548  			return module.Version{}, &ImportMissingError{Path: path, QueryErr: err}
   549  		} else {
   550  			return module.Version{}, err
   551  		}
   552  	}
   553  
   554  	candidate0MissingVersion := ""
   555  	for i, c := range candidates {
   556  		if v := mg.Selected(c.Mod.Path); semver.Compare(v, c.Mod.Version) > 0 {
   557  			// QueryPattern proposed that we add module c.Mod to provide the package,
   558  			// but we already depend on a newer version of that module (and that
   559  			// version doesn't have the package).
   560  			//
   561  			// This typically happens when a package is present at the "@latest"
   562  			// version (e.g., v1.0.0) of a module, but we have a newer version
   563  			// of the same module in the build list (e.g., v1.0.1-beta), and
   564  			// the package is not present there.
   565  			if i == 0 {
   566  				candidate0MissingVersion = v
   567  			}
   568  			continue
   569  		}
   570  		return c.Mod, nil
   571  	}
   572  	return module.Version{}, &ImportMissingError{
   573  		Path:              path,
   574  		Module:            candidates[0].Mod,
   575  		newMissingVersion: candidate0MissingVersion,
   576  	}
   577  }
   578  
   579  // maybeInModule reports whether, syntactically,
   580  // a package with the given import path could be supplied
   581  // by a module with the given module path (mpath).
   582  func maybeInModule(path, mpath string) bool {
   583  	return mpath == path ||
   584  		len(path) > len(mpath) && path[len(mpath)] == '/' && path[:len(mpath)] == mpath
   585  }
   586  
   587  var (
   588  	haveGoModCache   par.Cache // dir → bool
   589  	haveGoFilesCache par.Cache // dir → goFilesEntry
   590  )
   591  
   592  type goFilesEntry struct {
   593  	haveGoFiles bool
   594  	err         error
   595  }
   596  
   597  // dirInModule locates the directory that would hold the package named by the given path,
   598  // if it were in the module with module path mpath and root mdir.
   599  // If path is syntactically not within mpath,
   600  // or if mdir is a local file tree (isLocal == true) and the directory
   601  // that would hold path is in a sub-module (covered by a go.mod below mdir),
   602  // dirInModule returns "", false, nil.
   603  //
   604  // Otherwise, dirInModule returns the name of the directory where
   605  // Go source files would be expected, along with a boolean indicating
   606  // whether there are in fact Go source files in that directory.
   607  // A non-nil error indicates that the existence of the directory and/or
   608  // source files could not be determined, for example due to a permission error.
   609  func dirInModule(path, mpath, mdir string, isLocal bool) (dir string, haveGoFiles bool, err error) {
   610  	// Determine where to expect the package.
   611  	if path == mpath {
   612  		dir = mdir
   613  	} else if mpath == "" { // vendor directory
   614  		dir = filepath.Join(mdir, path)
   615  	} else if len(path) > len(mpath) && path[len(mpath)] == '/' && path[:len(mpath)] == mpath {
   616  		dir = filepath.Join(mdir, path[len(mpath)+1:])
   617  	} else {
   618  		return "", false, nil
   619  	}
   620  
   621  	// Check that there aren't other modules in the way.
   622  	// This check is unnecessary inside the module cache
   623  	// and important to skip in the vendor directory,
   624  	// where all the module trees have been overlaid.
   625  	// So we only check local module trees
   626  	// (the main module, and any directory trees pointed at by replace directives).
   627  	if isLocal {
   628  		for d := dir; d != mdir && len(d) > len(mdir); {
   629  			haveGoMod := haveGoModCache.Do(d, func() any {
   630  				fi, err := fsys.Stat(filepath.Join(d, "go.mod"))
   631  				return err == nil && !fi.IsDir()
   632  			}).(bool)
   633  
   634  			if haveGoMod {
   635  				return "", false, nil
   636  			}
   637  			parent := filepath.Dir(d)
   638  			if parent == d {
   639  				// Break the loop, as otherwise we'd loop
   640  				// forever if d=="." and mdir=="".
   641  				break
   642  			}
   643  			d = parent
   644  		}
   645  	}
   646  
   647  	// Now committed to returning dir (not "").
   648  
   649  	// Are there Go source files in the directory?
   650  	// We don't care about build tags, not even "+build ignore".
   651  	// We're just looking for a plausible directory.
   652  	res := haveGoFilesCache.Do(dir, func() any {
   653  		ok, err := fsys.IsDirWithGoFiles(dir)
   654  		return goFilesEntry{haveGoFiles: ok, err: err}
   655  	}).(goFilesEntry)
   656  
   657  	return dir, res.haveGoFiles, res.err
   658  }
   659  
   660  // fetch downloads the given module (or its replacement)
   661  // and returns its location.
   662  //
   663  // needSum indicates whether the module may be downloaded in readonly mode
   664  // without a go.sum entry. It should only be false for modules fetched
   665  // speculatively (for example, for incompatible version filtering). The sum
   666  // will still be verified normally.
   667  //
   668  // The isLocal return value reports whether the replacement,
   669  // if any, is local to the filesystem.
   670  func fetch(ctx context.Context, mod module.Version, needSum bool) (dir string, isLocal bool, err error) {
   671  	if modRoot := MainModules.ModRoot(mod); modRoot != "" {
   672  		return modRoot, true, nil
   673  	}
   674  	if r := Replacement(mod); r.Path != "" {
   675  		if r.Version == "" {
   676  			dir = r.Path
   677  			if !filepath.IsAbs(dir) {
   678  				dir = filepath.Join(replaceRelativeTo(), dir)
   679  			}
   680  			// Ensure that the replacement directory actually exists:
   681  			// dirInModule does not report errors for missing modules,
   682  			// so if we don't report the error now, later failures will be
   683  			// very mysterious.
   684  			if _, err := fsys.Stat(dir); err != nil {
   685  				if os.IsNotExist(err) {
   686  					// Semantically the module version itself “exists” — we just don't
   687  					// have its source code. Remove the equivalence to os.ErrNotExist,
   688  					// and make the message more concise while we're at it.
   689  					err = fmt.Errorf("replacement directory %s does not exist", r.Path)
   690  				} else {
   691  					err = fmt.Errorf("replacement directory %s: %w", r.Path, err)
   692  				}
   693  				return dir, true, module.VersionError(mod, err)
   694  			}
   695  			return dir, true, nil
   696  		}
   697  		mod = r
   698  	}
   699  
   700  	if HasModRoot() && cfg.BuildMod == "readonly" && !inWorkspaceMode() && needSum && !modfetch.HaveSum(mod) {
   701  		return "", false, module.VersionError(mod, &sumMissingError{})
   702  	}
   703  
   704  	dir, err = modfetch.Download(ctx, mod)
   705  	return dir, false, err
   706  }
   707  
   708  type sumMissingError struct {
   709  	suggestion string
   710  }
   711  
   712  func (e *sumMissingError) Error() string {
   713  	return "missing go.sum entry" + e.suggestion
   714  }
   715  

View as plain text