src/cmd/go/internal/modload/import.go GO 841 lines View on github.com → Search inside
1// Copyright 2018 The Go Authors. All rights reserved.2// Use of this source code is governed by a BSD-style3// license that can be found in the LICENSE file.45package modload67import (8	"context"9	"errors"10	"fmt"11	"go/build"12	"io/fs"13	"os"14	pathpkg "path"15	"path/filepath"16	"sort"17	"strings"1819	"cmd/go/internal/cfg"20	"cmd/go/internal/fsys"21	"cmd/go/internal/gover"22	"cmd/go/internal/modfetch"23	"cmd/go/internal/modindex"24	"cmd/go/internal/search"25	"cmd/go/internal/str"26	"cmd/internal/par"2728	"golang.org/x/mod/module"29)3031type ImportMissingError struct {32	Path                      string33	Module                    module.Version34	QueryErr                  error35	modContainingCWD          module.Version36	allowMissingModuleImports bool3738	// modRoot is dependent on the value of ImportingMainModule and should be39	// kept in sync.40	modRoot             string41	ImportingMainModule module.Version4243	// isStd indicates whether we would expect to find the package in the standard44	// library. This is normally true for all dotless import paths, but replace45	// directives can cause us to treat the replaced paths as also being in46	// modules.47	isStd bool4849	// importerGoVersion is the version the module containing the import error50	// specified. It is only set when isStd is true.51	importerGoVersion string5253	// replaced the highest replaced version of the module where the replacement54	// contains the package. replaced is only set if the replacement is unused.55	replaced module.Version5657	// newMissingVersion is set to a newer version of Module if one is present58	// in the build list. When set, we can't automatically upgrade.59	newMissingVersion string60}6162func (e *ImportMissingError) Error() string {63	if e.Module.Path == "" {64		if e.isStd {65			msg := fmt.Sprintf("package %s is not in std (%s)", e.Path, filepath.Join(cfg.GOROOT, "src", e.Path))66			if e.importerGoVersion != "" {67				msg += fmt.Sprintf("\nnote: imported by a module that requires go %s", e.importerGoVersion)68			}69			return msg70		}71		if e.QueryErr != nil && !errors.Is(e.QueryErr, ErrNoModRoot) {72			return fmt.Sprintf("cannot find module providing package %s: %v", e.Path, e.QueryErr)73		}74		if cfg.BuildMod == "mod" || (cfg.BuildMod == "readonly" && e.allowMissingModuleImports) {75			return "cannot find module providing package " + e.Path76		}7778		if e.replaced.Path != "" {79			suggestArg := e.replaced.Path80			if !module.IsZeroPseudoVersion(e.replaced.Version) {81				suggestArg = e.replaced.String()82			}83			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)84		}8586		message := fmt.Sprintf("no required module provides package %s", e.Path)87		if e.QueryErr != nil {88			return fmt.Sprintf("%s: %v", message, e.QueryErr)89		}90		if e.ImportingMainModule.Path != "" && e.ImportingMainModule != e.modContainingCWD {91			return fmt.Sprintf("%s; to add it:\n\tcd %s\n\tgo get %s", message, e.modRoot, e.Path)92		}93		return fmt.Sprintf("%s; to add it:\n\tgo get %s", message, e.Path)94	}9596	if e.newMissingVersion != "" {97		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)98	}99100	return fmt.Sprintf("missing module for import: %s@%s provides %s", e.Module.Path, e.Module.Version, e.Path)101}102103func (e *ImportMissingError) Unwrap() error {104	return e.QueryErr105}106107func (e *ImportMissingError) ImportPath() string {108	return e.Path109}110111// An AmbiguousImportError indicates an import of a package found in multiple112// modules in the build list, or found in both the main module and its vendor113// directory.114type AmbiguousImportError struct {115	importPath string116	Dirs       []string117	Modules    []module.Version // Either empty or 1:1 with Dirs.118}119120func (e *AmbiguousImportError) ImportPath() string {121	return e.importPath122}123124func (e *AmbiguousImportError) Error() string {125	locType := "modules"126	if len(e.Modules) == 0 {127		locType = "directories"128	}129130	var buf strings.Builder131	fmt.Fprintf(&buf, "ambiguous import: found package %s in multiple %s:", e.importPath, locType)132133	for i, dir := range e.Dirs {134		buf.WriteString("\n\t")135		if i < len(e.Modules) {136			m := e.Modules[i]137			buf.WriteString(m.Path)138			if m.Version != "" {139				fmt.Fprintf(&buf, " %s", m.Version)140			}141			fmt.Fprintf(&buf, " (%s)", dir)142		} else {143			buf.WriteString(dir)144		}145	}146147	return buf.String()148}149150// A DirectImportFromImplicitDependencyError indicates a package directly151// imported by a package or test in the main module that is satisfied by a152// dependency that is not explicit in the main module's go.mod file.153type DirectImportFromImplicitDependencyError struct {154	ImporterPath string155	ImportedPath string156	Module       module.Version157}158159func (e *DirectImportFromImplicitDependencyError) Error() string {160	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)161}162163func (e *DirectImportFromImplicitDependencyError) ImportPath() string {164	return e.ImporterPath165}166167// ImportMissingSumError is reported in readonly mode when we need to check168// if a module contains a package, but we don't have a sum for its .zip file.169// We might need sums for multiple modules to verify the package is unique.170//171// TODO(#43653): consolidate multiple errors of this type into a single error172// that suggests a 'go get' command for root packages that transitively import173// packages from modules with missing sums. load.CheckPackageErrors would be174// a good place to consolidate errors, but we'll need to attach the import175// stack here.176type ImportMissingSumError struct {177	importPath                string178	found                     bool179	mods                      []module.Version180	importer, importerVersion string // optional, but used for additional context181	importerIsTest            bool182}183184func (e *ImportMissingSumError) Error() string {185	var importParen string186	if e.importer != "" {187		importParen = fmt.Sprintf(" (imported by %s)", e.importer)188	}189	var message string190	if e.found {191		message = fmt.Sprintf("missing go.sum entry needed to verify package %s%s is provided by exactly one module", e.importPath, importParen)192	} else {193		message = fmt.Sprintf("missing go.sum entry for module providing package %s%s", e.importPath, importParen)194	}195	var hint string196	if e.importer == "" {197		// Importing package is unknown, or the missing package was named on the198		// command line. Recommend 'go mod download' for the modules that could199		// provide the package, since that shouldn't change go.mod.200		if len(e.mods) > 0 {201			args := make([]string, len(e.mods))202			for i, mod := range e.mods {203				args[i] = mod.Path204			}205			hint = fmt.Sprintf("; to add:\n\tgo mod download %s", strings.Join(args, " "))206		}207	} else {208		// Importing package is known (common case). Recommend 'go get' on the209		// current version of the importing package.210		tFlag := ""211		if e.importerIsTest {212			tFlag = " -t"213		}214		version := ""215		if e.importerVersion != "" {216			version = "@" + e.importerVersion217		}218		hint = fmt.Sprintf("; to add:\n\tgo get%s %s%s", tFlag, e.importer, version)219	}220	return message + hint221}222223func (e *ImportMissingSumError) ImportPath() string {224	return e.importPath225}226227type invalidImportError struct {228	importPath string229	err        error230}231232func (e *invalidImportError) ImportPath() string {233	return e.importPath234}235236func (e *invalidImportError) Error() string {237	return e.err.Error()238}239240func (e *invalidImportError) Unwrap() error {241	return e.err242}243244// importFromModules finds the module and directory in the dependency graph of245// rs containing the package with the given import path. If mg is nil,246// importFromModules attempts to locate the module using only the main module247// and the roots of rs before it loads the full graph.248//249// The answer must be unique: importFromModules returns an error if multiple250// modules are observed to provide the same package.251//252// importFromModules can return a module with an empty m.Path, for packages in253// the standard library.254//255// importFromModules can return an empty directory string, for fake packages256// like "C" and "unsafe".257//258// If the package is not present in any module selected from the requirement259// graph, importFromModules returns an *ImportMissingError.260//261// If the package is present in exactly one module, importFromModules will262// return the module, its root directory, and a list of other modules that263// lexically could have provided the package but did not.264//265// If skipModFile is true, the go.mod file for the package is not loaded. This266// allows 'go mod tidy' to preserve a minor checksum-preservation bug267// (https://go.dev/issue/56222) for modules with 'go' versions between 1.17 and268// 1.20, preventing unnecessary go.sum churn and network access in those269// modules.270func importFromModules(ld *Loader, ctx context.Context, path string, rs *Requirements, mg *ModuleGraph, skipModFile bool) (m module.Version, modroot, dir string, altMods []module.Version, err error) {271	invalidf := func(format string, args ...any) (module.Version, string, string, []module.Version, error) {272		return module.Version{}, "", "", nil, &invalidImportError{273			importPath: path,274			err:        fmt.Errorf(format, args...),275		}276	}277278	if strings.Contains(path, "@") {279		return invalidf("import path %q should not have @version", path)280	}281	if build.IsLocalImport(path) {282		return invalidf("%q is relative, but relative import paths are not supported in module mode", path)283	}284	if filepath.IsAbs(path) {285		return invalidf("%q is not a package path; see 'go help packages'", path)286	}287	if search.IsMetaPackage(path) {288		return invalidf("%q is not an importable package; see 'go help packages'", path)289	}290291	if path == "C" {292		// There's no directory for import "C".293		return module.Version{}, "", "", nil, nil294	}295	// Before any further lookup, check that the path is valid.296	if err := module.CheckImportPath(path); err != nil {297		return module.Version{}, "", "", nil, &invalidImportError{importPath: path, err: err}298	}299300	// Check each module on the build list.301	var dirs, roots []string302	var mods []module.Version303304	// Is the package in the standard library?305	pathIsStd := search.IsStandardImportPath(path)306	if pathIsStd && modindex.IsStandardPackage(cfg.GOROOT, cfg.BuildContext.Compiler, path) {307		for _, mainModule := range ld.MainModules.Versions() {308			if ld.MainModules.InGorootSrc(mainModule) {309				if dir, ok, err := dirInModule(path, ld.MainModules.PathPrefix(mainModule), ld.MainModules.ModRoot(mainModule), true); err != nil {310					return module.Version{}, ld.MainModules.ModRoot(mainModule), dir, nil, err311				} else if ok {312					return mainModule, ld.MainModules.ModRoot(mainModule), dir, nil, nil313				}314			}315		}316		dir := filepath.Join(cfg.GOROOTsrc, path)317		modroot = cfg.GOROOTsrc318		if str.HasPathPrefix(path, "cmd") {319			modroot = filepath.Join(cfg.GOROOTsrc, "cmd")320		}321		dirs = append(dirs, dir)322		roots = append(roots, modroot)323		mods = append(mods, module.Version{})324	}325	// -mod=vendor is special.326	// Everything must be in the main modules or the main module's or workspace's vendor directory.327	if cfg.BuildMod == "vendor" {328		var mainErr error329		for _, mainModule := range ld.MainModules.Versions() {330			modRoot := ld.MainModules.ModRoot(mainModule)331			if modRoot != "" {332				dir, mainOK, err := dirInModule(path, ld.MainModules.PathPrefix(mainModule), modRoot, true)333				if mainErr == nil {334					mainErr = err335				}336				if mainOK {337					mods = append(mods, mainModule)338					dirs = append(dirs, dir)339					roots = append(roots, modRoot)340				}341			}342		}343344		if ld.HasModRoot() {345			vendorDir := VendorDir(ld)346			dir, inVendorDir, _ := dirInModule(path, "", vendorDir, false)347			if inVendorDir {348				readVendorList(vendorDir)349				// If vendorPkgModule does not contain an entry for path then it's probably either because350				// vendor/modules.txt does not exist or the user manually added directories to the vendor directory.351				// Go 1.23 and later require vendored packages to be present in modules.txt to be imported.352				_, ok := vendorPkgModule[path]353				if ok || (gover.Compare(ld.MainModules.GoVersion(ld), gover.ExplicitModulesTxtImportVersion) < 0) {354					mods = append(mods, vendorPkgModule[path])355					dirs = append(dirs, dir)356					roots = append(roots, vendorDir)357				} else {358					subCommand := "mod"359					if ld.inWorkspaceMode() {360						subCommand = "work"361					}362					fmt.Fprintf(os.Stderr, "go: ignoring package %s which exists in the vendor directory but is missing from vendor/modules.txt. To sync the vendor directory run go %s vendor.\n", path, subCommand)363				}364			}365		}366367		if len(dirs) > 1 {368			return module.Version{}, "", "", nil, &AmbiguousImportError{importPath: path, Dirs: dirs}369		}370371		if mainErr != nil {372			return module.Version{}, "", "", nil, mainErr373		}374375		if len(mods) == 0 {376			return module.Version{}, "", "", nil, &ImportMissingError{377				Path:                      path,378				modContainingCWD:          ld.MainModules.ModContainingCWD(),379				allowMissingModuleImports: ld.allowMissingModuleImports,380			}381		}382383		return mods[0], roots[0], dirs[0], nil, nil384	}385386	// Iterate over possible modules for the path, not all selected modules.387	// Iterating over selected modules would make the overall loading time388	// O(M × P) for M modules providing P imported packages, whereas iterating389	// over path prefixes is only O(P × k) with maximum path depth k. For390	// large projects both M and P may be very large (note that M ≤ P), but k391	// will tend to remain smallish (if for no other reason than filesystem392	// path limitations).393	//394	// We perform this iteration either one or two times. If mg is initially nil,395	// then we first attempt to load the package using only the main module and396	// its root requirements. If that does not identify the package, or if mg is397	// already non-nil, then we attempt to load the package using the full398	// requirements in mg.399	for {400		var sumErrMods, altMods []module.Version401		for prefix := path; prefix != "."; prefix = pathpkg.Dir(prefix) {402			if gover.IsToolchain(prefix) {403				// Do not use the synthetic "go" module for "go/ast".404				continue405			}406			var (407				v  string408				ok bool409			)410			if mg == nil {411				v, ok = rs.rootSelected(ld, prefix)412			} else {413				v, ok = mg.Selected(prefix), true414			}415			if !ok || v == "none" {416				continue417			}418			m := module.Version{Path: prefix, Version: v}419420			root, isLocal, err := fetch(ld, ctx, m)421			if err != nil {422				if _, ok := errors.AsType[*sumMissingError](err); ok {423					// We are missing a sum needed to fetch a module in the build list.424					// We can't verify that the package is unique, and we may not find425					// the package at all. Keep checking other modules to decide which426					// error to report. Multiple sums may be missing if we need to look in427					// multiple nested modules to resolve the import; we'll report them all.428					sumErrMods = append(sumErrMods, m)429					continue430				}431				// Report fetch error.432				// Note that we don't know for sure this module is necessary,433				// but it certainly _could_ provide the package, and even if we434				// continue the loop and find the package in some other module,435				// we need to look at this module to make sure the import is436				// not ambiguous.437				return module.Version{}, "", "", nil, err438			}439			if dir, ok, err := dirInModule(path, m.Path, root, isLocal); err != nil {440				return module.Version{}, "", "", nil, err441			} else if ok {442				mods = append(mods, m)443				roots = append(roots, root)444				dirs = append(dirs, dir)445			} else {446				altMods = append(altMods, m)447			}448		}449450		if len(mods) > 1 {451			// We produce the list of directories from longest to shortest candidate452			// module path, but the AmbiguousImportError should report them from453			// shortest to longest. Reverse them now.454			for i := 0; i < len(mods)/2; i++ {455				j := len(mods) - 1 - i456				mods[i], mods[j] = mods[j], mods[i]457				roots[i], roots[j] = roots[j], roots[i]458				dirs[i], dirs[j] = dirs[j], dirs[i]459			}460			return module.Version{}, "", "", nil, &AmbiguousImportError{importPath: path, Dirs: dirs, Modules: mods}461		}462463		if len(sumErrMods) > 0 {464			for i := 0; i < len(sumErrMods)/2; i++ {465				j := len(sumErrMods) - 1 - i466				sumErrMods[i], sumErrMods[j] = sumErrMods[j], sumErrMods[i]467			}468			return module.Version{}, "", "", nil, &ImportMissingSumError{469				importPath: path,470				mods:       sumErrMods,471				found:      len(mods) > 0,472			}473		}474475		if len(mods) == 1 {476			// We've found the unique module containing the package.477			// However, in order to actually compile it we need to know what478			// Go language version to use, which requires its go.mod file.479			//480			// If the module graph is pruned and this is a test-only dependency481			// of a package in "all", we didn't necessarily load that file482			// when we read the module graph, so do it now to be sure.483			if !skipModFile && cfg.BuildMod != "vendor" && mods[0].Path != "" && !ld.MainModules.Contains(mods[0].Path) {484				if _, err := goModSummary(ld, mods[0]); err != nil {485					return module.Version{}, "", "", nil, err486				}487			}488			return mods[0], roots[0], dirs[0], altMods, nil489		}490491		if mg != nil {492			// We checked the full module graph and still didn't find the493			// requested package.494			var queryErr error495			if !ld.HasModRoot() {496				queryErr = NewNoMainModulesError(ld)497			}498			return module.Version{}, "", "", nil, &ImportMissingError{499				Path:                      path,500				QueryErr:                  queryErr,501				isStd:                     pathIsStd,502				modContainingCWD:          ld.MainModules.ModContainingCWD(),503				allowMissingModuleImports: ld.allowMissingModuleImports,504			}505		}506507		// So far we've checked the root dependencies.508		// Load the full module graph and try again.509		mg, err = rs.Graph(ld, ctx)510		if err != nil {511			// We might be missing one or more transitive (implicit) dependencies from512			// the module graph, so we can't return an ImportMissingError here — one513			// of the missing modules might actually contain the package in question,514			// in which case we shouldn't go looking for it in some new dependency.515			return module.Version{}, "", "", nil, err516		}517	}518}519520// queryImport attempts to locate a module that can be added to the current521// build list to provide the package with the given import path.522//523// Unlike QueryPattern, queryImport prefers to add a replaced version of a524// module *before* checking the proxies for a version to add.525func queryImport(ld *Loader, ctx context.Context, path string, rs *Requirements) (module.Version, error) {526	// To avoid spurious remote fetches, try the latest replacement for each527	// module (golang.org/issue/26241).528	var mods []module.Version529	if ld.MainModules != nil { // TODO(#48912): Ensure MainModules exists at this point, and remove the check.530		for mp, mv := range ld.MainModules.HighestReplaced() {531			if !maybeInModule(path, mp) {532				continue533			}534			if mv == "" {535				// The only replacement is a wildcard that doesn't specify a version, so536				// synthesize a pseudo-version with an appropriate major version and a537				// timestamp below any real timestamp. That way, if the main module is538				// used from within some other module, the user will be able to upgrade539				// the requirement to any real version they choose.540				if _, pathMajor, ok := module.SplitPathVersion(mp); ok && len(pathMajor) > 0 {541					mv = module.ZeroPseudoVersion(pathMajor[1:])542				} else {543					mv = module.ZeroPseudoVersion("v0")544				}545			}546			mg, err := rs.Graph(ld, ctx)547			if err != nil {548				return module.Version{}, err549			}550			if gover.ModCompare(mp, mg.Selected(mp), mv) >= 0 {551				// We can't resolve the import by adding mp@mv to the module graph,552				// because the selected version of mp is already at least mv.553				continue554			}555			mods = append(mods, module.Version{Path: mp, Version: mv})556		}557	}558559	// Every module path in mods is a prefix of the import path.560	// As in QueryPattern, prefer the longest prefix that satisfies the import.561	sort.Slice(mods, func(i, j int) bool {562		return len(mods[i].Path) > len(mods[j].Path)563	})564	for _, m := range mods {565		root, isLocal, err := fetch(ld, ctx, m)566		if err != nil {567			if _, ok := errors.AsType[*sumMissingError](err); ok {568				return module.Version{}, &ImportMissingSumError{importPath: path}569			}570			return module.Version{}, err571		}572		if _, ok, err := dirInModule(path, m.Path, root, isLocal); err != nil {573			return m, err574		} else if ok {575			if cfg.BuildMod == "readonly" {576				return module.Version{}, &ImportMissingError{577					Path:                      path,578					replaced:                  m,579					modContainingCWD:          ld.MainModules.ModContainingCWD(),580					allowMissingModuleImports: ld.allowMissingModuleImports,581				}582			}583			return m, nil584		}585	}586	if len(mods) > 0 && module.CheckPath(path) != nil {587		// The package path is not valid to fetch remotely,588		// so it can only exist in a replaced module,589		// and we know from the above loop that it is not.590		replacement := Replacement(ld, mods[0])591		return module.Version{}, &PackageNotInModuleError{592			Mod:         mods[0],593			Query:       "latest",594			Pattern:     path,595			Replacement: replacement,596		}597	}598599	if search.IsStandardImportPath(path) {600		// This package isn't in the standard library, isn't in any module already601		// in the build list, and isn't in any other module that the user has602		// shimmed in via a "replace" directive.603		// Moreover, the import path is reserved for the standard library, so604		// QueryPattern cannot possibly find a module containing this package.605		//606		// Instead of trying QueryPattern, report an ImportMissingError immediately.607		return module.Version{}, &ImportMissingError{608			Path:                      path,609			isStd:                     true,610			modContainingCWD:          ld.MainModules.ModContainingCWD(),611			allowMissingModuleImports: ld.allowMissingModuleImports,612		}613	}614615	if (cfg.BuildMod == "readonly" || cfg.BuildMod == "vendor") && !ld.allowMissingModuleImports {616		// In readonly mode, we can't write go.mod, so we shouldn't try to look up617		// the module. If readonly mode was enabled explicitly, include that in618		// the error message.619		// In vendor mode, we cannot use the network or module cache, so we620		// shouldn't try to look up the module621		var queryErr error622		if cfg.BuildModExplicit {623			queryErr = fmt.Errorf("import lookup disabled by -mod=%s", cfg.BuildMod)624		} else if cfg.BuildModReason != "" {625			queryErr = fmt.Errorf("import lookup disabled by -mod=%s\n\t(%s)", cfg.BuildMod, cfg.BuildModReason)626		}627		return module.Version{}, &ImportMissingError{628			Path:                      path,629			QueryErr:                  queryErr,630			modContainingCWD:          ld.MainModules.ModContainingCWD(),631			allowMissingModuleImports: ld.allowMissingModuleImports,632		}633	}634635	// Look up module containing the package, for addition to the build list.636	// Goal is to determine the module, download it to dir,637	// and return m, dir, ImportMissingError.638	fmt.Fprintf(os.Stderr, "go: finding module for package %s\n", path)639640	mg, err := rs.Graph(ld, ctx)641	if err != nil {642		return module.Version{}, err643	}644645	candidates, err := QueryPackages(ld, ctx, path, "latest", mg.Selected, ld.CheckAllowed)646	if err != nil {647		if errors.Is(err, fs.ErrNotExist) {648			// Return "cannot find module providing package […]" instead of whatever649			// low-level error QueryPattern produced.650			return module.Version{}, &ImportMissingError{651				Path:                      path,652				QueryErr:                  err,653				modContainingCWD:          ld.MainModules.ModContainingCWD(),654				allowMissingModuleImports: ld.allowMissingModuleImports,655			}656		} else {657			return module.Version{}, err658		}659	}660661	candidate0MissingVersion := ""662	for i, c := range candidates {663		if v := mg.Selected(c.Mod.Path); gover.ModCompare(c.Mod.Path, v, c.Mod.Version) > 0 {664			// QueryPattern proposed that we add module c.Mod to provide the package,665			// but we already depend on a newer version of that module (and that666			// version doesn't have the package).667			//668			// This typically happens when a package is present at the "@latest"669			// version (e.g., v1.0.0) of a module, but we have a newer version670			// of the same module in the build list (e.g., v1.0.1-beta), and671			// the package is not present there.672			if i == 0 {673				candidate0MissingVersion = v674			}675			continue676		}677		return c.Mod, nil678	}679	return module.Version{}, &ImportMissingError{680		Path:                      path,681		Module:                    candidates[0].Mod,682		newMissingVersion:         candidate0MissingVersion,683		modContainingCWD:          ld.MainModules.ModContainingCWD(),684		allowMissingModuleImports: ld.allowMissingModuleImports,685	}686}687688// maybeInModule reports whether, syntactically,689// a package with the given import path could be supplied690// by a module with the given module path (mpath).691func maybeInModule(path, mpath string) bool {692	return mpath == path ||693		len(path) > len(mpath) && path[len(mpath)] == '/' && path[:len(mpath)] == mpath694}695696var (697	haveGoModCache   par.Cache[string, bool]    // dir → bool698	haveGoFilesCache par.ErrCache[string, bool] // dir → haveGoFiles699)700701// PkgIsInLocalModule reports whether the directory of the package with702// the given pkgpath, exists in the module with the given modpath703// at the given modroot, and contains go source files.704func PkgIsInLocalModule(pkgpath, modpath, modroot string) bool {705	const isLocal = true706	_, haveGoFiles, err := dirInModule(pkgpath, modpath, modroot, isLocal)707	return err == nil && haveGoFiles708}709710// dirInModule locates the directory that would hold the package named by the given path,711// if it were in the module with module path mpath and root mdir.712// If path is syntactically not within mpath,713// or if mdir is a local file tree (isLocal == true) and the directory714// that would hold path is in a sub-module (covered by a go.mod below mdir),715// dirInModule returns "", false, nil.716//717// Otherwise, dirInModule returns the name of the directory where718// Go source files would be expected, along with a boolean indicating719// whether there are in fact Go source files in that directory.720// A non-nil error indicates that the existence of the directory and/or721// source files could not be determined, for example due to a permission error.722//723// TODO(matloob): Could we use the modindex to check packages in indexed modules?724func dirInModule(path, mpath, mdir string, isLocal bool) (dir string, haveGoFiles bool, err error) {725	// Determine where to expect the package.726	if path == mpath {727		dir = mdir728	} else if mpath == "" { // vendor directory729		dir = filepath.Join(mdir, path)730	} else if len(path) > len(mpath) && path[len(mpath)] == '/' && path[:len(mpath)] == mpath {731		dir = filepath.Join(mdir, path[len(mpath)+1:])732	} else {733		return "", false, nil734	}735736	// Check that there aren't other modules in the way.737	// This check is unnecessary inside the module cache738	// and important to skip in the vendor directory,739	// where all the module trees have been overlaid.740	// So we only check local module trees741	// (the main module, and any directory trees pointed at by replace directives).742	if isLocal {743		for d := dir; d != mdir && len(d) > len(mdir); {744			haveGoMod := haveGoModCache.Do(d, func() bool {745				fi, err := fsys.Stat(filepath.Join(d, "go.mod"))746				return err == nil && !fi.IsDir()747			})748749			if haveGoMod {750				return "", false, nil751			}752			parent := filepath.Dir(d)753			if parent == d {754				// Break the loop, as otherwise we'd loop755				// forever if d=="." and mdir=="".756				break757			}758			d = parent759		}760	}761762	// Now committed to returning dir (not "").763764	// Are there Go source files in the directory?765	// We don't care about build tags, not even "go:build ignore".766	// We're just looking for a plausible directory.767	haveGoFiles, err = haveGoFilesCache.Do(dir, func() (bool, error) {768		// modindex.GetPackage will return ErrNotIndexed for any directories which769		// are reached through a symlink, so that they will be handled by770		// fsys.IsGoDir below.771		if ip, err := modindex.GetPackage(mdir, dir); err == nil {772			return ip.IsGoDir()773		} else if !errors.Is(err, modindex.ErrNotIndexed) {774			return false, err775		}776		return fsys.IsGoDir(dir)777	})778779	return dir, haveGoFiles, err780}781782// fetch downloads the given module (or its replacement)783// and returns its location.784//785// The isLocal return value reports whether the replacement,786// if any, is local to the filesystem.787func fetch(ld *Loader, ctx context.Context, mod module.Version) (dir string, isLocal bool, err error) {788	if modRoot := ld.MainModules.ModRoot(mod); modRoot != "" {789		return modRoot, true, nil790	}791	if r := Replacement(ld, mod); r.Path != "" {792		if r.Version == "" {793			dir = r.Path794			if !filepath.IsAbs(dir) {795				dir = filepath.Join(replaceRelativeTo(ld), dir)796			}797			// Ensure that the replacement directory actually exists:798			// dirInModule does not report errors for missing modules,799			// so if we don't report the error now, later failures will be800			// very mysterious.801			if _, err := fsys.Stat(dir); err != nil {802				// TODO(bcmills): We should also read dir/go.mod here and check its Go version,803				// and return a gover.TooNewError if appropriate.804805				if os.IsNotExist(err) {806					// Semantically the module version itself “exists” — we just don't807					// have its source code. Remove the equivalence to os.ErrNotExist,808					// and make the message more concise while we're at it.809					err = fmt.Errorf("replacement directory %s does not exist", r.Path)810				} else {811					err = fmt.Errorf("replacement directory %s: %w", r.Path, err)812				}813				return dir, true, module.VersionError(mod, err)814			}815			return dir, true, nil816		}817		mod = r818	}819820	if mustHaveSums(ld) && !modfetch.HaveSum(ld.Fetcher(), mod) {821		return "", false, module.VersionError(mod, &sumMissingError{})822	}823824	dir, err = ld.Fetcher().Download(ctx, mod)825	return dir, false, err826}827828// mustHaveSums reports whether we require that all checksums829// needed to load or build packages are already present in the go.sum file.830func mustHaveSums(ld *Loader) bool {831	return ld.HasModRoot() && cfg.BuildMod == "readonly" && !ld.inWorkspaceMode()832}833834type sumMissingError struct {835	suggestion string836}837838func (e *sumMissingError) Error() string {839	return "missing go.sum entry" + e.suggestion840}

Findings

✓ No findings reported for this file.

Get this view in your editor

Same data, no extra tab — call code_get_file + code_get_findings over MCP from Claude/Cursor/Copilot.