github.com/bir3/gocompiler@v0.3.205/src/cmd/gocmd/internal/modfetch/coderepo.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 modfetch
     6  
     7  import (
     8  	"archive/zip"
     9  	"bytes"
    10  	"errors"
    11  	"fmt"
    12  	"io"
    13  	"io/fs"
    14  	"os"
    15  	"path"
    16  	"sort"
    17  	"strings"
    18  	"time"
    19  
    20  	"github.com/bir3/gocompiler/src/cmd/gocmd/internal/modfetch/codehost"
    21  
    22  	"github.com/bir3/gocompiler/src/xvendor/golang.org/x/mod/modfile"
    23  	"github.com/bir3/gocompiler/src/xvendor/golang.org/x/mod/module"
    24  	"github.com/bir3/gocompiler/src/xvendor/golang.org/x/mod/semver"
    25  	modzip "github.com/bir3/gocompiler/src/xvendor/golang.org/x/mod/zip"
    26  )
    27  
    28  // A codeRepo implements modfetch.Repo using an underlying codehost.Repo.
    29  type codeRepo struct {
    30  	modPath string
    31  
    32  	// code is the repository containing this module.
    33  	code codehost.Repo
    34  	// codeRoot is the import path at the root of code.
    35  	codeRoot string
    36  	// codeDir is the directory (relative to root) at which we expect to find the module.
    37  	// If pathMajor is non-empty and codeRoot is not the full modPath,
    38  	// then we look in both codeDir and codeDir/pathMajor[1:].
    39  	codeDir string
    40  
    41  	// pathMajor is the suffix of modPath that indicates its major version,
    42  	// or the empty string if modPath is at major version 0 or 1.
    43  	//
    44  	// pathMajor is typically of the form "/vN", but possibly ".vN", or
    45  	// ".vN-unstable" for modules resolved using gopkg.in.
    46  	pathMajor string
    47  	// pathPrefix is the prefix of modPath that excludes pathMajor.
    48  	// It is used only for logging.
    49  	pathPrefix string
    50  
    51  	// pseudoMajor is the major version prefix to require when generating
    52  	// pseudo-versions for this module, derived from the module path. pseudoMajor
    53  	// is empty if the module path does not include a version suffix (that is,
    54  	// accepts either v0 or v1).
    55  	pseudoMajor string
    56  }
    57  
    58  // newCodeRepo returns a Repo that reads the source code for the module with the
    59  // given path, from the repo stored in code, with the root of the repo
    60  // containing the path given by codeRoot.
    61  func newCodeRepo(code codehost.Repo, codeRoot, path string) (Repo, error) {
    62  	if !hasPathPrefix(path, codeRoot) {
    63  		return nil, fmt.Errorf("mismatched repo: found %s for %s", codeRoot, path)
    64  	}
    65  	pathPrefix, pathMajor, ok := module.SplitPathVersion(path)
    66  	if !ok {
    67  		return nil, fmt.Errorf("invalid module path %q", path)
    68  	}
    69  	if codeRoot == path {
    70  		pathPrefix = path
    71  	}
    72  	pseudoMajor := module.PathMajorPrefix(pathMajor)
    73  
    74  	// Compute codeDir = bar, the subdirectory within the repo
    75  	// corresponding to the module root.
    76  	//
    77  	// At this point we might have:
    78  	//	path = github.com/rsc/foo/bar/v2
    79  	//	codeRoot = github.com/rsc/foo
    80  	//	pathPrefix = github.com/rsc/foo/bar
    81  	//	pathMajor = /v2
    82  	//	pseudoMajor = v2
    83  	//
    84  	// which gives
    85  	//	codeDir = bar
    86  	//
    87  	// We know that pathPrefix is a prefix of path, and codeRoot is a prefix of
    88  	// path, but codeRoot may or may not be a prefix of pathPrefix, because
    89  	// codeRoot may be the entire path (in which case codeDir should be empty).
    90  	// That occurs in two situations.
    91  	//
    92  	// One is when a go-import meta tag resolves the complete module path,
    93  	// including the pathMajor suffix:
    94  	//	path = nanomsg.org/go/mangos/v2
    95  	//	codeRoot = nanomsg.org/go/mangos/v2
    96  	//	pathPrefix = nanomsg.org/go/mangos
    97  	//	pathMajor = /v2
    98  	//	pseudoMajor = v2
    99  	//
   100  	// The other is similar: for gopkg.in only, the major version is encoded
   101  	// with a dot rather than a slash, and thus can't be in a subdirectory.
   102  	//	path = gopkg.in/yaml.v2
   103  	//	codeRoot = gopkg.in/yaml.v2
   104  	//	pathPrefix = gopkg.in/yaml
   105  	//	pathMajor = .v2
   106  	//	pseudoMajor = v2
   107  	//
   108  	codeDir := ""
   109  	if codeRoot != path {
   110  		if !hasPathPrefix(pathPrefix, codeRoot) {
   111  			return nil, fmt.Errorf("repository rooted at %s cannot contain module %s", codeRoot, path)
   112  		}
   113  		codeDir = strings.Trim(pathPrefix[len(codeRoot):], "/")
   114  	}
   115  
   116  	r := &codeRepo{
   117  		modPath:     path,
   118  		code:        code,
   119  		codeRoot:    codeRoot,
   120  		codeDir:     codeDir,
   121  		pathPrefix:  pathPrefix,
   122  		pathMajor:   pathMajor,
   123  		pseudoMajor: pseudoMajor,
   124  	}
   125  
   126  	return r, nil
   127  }
   128  
   129  func (r *codeRepo) ModulePath() string {
   130  	return r.modPath
   131  }
   132  
   133  func (r *codeRepo) CheckReuse(old *codehost.Origin) error {
   134  	return r.code.CheckReuse(old, r.codeDir)
   135  }
   136  
   137  func (r *codeRepo) Versions(prefix string) (*Versions, error) {
   138  	// Special case: gopkg.in/macaroon-bakery.v2-unstable
   139  	// does not use the v2 tags (those are for macaroon-bakery.v2).
   140  	// It has no possible tags at all.
   141  	if strings.HasPrefix(r.modPath, "gopkg.in/") && strings.HasSuffix(r.modPath, "-unstable") {
   142  		return &Versions{}, nil
   143  	}
   144  
   145  	p := prefix
   146  	if r.codeDir != "" {
   147  		p = r.codeDir + "/" + p
   148  	}
   149  	tags, err := r.code.Tags(p)
   150  	if err != nil {
   151  		return nil, &module.ModuleError{
   152  			Path: r.modPath,
   153  			Err:  err,
   154  		}
   155  	}
   156  	if tags.Origin != nil {
   157  		tags.Origin.Subdir = r.codeDir
   158  	}
   159  
   160  	var list, incompatible []string
   161  	for _, tag := range tags.List {
   162  		if !strings.HasPrefix(tag.Name, p) {
   163  			continue
   164  		}
   165  		v := tag.Name
   166  		if r.codeDir != "" {
   167  			v = v[len(r.codeDir)+1:]
   168  		}
   169  		// Note: ./codehost/codehost.go's isOriginTag knows about these conditions too.
   170  		// If these are relaxed, isOriginTag will need to be relaxed as well.
   171  		if v == "" || v != semver.Canonical(v) {
   172  			// Ignore non-canonical tags: Stat rewrites those to canonical
   173  			// pseudo-versions. Note that we compare against semver.Canonical here
   174  			// instead of module.CanonicalVersion: revToRev strips "+incompatible"
   175  			// suffixes before looking up tags, so a tag like "v2.0.0+incompatible"
   176  			// would not resolve at all. (The Go version string "v2.0.0+incompatible"
   177  			// refers to the "v2.0.0" version tag, which we handle below.)
   178  			continue
   179  		}
   180  		if module.IsPseudoVersion(v) {
   181  			// Ignore tags that look like pseudo-versions: Stat rewrites those
   182  			// unambiguously to the underlying commit, and tagToVersion drops them.
   183  			continue
   184  		}
   185  
   186  		if err := module.CheckPathMajor(v, r.pathMajor); err != nil {
   187  			if r.codeDir == "" && r.pathMajor == "" && semver.Major(v) > "v1" {
   188  				incompatible = append(incompatible, v)
   189  			}
   190  			continue
   191  		}
   192  
   193  		list = append(list, v)
   194  	}
   195  	semver.Sort(list)
   196  	semver.Sort(incompatible)
   197  
   198  	return r.appendIncompatibleVersions(tags.Origin, list, incompatible)
   199  }
   200  
   201  // appendIncompatibleVersions appends "+incompatible" versions to list if
   202  // appropriate, returning the final list.
   203  //
   204  // The incompatible list contains candidate versions without the '+incompatible'
   205  // prefix.
   206  //
   207  // Both list and incompatible must be sorted in semantic order.
   208  func (r *codeRepo) appendIncompatibleVersions(origin *codehost.Origin, list, incompatible []string) (*Versions, error) {
   209  	versions := &Versions{
   210  		Origin: origin,
   211  		List:   list,
   212  	}
   213  	if len(incompatible) == 0 || r.pathMajor != "" {
   214  		// No +incompatible versions are possible, so no need to check them.
   215  		return versions, nil
   216  	}
   217  
   218  	versionHasGoMod := func(v string) (bool, error) {
   219  		_, err := r.code.ReadFile(v, "go.mod", codehost.MaxGoMod)
   220  		if err == nil {
   221  			return true, nil
   222  		}
   223  		if !os.IsNotExist(err) {
   224  			return false, &module.ModuleError{
   225  				Path: r.modPath,
   226  				Err:  err,
   227  			}
   228  		}
   229  		return false, nil
   230  	}
   231  
   232  	if len(list) > 0 {
   233  		ok, err := versionHasGoMod(list[len(list)-1])
   234  		if err != nil {
   235  			return nil, err
   236  		}
   237  		if ok {
   238  			// The latest compatible version has a go.mod file, so assume that all
   239  			// subsequent versions do as well, and do not include any +incompatible
   240  			// versions. Even if we are wrong, the author clearly intends module
   241  			// consumers to be on the v0/v1 line instead of a higher +incompatible
   242  			// version. (See https://golang.org/issue/34189.)
   243  			//
   244  			// We know of at least two examples where this behavior is desired
   245  			// (github.com/russross/blackfriday@v2.0.0 and
   246  			// github.com/libp2p/go-libp2p@v6.0.23), and (as of 2019-10-29) have no
   247  			// concrete examples for which it is undesired.
   248  			return versions, nil
   249  		}
   250  	}
   251  
   252  	var (
   253  		lastMajor         string
   254  		lastMajorHasGoMod bool
   255  	)
   256  	for i, v := range incompatible {
   257  		major := semver.Major(v)
   258  
   259  		if major != lastMajor {
   260  			rem := incompatible[i:]
   261  			j := sort.Search(len(rem), func(j int) bool {
   262  				return semver.Major(rem[j]) != major
   263  			})
   264  			latestAtMajor := rem[j-1]
   265  
   266  			var err error
   267  			lastMajor = major
   268  			lastMajorHasGoMod, err = versionHasGoMod(latestAtMajor)
   269  			if err != nil {
   270  				return nil, err
   271  			}
   272  		}
   273  
   274  		if lastMajorHasGoMod {
   275  			// The latest release of this major version has a go.mod file, so it is
   276  			// not allowed as +incompatible. It would be confusing to include some
   277  			// minor versions of this major version as +incompatible but require
   278  			// semantic import versioning for others, so drop all +incompatible
   279  			// versions for this major version.
   280  			//
   281  			// If we're wrong about a minor version in the middle, users will still be
   282  			// able to 'go get' specific tags for that version explicitly — they just
   283  			// won't appear in 'go list' or as the results for queries with inequality
   284  			// bounds.
   285  			continue
   286  		}
   287  		versions.List = append(versions.List, v+"+incompatible")
   288  	}
   289  
   290  	return versions, nil
   291  }
   292  
   293  func (r *codeRepo) Stat(rev string) (*RevInfo, error) {
   294  	if rev == "latest" {
   295  		return r.Latest()
   296  	}
   297  	codeRev := r.revToRev(rev)
   298  	info, err := r.code.Stat(codeRev)
   299  	if err != nil {
   300  		// Note: info may be non-nil to supply Origin for caching error.
   301  		var revInfo *RevInfo
   302  		if info != nil {
   303  			revInfo = &RevInfo{
   304  				Origin:  info.Origin,
   305  				Version: rev,
   306  			}
   307  		}
   308  		return revInfo, &module.ModuleError{
   309  			Path: r.modPath,
   310  			Err: &module.InvalidVersionError{
   311  				Version: rev,
   312  				Err:     err,
   313  			},
   314  		}
   315  	}
   316  	return r.convert(info, rev)
   317  }
   318  
   319  func (r *codeRepo) Latest() (*RevInfo, error) {
   320  	info, err := r.code.Latest()
   321  	if err != nil {
   322  		return nil, err
   323  	}
   324  	return r.convert(info, "")
   325  }
   326  
   327  // convert converts a version as reported by the code host to a version as
   328  // interpreted by the module system.
   329  //
   330  // If statVers is a valid module version, it is used for the Version field.
   331  // Otherwise, the Version is derived from the passed-in info and recent tags.
   332  func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, error) {
   333  	// If this is a plain tag (no dir/ prefix)
   334  	// and the module path is unversioned,
   335  	// and if the underlying file tree has no go.mod,
   336  	// then allow using the tag with a +incompatible suffix.
   337  	//
   338  	// (If the version is +incompatible, then the go.mod file must not exist:
   339  	// +incompatible is not an ongoing opt-out from semantic import versioning.)
   340  	incompatibleOk := map[string]bool{}
   341  	canUseIncompatible := func(v string) bool {
   342  		if r.codeDir != "" || r.pathMajor != "" {
   343  			// A non-empty codeDir indicates a module within a subdirectory,
   344  			// which necessarily has a go.mod file indicating the module boundary.
   345  			// A non-empty pathMajor indicates a module path with a major-version
   346  			// suffix, which must match.
   347  			return false
   348  		}
   349  
   350  		ok, seen := incompatibleOk[""]
   351  		if !seen {
   352  			_, errGoMod := r.code.ReadFile(info.Name, "go.mod", codehost.MaxGoMod)
   353  			ok = (errGoMod != nil)
   354  			incompatibleOk[""] = ok
   355  		}
   356  		if !ok {
   357  			// A go.mod file exists at the repo root.
   358  			return false
   359  		}
   360  
   361  		// Per https://go.dev/issue/51324, previous versions of the 'go' command
   362  		// didn't always check for go.mod files in subdirectories, so if the user
   363  		// requests a +incompatible version explicitly, we should continue to allow
   364  		// it. Otherwise, if vN/go.mod exists, expect that release tags for that
   365  		// major version are intended for the vN module.
   366  		if v != "" && !strings.HasSuffix(statVers, "+incompatible") {
   367  			major := semver.Major(v)
   368  			ok, seen = incompatibleOk[major]
   369  			if !seen {
   370  				_, errGoModSub := r.code.ReadFile(info.Name, path.Join(major, "go.mod"), codehost.MaxGoMod)
   371  				ok = (errGoModSub != nil)
   372  				incompatibleOk[major] = ok
   373  			}
   374  			if !ok {
   375  				return false
   376  			}
   377  		}
   378  
   379  		return true
   380  	}
   381  
   382  	// checkCanonical verifies that the canonical version v is compatible with the
   383  	// module path represented by r, adding a "+incompatible" suffix if needed.
   384  	//
   385  	// If statVers is also canonical, checkCanonical also verifies that v is
   386  	// either statVers or statVers with the added "+incompatible" suffix.
   387  	checkCanonical := func(v string) (*RevInfo, error) {
   388  		// If r.codeDir is non-empty, then the go.mod file must exist: the module
   389  		// author — not the module consumer, — gets to decide how to carve up the repo
   390  		// into modules.
   391  		//
   392  		// Conversely, if the go.mod file exists, the module author — not the module
   393  		// consumer — gets to determine the module's path
   394  		//
   395  		// r.findDir verifies both of these conditions. Execute it now so that
   396  		// r.Stat will correctly return a notExistError if the go.mod location or
   397  		// declared module path doesn't match.
   398  		_, _, _, err := r.findDir(v)
   399  		if err != nil {
   400  			// TODO: It would be nice to return an error like "not a module".
   401  			// Right now we return "missing go.mod", which is a little confusing.
   402  			return nil, &module.ModuleError{
   403  				Path: r.modPath,
   404  				Err: &module.InvalidVersionError{
   405  					Version: v,
   406  					Err:     notExistError{err: err},
   407  				},
   408  			}
   409  		}
   410  
   411  		invalidf := func(format string, args ...any) error {
   412  			return &module.ModuleError{
   413  				Path: r.modPath,
   414  				Err: &module.InvalidVersionError{
   415  					Version: v,
   416  					Err:     fmt.Errorf(format, args...),
   417  				},
   418  			}
   419  		}
   420  
   421  		// Add the +incompatible suffix if needed or requested explicitly, and
   422  		// verify that its presence or absence is appropriate for this version
   423  		// (which depends on whether it has an explicit go.mod file).
   424  
   425  		if v == strings.TrimSuffix(statVers, "+incompatible") {
   426  			v = statVers
   427  		}
   428  		base := strings.TrimSuffix(v, "+incompatible")
   429  		var errIncompatible error
   430  		if !module.MatchPathMajor(base, r.pathMajor) {
   431  			if canUseIncompatible(base) {
   432  				v = base + "+incompatible"
   433  			} else {
   434  				if r.pathMajor != "" {
   435  					errIncompatible = invalidf("module path includes a major version suffix, so major version must match")
   436  				} else {
   437  					errIncompatible = invalidf("module contains a go.mod file, so module path must match major version (%q)", path.Join(r.pathPrefix, semver.Major(v)))
   438  				}
   439  			}
   440  		} else if strings.HasSuffix(v, "+incompatible") {
   441  			errIncompatible = invalidf("+incompatible suffix not allowed: major version %s is compatible", semver.Major(v))
   442  		}
   443  
   444  		if statVers != "" && statVers == module.CanonicalVersion(statVers) {
   445  			// Since the caller-requested version is canonical, it would be very
   446  			// confusing to resolve it to anything but itself, possibly with a
   447  			// "+incompatible" suffix. Error out explicitly.
   448  			if statBase := strings.TrimSuffix(statVers, "+incompatible"); statBase != base {
   449  				return nil, &module.ModuleError{
   450  					Path: r.modPath,
   451  					Err: &module.InvalidVersionError{
   452  						Version: statVers,
   453  						Err:     fmt.Errorf("resolves to version %v (%s is not a tag)", v, statBase),
   454  					},
   455  				}
   456  			}
   457  		}
   458  
   459  		if errIncompatible != nil {
   460  			return nil, errIncompatible
   461  		}
   462  
   463  		origin := info.Origin
   464  		if origin != nil {
   465  			o := *origin
   466  			origin = &o
   467  			origin.Subdir = r.codeDir
   468  			if module.IsPseudoVersion(v) && (v != statVers || !strings.HasPrefix(v, "v0.0.0-")) {
   469  				// Add tags that are relevant to pseudo-version calculation to origin.
   470  				prefix := r.codeDir
   471  				if prefix != "" {
   472  					prefix += "/"
   473  				}
   474  				if r.pathMajor != "" { // "/v2" or "/.v2"
   475  					prefix += r.pathMajor[1:] + "." // += "v2."
   476  				}
   477  				tags, err := r.code.Tags(prefix)
   478  				if err != nil {
   479  					return nil, err
   480  				}
   481  				origin.TagPrefix = tags.Origin.TagPrefix
   482  				origin.TagSum = tags.Origin.TagSum
   483  			}
   484  		}
   485  
   486  		return &RevInfo{
   487  			Origin:  origin,
   488  			Name:    info.Name,
   489  			Short:   info.Short,
   490  			Time:    info.Time,
   491  			Version: v,
   492  		}, nil
   493  	}
   494  
   495  	// Determine version.
   496  
   497  	if module.IsPseudoVersion(statVers) {
   498  		if err := r.validatePseudoVersion(info, statVers); err != nil {
   499  			return nil, err
   500  		}
   501  		return checkCanonical(statVers)
   502  	}
   503  
   504  	// statVers is not a pseudo-version, so we need to either resolve it to a
   505  	// canonical version or verify that it is already a canonical tag
   506  	// (not a branch).
   507  
   508  	// Derive or verify a version from a code repo tag.
   509  	// Tag must have a prefix matching codeDir.
   510  	tagPrefix := ""
   511  	if r.codeDir != "" {
   512  		tagPrefix = r.codeDir + "/"
   513  	}
   514  
   515  	isRetracted, err := r.retractedVersions()
   516  	if err != nil {
   517  		isRetracted = func(string) bool { return false }
   518  	}
   519  
   520  	// tagToVersion returns the version obtained by trimming tagPrefix from tag.
   521  	// If the tag is invalid, retracted, or a pseudo-version, tagToVersion returns
   522  	// an empty version.
   523  	tagToVersion := func(tag string) (v string, tagIsCanonical bool) {
   524  		if !strings.HasPrefix(tag, tagPrefix) {
   525  			return "", false
   526  		}
   527  		trimmed := tag[len(tagPrefix):]
   528  		// Tags that look like pseudo-versions would be confusing. Ignore them.
   529  		if module.IsPseudoVersion(tag) {
   530  			return "", false
   531  		}
   532  
   533  		v = semver.Canonical(trimmed) // Not module.Canonical: we don't want to pick up an explicit "+incompatible" suffix from the tag.
   534  		if v == "" || !strings.HasPrefix(trimmed, v) {
   535  			return "", false // Invalid or incomplete version (just vX or vX.Y).
   536  		}
   537  		if v == trimmed {
   538  			tagIsCanonical = true
   539  		}
   540  		return v, tagIsCanonical
   541  	}
   542  
   543  	// If the VCS gave us a valid version, use that.
   544  	if v, tagIsCanonical := tagToVersion(info.Version); tagIsCanonical {
   545  		if info, err := checkCanonical(v); err == nil {
   546  			return info, err
   547  		}
   548  	}
   549  
   550  	// Look through the tags on the revision for either a usable canonical version
   551  	// or an appropriate base for a pseudo-version.
   552  	var (
   553  		highestCanonical string
   554  		pseudoBase       string
   555  	)
   556  	for _, pathTag := range info.Tags {
   557  		v, tagIsCanonical := tagToVersion(pathTag)
   558  		if statVers != "" && semver.Compare(v, statVers) == 0 {
   559  			// The tag is equivalent to the version requested by the user.
   560  			if tagIsCanonical {
   561  				// This tag is the canonical form of the requested version,
   562  				// not some other form with extra build metadata.
   563  				// Use this tag so that the resolved version will match exactly.
   564  				// (If it isn't actually allowed, we'll error out in checkCanonical.)
   565  				return checkCanonical(v)
   566  			} else {
   567  				// The user explicitly requested something equivalent to this tag. We
   568  				// can't use the version from the tag directly: since the tag is not
   569  				// canonical, it could be ambiguous. For example, tags v0.0.1+a and
   570  				// v0.0.1+b might both exist and refer to different revisions.
   571  				//
   572  				// The tag is otherwise valid for the module, so we can at least use it as
   573  				// the base of an unambiguous pseudo-version.
   574  				//
   575  				// If multiple tags match, tagToVersion will canonicalize them to the same
   576  				// base version.
   577  				pseudoBase = v
   578  			}
   579  		}
   580  		// Save the highest non-retracted canonical tag for the revision.
   581  		// If we don't find a better match, we'll use it as the canonical version.
   582  		if tagIsCanonical && semver.Compare(highestCanonical, v) < 0 && !isRetracted(v) {
   583  			if module.MatchPathMajor(v, r.pathMajor) || canUseIncompatible(v) {
   584  				highestCanonical = v
   585  			}
   586  		}
   587  	}
   588  
   589  	// If we found a valid canonical tag for the revision, return it.
   590  	// Even if we found a good pseudo-version base, a canonical version is better.
   591  	if highestCanonical != "" {
   592  		return checkCanonical(highestCanonical)
   593  	}
   594  
   595  	// Find the highest tagged version in the revision's history, subject to
   596  	// major version and +incompatible constraints. Use that version as the
   597  	// pseudo-version base so that the pseudo-version sorts higher. Ignore
   598  	// retracted versions.
   599  	tagAllowed := func(tag string) bool {
   600  		v, _ := tagToVersion(tag)
   601  		if v == "" {
   602  			return false
   603  		}
   604  		if !module.MatchPathMajor(v, r.pathMajor) && !canUseIncompatible(v) {
   605  			return false
   606  		}
   607  		return !isRetracted(v)
   608  	}
   609  	if pseudoBase == "" {
   610  		tag, err := r.code.RecentTag(info.Name, tagPrefix, tagAllowed)
   611  		if err != nil && !errors.Is(err, codehost.ErrUnsupported) {
   612  			return nil, err
   613  		}
   614  		if tag != "" {
   615  			pseudoBase, _ = tagToVersion(tag)
   616  		}
   617  	}
   618  
   619  	return checkCanonical(module.PseudoVersion(r.pseudoMajor, pseudoBase, info.Time, info.Short))
   620  }
   621  
   622  // validatePseudoVersion checks that version has a major version compatible with
   623  // r.modPath and encodes a base version and commit metadata that agrees with
   624  // info.
   625  //
   626  // Note that verifying a nontrivial base version in particular may be somewhat
   627  // expensive: in order to do so, r.code.DescendsFrom will need to fetch at least
   628  // enough of the commit history to find a path between version and its base.
   629  // Fortunately, many pseudo-versions — such as those for untagged repositories —
   630  // have trivial bases!
   631  func (r *codeRepo) validatePseudoVersion(info *codehost.RevInfo, version string) (err error) {
   632  	defer func() {
   633  		if err != nil {
   634  			if _, ok := err.(*module.ModuleError); !ok {
   635  				if _, ok := err.(*module.InvalidVersionError); !ok {
   636  					err = &module.InvalidVersionError{Version: version, Pseudo: true, Err: err}
   637  				}
   638  				err = &module.ModuleError{Path: r.modPath, Err: err}
   639  			}
   640  		}
   641  	}()
   642  
   643  	rev, err := module.PseudoVersionRev(version)
   644  	if err != nil {
   645  		return err
   646  	}
   647  	if rev != info.Short {
   648  		switch {
   649  		case strings.HasPrefix(rev, info.Short):
   650  			return fmt.Errorf("revision is longer than canonical (expected %s)", info.Short)
   651  		case strings.HasPrefix(info.Short, rev):
   652  			return fmt.Errorf("revision is shorter than canonical (expected %s)", info.Short)
   653  		default:
   654  			return fmt.Errorf("does not match short name of revision (expected %s)", info.Short)
   655  		}
   656  	}
   657  
   658  	t, err := module.PseudoVersionTime(version)
   659  	if err != nil {
   660  		return err
   661  	}
   662  	if !t.Equal(info.Time.Truncate(time.Second)) {
   663  		return fmt.Errorf("does not match version-control timestamp (expected %s)", info.Time.UTC().Format(module.PseudoVersionTimestampFormat))
   664  	}
   665  
   666  	tagPrefix := ""
   667  	if r.codeDir != "" {
   668  		tagPrefix = r.codeDir + "/"
   669  	}
   670  
   671  	// A pseudo-version should have a precedence just above its parent revisions,
   672  	// and no higher. Otherwise, it would be possible for library authors to "pin"
   673  	// dependency versions (and bypass the usual minimum version selection) by
   674  	// naming an extremely high pseudo-version rather than an accurate one.
   675  	//
   676  	// Moreover, if we allow a pseudo-version to use any arbitrary pre-release
   677  	// tag, we end up with infinitely many possible names for each commit. Each
   678  	// name consumes resources in the module cache and proxies, so we want to
   679  	// restrict them to a finite set under control of the module author.
   680  	//
   681  	// We address both of these issues by requiring the tag upon which the
   682  	// pseudo-version is based to refer to some ancestor of the revision. We
   683  	// prefer the highest such tag when constructing a new pseudo-version, but do
   684  	// not enforce that property when resolving existing pseudo-versions: we don't
   685  	// know when the parent tags were added, and the highest-tagged parent may not
   686  	// have existed when the pseudo-version was first resolved.
   687  	base, err := module.PseudoVersionBase(strings.TrimSuffix(version, "+incompatible"))
   688  	if err != nil {
   689  		return err
   690  	}
   691  	if base == "" {
   692  		if r.pseudoMajor == "" && semver.Major(version) == "v1" {
   693  			return fmt.Errorf("major version without preceding tag must be v0, not v1")
   694  		}
   695  		return nil
   696  	} else {
   697  		for _, tag := range info.Tags {
   698  			versionOnly := strings.TrimPrefix(tag, tagPrefix)
   699  			if versionOnly == base {
   700  				// The base version is canonical, so if the version from the tag is
   701  				// literally equal (not just equivalent), then the tag is canonical too.
   702  				//
   703  				// We allow pseudo-versions to be derived from non-canonical tags on the
   704  				// same commit, so that tags like "v1.1.0+some-metadata" resolve as
   705  				// close as possible to the canonical version ("v1.1.0") while still
   706  				// enforcing a total ordering ("v1.1.1-0.[…]" with a unique suffix).
   707  				//
   708  				// However, canonical tags already have a total ordering, so there is no
   709  				// reason not to use the canonical tag directly, and we know that the
   710  				// canonical tag must already exist because the pseudo-version is
   711  				// derived from it. In that case, referring to the revision by a
   712  				// pseudo-version derived from its own canonical tag is just confusing.
   713  				return fmt.Errorf("tag (%s) found on revision %s is already canonical, so should not be replaced with a pseudo-version derived from that tag", tag, rev)
   714  			}
   715  		}
   716  	}
   717  
   718  	tags, err := r.code.Tags(tagPrefix + base)
   719  	if err != nil {
   720  		return err
   721  	}
   722  
   723  	var lastTag string // Prefer to log some real tag rather than a canonically-equivalent base.
   724  	ancestorFound := false
   725  	for _, tag := range tags.List {
   726  		versionOnly := strings.TrimPrefix(tag.Name, tagPrefix)
   727  		if semver.Compare(versionOnly, base) == 0 {
   728  			lastTag = tag.Name
   729  			ancestorFound, err = r.code.DescendsFrom(info.Name, tag.Name)
   730  			if ancestorFound {
   731  				break
   732  			}
   733  		}
   734  	}
   735  
   736  	if lastTag == "" {
   737  		return fmt.Errorf("preceding tag (%s) not found", base)
   738  	}
   739  
   740  	if !ancestorFound {
   741  		if err != nil {
   742  			return err
   743  		}
   744  		rev, err := module.PseudoVersionRev(version)
   745  		if err != nil {
   746  			return fmt.Errorf("not a descendent of preceding tag (%s)", lastTag)
   747  		}
   748  		return fmt.Errorf("revision %s is not a descendent of preceding tag (%s)", rev, lastTag)
   749  	}
   750  	return nil
   751  }
   752  
   753  func (r *codeRepo) revToRev(rev string) string {
   754  	if semver.IsValid(rev) {
   755  		if module.IsPseudoVersion(rev) {
   756  			r, _ := module.PseudoVersionRev(rev)
   757  			return r
   758  		}
   759  		if semver.Build(rev) == "+incompatible" {
   760  			rev = rev[:len(rev)-len("+incompatible")]
   761  		}
   762  		if r.codeDir == "" {
   763  			return rev
   764  		}
   765  		return r.codeDir + "/" + rev
   766  	}
   767  	return rev
   768  }
   769  
   770  func (r *codeRepo) versionToRev(version string) (rev string, err error) {
   771  	if !semver.IsValid(version) {
   772  		return "", &module.ModuleError{
   773  			Path: r.modPath,
   774  			Err: &module.InvalidVersionError{
   775  				Version: version,
   776  				Err:     errors.New("syntax error"),
   777  			},
   778  		}
   779  	}
   780  	return r.revToRev(version), nil
   781  }
   782  
   783  // findDir locates the directory within the repo containing the module.
   784  //
   785  // If r.pathMajor is non-empty, this can be either r.codeDir or — if a go.mod
   786  // file exists — r.codeDir/r.pathMajor[1:].
   787  func (r *codeRepo) findDir(version string) (rev, dir string, gomod []byte, err error) {
   788  	rev, err = r.versionToRev(version)
   789  	if err != nil {
   790  		return "", "", nil, err
   791  	}
   792  
   793  	// Load info about go.mod but delay consideration
   794  	// (except I/O error) until we rule out v2/go.mod.
   795  	file1 := path.Join(r.codeDir, "go.mod")
   796  	gomod1, err1 := r.code.ReadFile(rev, file1, codehost.MaxGoMod)
   797  	if err1 != nil && !os.IsNotExist(err1) {
   798  		return "", "", nil, fmt.Errorf("reading %s/%s at revision %s: %v", r.codeRoot, file1, rev, err1)
   799  	}
   800  	mpath1 := modfile.ModulePath(gomod1)
   801  	found1 := err1 == nil && (isMajor(mpath1, r.pathMajor) || r.canReplaceMismatchedVersionDueToBug(mpath1))
   802  
   803  	var file2 string
   804  	if r.pathMajor != "" && r.codeRoot != r.modPath && !strings.HasPrefix(r.pathMajor, ".") {
   805  		// Suppose pathMajor is "/v2".
   806  		// Either go.mod should claim v2 and v2/go.mod should not exist,
   807  		// or v2/go.mod should exist and claim v2. Not both.
   808  		// Note that we don't check the full path, just the major suffix,
   809  		// because of replacement modules. This might be a fork of
   810  		// the real module, found at a different path, usable only in
   811  		// a replace directive.
   812  		dir2 := path.Join(r.codeDir, r.pathMajor[1:])
   813  		file2 = path.Join(dir2, "go.mod")
   814  		gomod2, err2 := r.code.ReadFile(rev, file2, codehost.MaxGoMod)
   815  		if err2 != nil && !os.IsNotExist(err2) {
   816  			return "", "", nil, fmt.Errorf("reading %s/%s at revision %s: %v", r.codeRoot, file2, rev, err2)
   817  		}
   818  		mpath2 := modfile.ModulePath(gomod2)
   819  		found2 := err2 == nil && isMajor(mpath2, r.pathMajor)
   820  
   821  		if found1 && found2 {
   822  			return "", "", nil, fmt.Errorf("%s/%s and ...%s/go.mod both have ...%s module paths at revision %s", r.pathPrefix, file1, r.pathMajor, r.pathMajor, rev)
   823  		}
   824  		if found2 {
   825  			return rev, dir2, gomod2, nil
   826  		}
   827  		if err2 == nil {
   828  			if mpath2 == "" {
   829  				return "", "", nil, fmt.Errorf("%s/%s is missing module path at revision %s", r.codeRoot, file2, rev)
   830  			}
   831  			return "", "", nil, fmt.Errorf("%s/%s has non-...%s module path %q at revision %s", r.codeRoot, file2, r.pathMajor, mpath2, rev)
   832  		}
   833  	}
   834  
   835  	// Not v2/go.mod, so it's either go.mod or nothing. Which is it?
   836  	if found1 {
   837  		// Explicit go.mod with matching major version ok.
   838  		return rev, r.codeDir, gomod1, nil
   839  	}
   840  	if err1 == nil {
   841  		// Explicit go.mod with non-matching major version disallowed.
   842  		suffix := ""
   843  		if file2 != "" {
   844  			suffix = fmt.Sprintf(" (and ...%s/go.mod does not exist)", r.pathMajor)
   845  		}
   846  		if mpath1 == "" {
   847  			return "", "", nil, fmt.Errorf("%s is missing module path%s at revision %s", file1, suffix, rev)
   848  		}
   849  		if r.pathMajor != "" { // ".v1", ".v2" for gopkg.in
   850  			return "", "", nil, fmt.Errorf("%s has non-...%s module path %q%s at revision %s", file1, r.pathMajor, mpath1, suffix, rev)
   851  		}
   852  		if _, _, ok := module.SplitPathVersion(mpath1); !ok {
   853  			return "", "", nil, fmt.Errorf("%s has malformed module path %q%s at revision %s", file1, mpath1, suffix, rev)
   854  		}
   855  		return "", "", nil, fmt.Errorf("%s has post-%s module path %q%s at revision %s", file1, semver.Major(version), mpath1, suffix, rev)
   856  	}
   857  
   858  	if r.codeDir == "" && (r.pathMajor == "" || strings.HasPrefix(r.pathMajor, ".")) {
   859  		// Implicit go.mod at root of repo OK for v0/v1 and for gopkg.in.
   860  		return rev, "", nil, nil
   861  	}
   862  
   863  	// Implicit go.mod below root of repo or at v2+ disallowed.
   864  	// Be clear about possibility of using either location for v2+.
   865  	if file2 != "" {
   866  		return "", "", nil, fmt.Errorf("missing %s/go.mod and ...%s/go.mod at revision %s", r.pathPrefix, r.pathMajor, rev)
   867  	}
   868  	return "", "", nil, fmt.Errorf("missing %s/go.mod at revision %s", r.pathPrefix, rev)
   869  }
   870  
   871  // isMajor reports whether the versions allowed for mpath are compatible with
   872  // the major version(s) implied by pathMajor, or false if mpath has an invalid
   873  // version suffix.
   874  func isMajor(mpath, pathMajor string) bool {
   875  	if mpath == "" {
   876  		// If we don't have a path, we don't know what version(s) it is compatible with.
   877  		return false
   878  	}
   879  	_, mpathMajor, ok := module.SplitPathVersion(mpath)
   880  	if !ok {
   881  		// An invalid module path is not compatible with any version.
   882  		return false
   883  	}
   884  	if pathMajor == "" {
   885  		// All of the valid versions for a gopkg.in module that requires major
   886  		// version v0 or v1 are compatible with the "v0 or v1" implied by an empty
   887  		// pathMajor.
   888  		switch module.PathMajorPrefix(mpathMajor) {
   889  		case "", "v0", "v1":
   890  			return true
   891  		default:
   892  			return false
   893  		}
   894  	}
   895  	if mpathMajor == "" {
   896  		// Even if pathMajor is ".v0" or ".v1", we can't be sure that a module
   897  		// without a suffix is tagged appropriately. Besides, we don't expect clones
   898  		// of non-gopkg.in modules to have gopkg.in paths, so a non-empty,
   899  		// non-gopkg.in mpath is probably the wrong module for any such pathMajor
   900  		// anyway.
   901  		return false
   902  	}
   903  	// If both pathMajor and mpathMajor are non-empty, then we only care that they
   904  	// have the same major-version validation rules. A clone fetched via a /v2
   905  	// path might replace a module with path gopkg.in/foo.v2-unstable, and that's
   906  	// ok.
   907  	return pathMajor[1:] == mpathMajor[1:]
   908  }
   909  
   910  // canReplaceMismatchedVersionDueToBug reports whether versions of r
   911  // could replace versions of mpath with otherwise-mismatched major versions
   912  // due to a historical bug in the Go command (golang.org/issue/34254).
   913  func (r *codeRepo) canReplaceMismatchedVersionDueToBug(mpath string) bool {
   914  	// The bug caused us to erroneously accept unversioned paths as replacements
   915  	// for versioned gopkg.in paths.
   916  	unversioned := r.pathMajor == ""
   917  	replacingGopkgIn := strings.HasPrefix(mpath, "gopkg.in/")
   918  	return unversioned && replacingGopkgIn
   919  }
   920  
   921  func (r *codeRepo) GoMod(version string) (data []byte, err error) {
   922  	if version != module.CanonicalVersion(version) {
   923  		return nil, fmt.Errorf("version %s is not canonical", version)
   924  	}
   925  
   926  	if module.IsPseudoVersion(version) {
   927  		// findDir ignores the metadata encoded in a pseudo-version,
   928  		// only using the revision at the end.
   929  		// Invoke Stat to verify the metadata explicitly so we don't return
   930  		// a bogus file for an invalid version.
   931  		_, err := r.Stat(version)
   932  		if err != nil {
   933  			return nil, err
   934  		}
   935  	}
   936  
   937  	rev, dir, gomod, err := r.findDir(version)
   938  	if err != nil {
   939  		return nil, err
   940  	}
   941  	if gomod != nil {
   942  		return gomod, nil
   943  	}
   944  	data, err = r.code.ReadFile(rev, path.Join(dir, "go.mod"), codehost.MaxGoMod)
   945  	if err != nil {
   946  		if os.IsNotExist(err) {
   947  			return LegacyGoMod(r.modPath), nil
   948  		}
   949  		return nil, err
   950  	}
   951  	return data, nil
   952  }
   953  
   954  // LegacyGoMod generates a fake go.mod file for a module that doesn't have one.
   955  // The go.mod file contains a module directive and nothing else: no go version,
   956  // no requirements.
   957  //
   958  // We used to try to build a go.mod reflecting pre-existing
   959  // package management metadata files, but the conversion
   960  // was inherently imperfect (because those files don't have
   961  // exactly the same semantics as go.mod) and, when done
   962  // for dependencies in the middle of a build, impossible to
   963  // correct. So we stopped.
   964  func LegacyGoMod(modPath string) []byte {
   965  	return fmt.Appendf(nil, "module %s\n", modfile.AutoQuote(modPath))
   966  }
   967  
   968  func (r *codeRepo) modPrefix(rev string) string {
   969  	return r.modPath + "@" + rev
   970  }
   971  
   972  func (r *codeRepo) retractedVersions() (func(string) bool, error) {
   973  	vs, err := r.Versions("")
   974  	if err != nil {
   975  		return nil, err
   976  	}
   977  	versions := vs.List
   978  
   979  	for i, v := range versions {
   980  		if strings.HasSuffix(v, "+incompatible") {
   981  			// We're looking for the latest release tag that may list retractions in a
   982  			// go.mod file. +incompatible versions necessarily do not, and they start
   983  			// at major version 2 — which is higher than any version that could
   984  			// validly contain a go.mod file.
   985  			versions = versions[:i]
   986  			break
   987  		}
   988  	}
   989  	if len(versions) == 0 {
   990  		return func(string) bool { return false }, nil
   991  	}
   992  
   993  	var highest string
   994  	for i := len(versions) - 1; i >= 0; i-- {
   995  		v := versions[i]
   996  		if semver.Prerelease(v) == "" {
   997  			highest = v
   998  			break
   999  		}
  1000  	}
  1001  	if highest == "" {
  1002  		highest = versions[len(versions)-1]
  1003  	}
  1004  
  1005  	data, err := r.GoMod(highest)
  1006  	if err != nil {
  1007  		return nil, err
  1008  	}
  1009  	f, err := modfile.ParseLax("go.mod", data, nil)
  1010  	if err != nil {
  1011  		return nil, err
  1012  	}
  1013  	retractions := make([]modfile.VersionInterval, len(f.Retract))
  1014  	for _, r := range f.Retract {
  1015  		retractions = append(retractions, r.VersionInterval)
  1016  	}
  1017  
  1018  	return func(v string) bool {
  1019  		for _, r := range retractions {
  1020  			if semver.Compare(r.Low, v) <= 0 && semver.Compare(v, r.High) <= 0 {
  1021  				return true
  1022  			}
  1023  		}
  1024  		return false
  1025  	}, nil
  1026  }
  1027  
  1028  func (r *codeRepo) Zip(dst io.Writer, version string) error {
  1029  	if version != module.CanonicalVersion(version) {
  1030  		return fmt.Errorf("version %s is not canonical", version)
  1031  	}
  1032  
  1033  	if module.IsPseudoVersion(version) {
  1034  		// findDir ignores the metadata encoded in a pseudo-version,
  1035  		// only using the revision at the end.
  1036  		// Invoke Stat to verify the metadata explicitly so we don't return
  1037  		// a bogus file for an invalid version.
  1038  		_, err := r.Stat(version)
  1039  		if err != nil {
  1040  			return err
  1041  		}
  1042  	}
  1043  
  1044  	rev, subdir, _, err := r.findDir(version)
  1045  	if err != nil {
  1046  		return err
  1047  	}
  1048  	dl, err := r.code.ReadZip(rev, subdir, codehost.MaxZipFile)
  1049  	if err != nil {
  1050  		return err
  1051  	}
  1052  	defer dl.Close()
  1053  	subdir = strings.Trim(subdir, "/")
  1054  
  1055  	// Spool to local file.
  1056  	f, err := os.CreateTemp("", "go-codehost-")
  1057  	if err != nil {
  1058  		dl.Close()
  1059  		return err
  1060  	}
  1061  	defer os.Remove(f.Name())
  1062  	defer f.Close()
  1063  	maxSize := int64(codehost.MaxZipFile)
  1064  	lr := &io.LimitedReader{R: dl, N: maxSize + 1}
  1065  	if _, err := io.Copy(f, lr); err != nil {
  1066  		dl.Close()
  1067  		return err
  1068  	}
  1069  	dl.Close()
  1070  	if lr.N <= 0 {
  1071  		return fmt.Errorf("downloaded zip file too large")
  1072  	}
  1073  	size := (maxSize + 1) - lr.N
  1074  	if _, err := f.Seek(0, 0); err != nil {
  1075  		return err
  1076  	}
  1077  
  1078  	// Translate from zip file we have to zip file we want.
  1079  	zr, err := zip.NewReader(f, size)
  1080  	if err != nil {
  1081  		return err
  1082  	}
  1083  
  1084  	var files []modzip.File
  1085  	if subdir != "" {
  1086  		subdir += "/"
  1087  	}
  1088  	haveLICENSE := false
  1089  	topPrefix := ""
  1090  	for _, zf := range zr.File {
  1091  		if topPrefix == "" {
  1092  			i := strings.Index(zf.Name, "/")
  1093  			if i < 0 {
  1094  				return fmt.Errorf("missing top-level directory prefix")
  1095  			}
  1096  			topPrefix = zf.Name[:i+1]
  1097  		}
  1098  		var name string
  1099  		var found bool
  1100  		if name, found = strings.CutPrefix(zf.Name, topPrefix); !found {
  1101  			return fmt.Errorf("zip file contains more than one top-level directory")
  1102  		}
  1103  
  1104  		if name, found = strings.CutPrefix(name, subdir); !found {
  1105  			continue
  1106  		}
  1107  
  1108  		if name == "" || strings.HasSuffix(name, "/") {
  1109  			continue
  1110  		}
  1111  		files = append(files, zipFile{name: name, f: zf})
  1112  		if name == "LICENSE" {
  1113  			haveLICENSE = true
  1114  		}
  1115  	}
  1116  
  1117  	if !haveLICENSE && subdir != "" {
  1118  		data, err := r.code.ReadFile(rev, "LICENSE", codehost.MaxLICENSE)
  1119  		if err == nil {
  1120  			files = append(files, dataFile{name: "LICENSE", data: data})
  1121  		}
  1122  	}
  1123  
  1124  	return modzip.Create(dst, module.Version{Path: r.modPath, Version: version}, files)
  1125  }
  1126  
  1127  type zipFile struct {
  1128  	name string
  1129  	f    *zip.File
  1130  }
  1131  
  1132  func (f zipFile) Path() string                 { return f.name }
  1133  func (f zipFile) Lstat() (fs.FileInfo, error)  { return f.f.FileInfo(), nil }
  1134  func (f zipFile) Open() (io.ReadCloser, error) { return f.f.Open() }
  1135  
  1136  type dataFile struct {
  1137  	name string
  1138  	data []byte
  1139  }
  1140  
  1141  func (f dataFile) Path() string                { return f.name }
  1142  func (f dataFile) Lstat() (fs.FileInfo, error) { return dataFileInfo{f}, nil }
  1143  func (f dataFile) Open() (io.ReadCloser, error) {
  1144  	return io.NopCloser(bytes.NewReader(f.data)), nil
  1145  }
  1146  
  1147  type dataFileInfo struct {
  1148  	f dataFile
  1149  }
  1150  
  1151  func (fi dataFileInfo) Name() string       { return path.Base(fi.f.name) }
  1152  func (fi dataFileInfo) Size() int64        { return int64(len(fi.f.data)) }
  1153  func (fi dataFileInfo) Mode() fs.FileMode  { return 0644 }
  1154  func (fi dataFileInfo) ModTime() time.Time { return time.Time{} }
  1155  func (fi dataFileInfo) IsDir() bool        { return false }
  1156  func (fi dataFileInfo) Sys() any           { return nil }
  1157  
  1158  // hasPathPrefix reports whether the path s begins with the
  1159  // elements in prefix.
  1160  func hasPathPrefix(s, prefix string) bool {
  1161  	switch {
  1162  	default:
  1163  		return false
  1164  	case len(s) == len(prefix):
  1165  		return s == prefix
  1166  	case len(s) > len(prefix):
  1167  		if prefix != "" && prefix[len(prefix)-1] == '/' {
  1168  			return strings.HasPrefix(s, prefix)
  1169  		}
  1170  		return s[len(prefix)] == '/' && s[:len(prefix)] == prefix
  1171  	}
  1172  }