github.com/gagliardetto/golang-go@v0.0.0-20201020153340-53909ea70814/cmd/go/not-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/ioutil"
    14  	"os"
    15  	"path"
    16  	"sort"
    17  	"strings"
    18  	"time"
    19  
    20  	"github.com/gagliardetto/golang-go/cmd/go/not-internal/modfetch/codehost"
    21  
    22  	"golang.org/x/mod/modfile"
    23  	"golang.org/x/mod/module"
    24  	"golang.org/x/mod/semver"
    25  	modzip "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) Versions(prefix string) ([]string, error) {
   134  	// Special case: gopkg.in/macaroon-bakery.v2-unstable
   135  	// does not use the v2 tags (those are for macaroon-bakery.v2).
   136  	// It has no possible tags at all.
   137  	if strings.HasPrefix(r.modPath, "gopkg.in/") && strings.HasSuffix(r.modPath, "-unstable") {
   138  		return nil, nil
   139  	}
   140  
   141  	p := prefix
   142  	if r.codeDir != "" {
   143  		p = r.codeDir + "/" + p
   144  	}
   145  	tags, err := r.code.Tags(p)
   146  	if err != nil {
   147  		return nil, &module.ModuleError{
   148  			Path: r.modPath,
   149  			Err:  err,
   150  		}
   151  	}
   152  
   153  	var list, incompatible []string
   154  	for _, tag := range tags {
   155  		if !strings.HasPrefix(tag, p) {
   156  			continue
   157  		}
   158  		v := tag
   159  		if r.codeDir != "" {
   160  			v = v[len(r.codeDir)+1:]
   161  		}
   162  		if v == "" || v != module.CanonicalVersion(v) || IsPseudoVersion(v) {
   163  			continue
   164  		}
   165  
   166  		if err := module.CheckPathMajor(v, r.pathMajor); err != nil {
   167  			if r.codeDir == "" && r.pathMajor == "" && semver.Major(v) > "v1" {
   168  				incompatible = append(incompatible, v)
   169  			}
   170  			continue
   171  		}
   172  
   173  		list = append(list, v)
   174  	}
   175  	SortVersions(list)
   176  	SortVersions(incompatible)
   177  
   178  	return r.appendIncompatibleVersions(list, incompatible)
   179  }
   180  
   181  // appendIncompatibleVersions appends "+incompatible" versions to list if
   182  // appropriate, returning the final list.
   183  //
   184  // The incompatible list contains candidate versions without the '+incompatible'
   185  // prefix.
   186  //
   187  // Both list and incompatible must be sorted in semantic order.
   188  func (r *codeRepo) appendIncompatibleVersions(list, incompatible []string) ([]string, error) {
   189  	if len(incompatible) == 0 || r.pathMajor != "" {
   190  		// No +incompatible versions are possible, so no need to check them.
   191  		return list, nil
   192  	}
   193  
   194  	versionHasGoMod := func(v string) (bool, error) {
   195  		_, err := r.code.ReadFile(v, "go.mod", codehost.MaxGoMod)
   196  		if err == nil {
   197  			return true, nil
   198  		}
   199  		if !os.IsNotExist(err) {
   200  			return false, &module.ModuleError{
   201  				Path: r.modPath,
   202  				Err:  err,
   203  			}
   204  		}
   205  		return false, nil
   206  	}
   207  
   208  	if len(list) > 0 {
   209  		ok, err := versionHasGoMod(list[len(list)-1])
   210  		if err != nil {
   211  			return nil, err
   212  		}
   213  		if ok {
   214  			// The latest compatible version has a go.mod file, so assume that all
   215  			// subsequent versions do as well, and do not include any +incompatible
   216  			// versions. Even if we are wrong, the author clearly intends module
   217  			// consumers to be on the v0/v1 line instead of a higher +incompatible
   218  			// version. (See https://golang.org/issue/34189.)
   219  			//
   220  			// We know of at least two examples where this behavior is desired
   221  			// (github.com/russross/blackfriday@v2.0.0 and
   222  			// github.com/libp2p/go-libp2p@v6.0.23), and (as of 2019-10-29) have no
   223  			// concrete examples for which it is undesired.
   224  			return list, nil
   225  		}
   226  	}
   227  
   228  	var (
   229  		lastMajor         string
   230  		lastMajorHasGoMod bool
   231  	)
   232  	for i, v := range incompatible {
   233  		major := semver.Major(v)
   234  
   235  		if major != lastMajor {
   236  			rem := incompatible[i:]
   237  			j := sort.Search(len(rem), func(j int) bool {
   238  				return semver.Major(rem[j]) != major
   239  			})
   240  			latestAtMajor := rem[j-1]
   241  
   242  			var err error
   243  			lastMajor = major
   244  			lastMajorHasGoMod, err = versionHasGoMod(latestAtMajor)
   245  			if err != nil {
   246  				return nil, err
   247  			}
   248  		}
   249  
   250  		if lastMajorHasGoMod {
   251  			// The latest release of this major version has a go.mod file, so it is
   252  			// not allowed as +incompatible. It would be confusing to include some
   253  			// minor versions of this major version as +incompatible but require
   254  			// semantic import versioning for others, so drop all +incompatible
   255  			// versions for this major version.
   256  			//
   257  			// If we're wrong about a minor version in the middle, users will still be
   258  			// able to 'go get' specific tags for that version explicitly — they just
   259  			// won't appear in 'go list' or as the results for queries with inequality
   260  			// bounds.
   261  			continue
   262  		}
   263  		list = append(list, v+"+incompatible")
   264  	}
   265  
   266  	return list, nil
   267  }
   268  
   269  func (r *codeRepo) Stat(rev string) (*RevInfo, error) {
   270  	if rev == "latest" {
   271  		return r.Latest()
   272  	}
   273  	codeRev := r.revToRev(rev)
   274  	info, err := r.code.Stat(codeRev)
   275  	if err != nil {
   276  		return nil, &module.ModuleError{
   277  			Path: r.modPath,
   278  			Err: &module.InvalidVersionError{
   279  				Version: rev,
   280  				Err:     err,
   281  			},
   282  		}
   283  	}
   284  	return r.convert(info, rev)
   285  }
   286  
   287  func (r *codeRepo) Latest() (*RevInfo, error) {
   288  	info, err := r.code.Latest()
   289  	if err != nil {
   290  		return nil, err
   291  	}
   292  	return r.convert(info, "")
   293  }
   294  
   295  // convert converts a version as reported by the code host to a version as
   296  // interpreted by the module system.
   297  //
   298  // If statVers is a valid module version, it is used for the Version field.
   299  // Otherwise, the Version is derived from the passed-in info and recent tags.
   300  func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, error) {
   301  	info2 := &RevInfo{
   302  		Name:  info.Name,
   303  		Short: info.Short,
   304  		Time:  info.Time,
   305  	}
   306  
   307  	// If this is a plain tag (no dir/ prefix)
   308  	// and the module path is unversioned,
   309  	// and if the underlying file tree has no go.mod,
   310  	// then allow using the tag with a +incompatible suffix.
   311  	var canUseIncompatible func() bool
   312  	canUseIncompatible = func() bool {
   313  		var ok bool
   314  		if r.codeDir == "" && r.pathMajor == "" {
   315  			_, errGoMod := r.code.ReadFile(info.Name, "go.mod", codehost.MaxGoMod)
   316  			if errGoMod != nil {
   317  				ok = true
   318  			}
   319  		}
   320  		canUseIncompatible = func() bool { return ok }
   321  		return ok
   322  	}
   323  
   324  	invalidf := func(format string, args ...interface{}) error {
   325  		return &module.ModuleError{
   326  			Path: r.modPath,
   327  			Err: &module.InvalidVersionError{
   328  				Version: info2.Version,
   329  				Err:     fmt.Errorf(format, args...),
   330  			},
   331  		}
   332  	}
   333  
   334  	// checkGoMod verifies that the go.mod file for the module exists or does not
   335  	// exist as required by info2.Version and the module path represented by r.
   336  	checkGoMod := func() (*RevInfo, error) {
   337  		// If r.codeDir is non-empty, then the go.mod file must exist: the module
   338  		// author — not the module consumer, — gets to decide how to carve up the repo
   339  		// into modules.
   340  		//
   341  		// Conversely, if the go.mod file exists, the module author — not the module
   342  		// consumer — gets to determine the module's path
   343  		//
   344  		// r.findDir verifies both of these conditions. Execute it now so that
   345  		// r.Stat will correctly return a notExistError if the go.mod location or
   346  		// declared module path doesn't match.
   347  		_, _, _, err := r.findDir(info2.Version)
   348  		if err != nil {
   349  			// TODO: It would be nice to return an error like "not a module".
   350  			// Right now we return "missing go.mod", which is a little confusing.
   351  			return nil, &module.ModuleError{
   352  				Path: r.modPath,
   353  				Err: &module.InvalidVersionError{
   354  					Version: info2.Version,
   355  					Err:     notExistError{err: err},
   356  				},
   357  			}
   358  		}
   359  
   360  		// If the version is +incompatible, then the go.mod file must not exist:
   361  		// +incompatible is not an ongoing opt-out from semantic import versioning.
   362  		if strings.HasSuffix(info2.Version, "+incompatible") {
   363  			if !canUseIncompatible() {
   364  				if r.pathMajor != "" {
   365  					return nil, invalidf("+incompatible suffix not allowed: module path includes a major version suffix, so major version must match")
   366  				} else {
   367  					return nil, invalidf("+incompatible suffix not allowed: module contains a go.mod file, so semantic import versioning is required")
   368  				}
   369  			}
   370  
   371  			if err := module.CheckPathMajor(strings.TrimSuffix(info2.Version, "+incompatible"), r.pathMajor); err == nil {
   372  				return nil, invalidf("+incompatible suffix not allowed: major version %s is compatible", semver.Major(info2.Version))
   373  			}
   374  		}
   375  
   376  		return info2, nil
   377  	}
   378  
   379  	// Determine version.
   380  	//
   381  	// If statVers is canonical, then the original call was repo.Stat(statVers).
   382  	// Since the version is canonical, we must not resolve it to anything but
   383  	// itself, possibly with a '+incompatible' annotation: we do not need to do
   384  	// the work required to look for an arbitrary pseudo-version.
   385  	if statVers != "" && statVers == module.CanonicalVersion(statVers) {
   386  		info2.Version = statVers
   387  
   388  		if IsPseudoVersion(info2.Version) {
   389  			if err := r.validatePseudoVersion(info, info2.Version); err != nil {
   390  				return nil, err
   391  			}
   392  			return checkGoMod()
   393  		}
   394  
   395  		if err := module.CheckPathMajor(info2.Version, r.pathMajor); err != nil {
   396  			if canUseIncompatible() {
   397  				info2.Version += "+incompatible"
   398  				return checkGoMod()
   399  			} else {
   400  				if vErr, ok := err.(*module.InvalidVersionError); ok {
   401  					// We're going to describe why the version is invalid in more detail,
   402  					// so strip out the existing “invalid version” wrapper.
   403  					err = vErr.Err
   404  				}
   405  				return nil, invalidf("module contains a go.mod file, so major version must be compatible: %v", err)
   406  			}
   407  		}
   408  
   409  		return checkGoMod()
   410  	}
   411  
   412  	// statVers is empty or non-canonical, so we need to resolve it to a canonical
   413  	// version or pseudo-version.
   414  
   415  	// Derive or verify a version from a code repo tag.
   416  	// Tag must have a prefix matching codeDir.
   417  	tagPrefix := ""
   418  	if r.codeDir != "" {
   419  		tagPrefix = r.codeDir + "/"
   420  	}
   421  
   422  	// tagToVersion returns the version obtained by trimming tagPrefix from tag.
   423  	// If the tag is invalid or a pseudo-version, tagToVersion returns an empty
   424  	// version.
   425  	tagToVersion := func(tag string) (v string, tagIsCanonical bool) {
   426  		if !strings.HasPrefix(tag, tagPrefix) {
   427  			return "", false
   428  		}
   429  		trimmed := tag[len(tagPrefix):]
   430  		// Tags that look like pseudo-versions would be confusing. Ignore them.
   431  		if IsPseudoVersion(tag) {
   432  			return "", false
   433  		}
   434  
   435  		v = semver.Canonical(trimmed) // Not module.Canonical: we don't want to pick up an explicit "+incompatible" suffix from the tag.
   436  		if v == "" || !strings.HasPrefix(trimmed, v) {
   437  			return "", false // Invalid or incomplete version (just vX or vX.Y).
   438  		}
   439  		if v == trimmed {
   440  			tagIsCanonical = true
   441  		}
   442  
   443  		if err := module.CheckPathMajor(v, r.pathMajor); err != nil {
   444  			if canUseIncompatible() {
   445  				return v + "+incompatible", tagIsCanonical
   446  			}
   447  			return "", false
   448  		}
   449  
   450  		return v, tagIsCanonical
   451  	}
   452  
   453  	// If the VCS gave us a valid version, use that.
   454  	if v, tagIsCanonical := tagToVersion(info.Version); tagIsCanonical {
   455  		info2.Version = v
   456  		return checkGoMod()
   457  	}
   458  
   459  	// Look through the tags on the revision for either a usable canonical version
   460  	// or an appropriate base for a pseudo-version.
   461  	var pseudoBase string
   462  	for _, pathTag := range info.Tags {
   463  		v, tagIsCanonical := tagToVersion(pathTag)
   464  		if tagIsCanonical {
   465  			if statVers != "" && semver.Compare(v, statVers) == 0 {
   466  				// The user requested a non-canonical version, but the tag for the
   467  				// canonical equivalent refers to the same revision. Use it.
   468  				info2.Version = v
   469  				return checkGoMod()
   470  			} else {
   471  				// Save the highest canonical tag for the revision. If we don't find a
   472  				// better match, we'll use it as the canonical version.
   473  				//
   474  				// NOTE: Do not replace this with semver.Max. Despite the name,
   475  				// semver.Max *also* canonicalizes its arguments, which uses
   476  				// semver.Canonical instead of module.CanonicalVersion and thereby
   477  				// strips our "+incompatible" suffix.
   478  				if semver.Compare(info2.Version, v) < 0 {
   479  					info2.Version = v
   480  				}
   481  			}
   482  		} else if v != "" && semver.Compare(v, statVers) == 0 {
   483  			// The user explicitly requested something equivalent to this tag. We
   484  			// can't use the version from the tag directly: since the tag is not
   485  			// canonical, it could be ambiguous. For example, tags v0.0.1+a and
   486  			// v0.0.1+b might both exist and refer to different revisions.
   487  			//
   488  			// The tag is otherwise valid for the module, so we can at least use it as
   489  			// the base of an unambiguous pseudo-version.
   490  			//
   491  			// If multiple tags match, tagToVersion will canonicalize them to the same
   492  			// base version.
   493  			pseudoBase = v
   494  		}
   495  	}
   496  
   497  	// If we found any canonical tag for the revision, return it.
   498  	// Even if we found a good pseudo-version base, a canonical version is better.
   499  	if info2.Version != "" {
   500  		return checkGoMod()
   501  	}
   502  
   503  	if pseudoBase == "" {
   504  		var tag string
   505  		if r.pseudoMajor != "" || canUseIncompatible() {
   506  			tag, _ = r.code.RecentTag(info.Name, tagPrefix, r.pseudoMajor)
   507  		} else {
   508  			// Allow either v1 or v0, but not incompatible higher versions.
   509  			tag, _ = r.code.RecentTag(info.Name, tagPrefix, "v1")
   510  			if tag == "" {
   511  				tag, _ = r.code.RecentTag(info.Name, tagPrefix, "v0")
   512  			}
   513  		}
   514  		pseudoBase, _ = tagToVersion(tag) // empty if the tag is invalid
   515  	}
   516  
   517  	info2.Version = PseudoVersion(r.pseudoMajor, pseudoBase, info.Time, info.Short)
   518  	return checkGoMod()
   519  }
   520  
   521  // validatePseudoVersion checks that version has a major version compatible with
   522  // r.modPath and encodes a base version and commit metadata that agrees with
   523  // info.
   524  //
   525  // Note that verifying a nontrivial base version in particular may be somewhat
   526  // expensive: in order to do so, r.code.DescendsFrom will need to fetch at least
   527  // enough of the commit history to find a path between version and its base.
   528  // Fortunately, many pseudo-versions — such as those for untagged repositories —
   529  // have trivial bases!
   530  func (r *codeRepo) validatePseudoVersion(info *codehost.RevInfo, version string) (err error) {
   531  	defer func() {
   532  		if err != nil {
   533  			if _, ok := err.(*module.ModuleError); !ok {
   534  				if _, ok := err.(*module.InvalidVersionError); !ok {
   535  					err = &module.InvalidVersionError{Version: version, Pseudo: true, Err: err}
   536  				}
   537  				err = &module.ModuleError{Path: r.modPath, Err: err}
   538  			}
   539  		}
   540  	}()
   541  
   542  	if err := module.CheckPathMajor(version, r.pathMajor); err != nil {
   543  		return err
   544  	}
   545  
   546  	rev, err := PseudoVersionRev(version)
   547  	if err != nil {
   548  		return err
   549  	}
   550  	if rev != info.Short {
   551  		switch {
   552  		case strings.HasPrefix(rev, info.Short):
   553  			return fmt.Errorf("revision is longer than canonical (%s)", info.Short)
   554  		case strings.HasPrefix(info.Short, rev):
   555  			return fmt.Errorf("revision is shorter than canonical (%s)", info.Short)
   556  		default:
   557  			return fmt.Errorf("does not match short name of revision (%s)", info.Short)
   558  		}
   559  	}
   560  
   561  	t, err := PseudoVersionTime(version)
   562  	if err != nil {
   563  		return err
   564  	}
   565  	if !t.Equal(info.Time.Truncate(time.Second)) {
   566  		return fmt.Errorf("does not match version-control timestamp (%s)", info.Time.UTC().Format(time.RFC3339))
   567  	}
   568  
   569  	tagPrefix := ""
   570  	if r.codeDir != "" {
   571  		tagPrefix = r.codeDir + "/"
   572  	}
   573  
   574  	// A pseudo-version should have a precedence just above its parent revisions,
   575  	// and no higher. Otherwise, it would be possible for library authors to "pin"
   576  	// dependency versions (and bypass the usual minimum version selection) by
   577  	// naming an extremely high pseudo-version rather than an accurate one.
   578  	//
   579  	// Moreover, if we allow a pseudo-version to use any arbitrary pre-release
   580  	// tag, we end up with infinitely many possible names for each commit. Each
   581  	// name consumes resources in the module cache and proxies, so we want to
   582  	// restrict them to a finite set under control of the module author.
   583  	//
   584  	// We address both of these issues by requiring the tag upon which the
   585  	// pseudo-version is based to refer to some ancestor of the revision. We
   586  	// prefer the highest such tag when constructing a new pseudo-version, but do
   587  	// not enforce that property when resolving existing pseudo-versions: we don't
   588  	// know when the parent tags were added, and the highest-tagged parent may not
   589  	// have existed when the pseudo-version was first resolved.
   590  	base, err := PseudoVersionBase(strings.TrimSuffix(version, "+incompatible"))
   591  	if err != nil {
   592  		return err
   593  	}
   594  	if base == "" {
   595  		if r.pseudoMajor == "" && semver.Major(version) == "v1" {
   596  			return fmt.Errorf("major version without preceding tag must be v0, not v1")
   597  		}
   598  		return nil
   599  	} else {
   600  		for _, tag := range info.Tags {
   601  			versionOnly := strings.TrimPrefix(tag, tagPrefix)
   602  			if versionOnly == base {
   603  				// The base version is canonical, so if the version from the tag is
   604  				// literally equal (not just equivalent), then the tag is canonical too.
   605  				//
   606  				// We allow pseudo-versions to be derived from non-canonical tags on the
   607  				// same commit, so that tags like "v1.1.0+some-metadata" resolve as
   608  				// close as possible to the canonical version ("v1.1.0") while still
   609  				// enforcing a total ordering ("v1.1.1-0.[…]" with a unique suffix).
   610  				//
   611  				// However, canonical tags already have a total ordering, so there is no
   612  				// reason not to use the canonical tag directly, and we know that the
   613  				// canonical tag must already exist because the pseudo-version is
   614  				// derived from it. In that case, referring to the revision by a
   615  				// pseudo-version derived from its own canonical tag is just confusing.
   616  				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)
   617  			}
   618  		}
   619  	}
   620  
   621  	tags, err := r.code.Tags(tagPrefix + base)
   622  	if err != nil {
   623  		return err
   624  	}
   625  
   626  	var lastTag string // Prefer to log some real tag rather than a canonically-equivalent base.
   627  	ancestorFound := false
   628  	for _, tag := range tags {
   629  		versionOnly := strings.TrimPrefix(tag, tagPrefix)
   630  		if semver.Compare(versionOnly, base) == 0 {
   631  			lastTag = tag
   632  			ancestorFound, err = r.code.DescendsFrom(info.Name, tag)
   633  			if ancestorFound {
   634  				break
   635  			}
   636  		}
   637  	}
   638  
   639  	if lastTag == "" {
   640  		return fmt.Errorf("preceding tag (%s) not found", base)
   641  	}
   642  
   643  	if !ancestorFound {
   644  		if err != nil {
   645  			return err
   646  		}
   647  		rev, err := PseudoVersionRev(version)
   648  		if err != nil {
   649  			return fmt.Errorf("not a descendent of preceding tag (%s)", lastTag)
   650  		}
   651  		return fmt.Errorf("revision %s is not a descendent of preceding tag (%s)", rev, lastTag)
   652  	}
   653  	return nil
   654  }
   655  
   656  func (r *codeRepo) revToRev(rev string) string {
   657  	if semver.IsValid(rev) {
   658  		if IsPseudoVersion(rev) {
   659  			r, _ := PseudoVersionRev(rev)
   660  			return r
   661  		}
   662  		if semver.Build(rev) == "+incompatible" {
   663  			rev = rev[:len(rev)-len("+incompatible")]
   664  		}
   665  		if r.codeDir == "" {
   666  			return rev
   667  		}
   668  		return r.codeDir + "/" + rev
   669  	}
   670  	return rev
   671  }
   672  
   673  func (r *codeRepo) versionToRev(version string) (rev string, err error) {
   674  	if !semver.IsValid(version) {
   675  		return "", &module.ModuleError{
   676  			Path: r.modPath,
   677  			Err: &module.InvalidVersionError{
   678  				Version: version,
   679  				Err:     errors.New("syntax error"),
   680  			},
   681  		}
   682  	}
   683  	return r.revToRev(version), nil
   684  }
   685  
   686  // findDir locates the directory within the repo containing the module.
   687  //
   688  // If r.pathMajor is non-empty, this can be either r.codeDir or — if a go.mod
   689  // file exists — r.codeDir/r.pathMajor[1:].
   690  func (r *codeRepo) findDir(version string) (rev, dir string, gomod []byte, err error) {
   691  	rev, err = r.versionToRev(version)
   692  	if err != nil {
   693  		return "", "", nil, err
   694  	}
   695  
   696  	// Load info about go.mod but delay consideration
   697  	// (except I/O error) until we rule out v2/go.mod.
   698  	file1 := path.Join(r.codeDir, "go.mod")
   699  	gomod1, err1 := r.code.ReadFile(rev, file1, codehost.MaxGoMod)
   700  	if err1 != nil && !os.IsNotExist(err1) {
   701  		return "", "", nil, fmt.Errorf("reading %s/%s at revision %s: %v", r.pathPrefix, file1, rev, err1)
   702  	}
   703  	mpath1 := modfile.ModulePath(gomod1)
   704  	found1 := err1 == nil && (isMajor(mpath1, r.pathMajor) || r.canReplaceMismatchedVersionDueToBug(mpath1))
   705  
   706  	var file2 string
   707  	if r.pathMajor != "" && r.codeRoot != r.modPath && !strings.HasPrefix(r.pathMajor, ".") {
   708  		// Suppose pathMajor is "/v2".
   709  		// Either go.mod should claim v2 and v2/go.mod should not exist,
   710  		// or v2/go.mod should exist and claim v2. Not both.
   711  		// Note that we don't check the full path, just the major suffix,
   712  		// because of replacement modules. This might be a fork of
   713  		// the real module, found at a different path, usable only in
   714  		// a replace directive.
   715  		dir2 := path.Join(r.codeDir, r.pathMajor[1:])
   716  		file2 = path.Join(dir2, "go.mod")
   717  		gomod2, err2 := r.code.ReadFile(rev, file2, codehost.MaxGoMod)
   718  		if err2 != nil && !os.IsNotExist(err2) {
   719  			return "", "", nil, fmt.Errorf("reading %s/%s at revision %s: %v", r.pathPrefix, file2, rev, err2)
   720  		}
   721  		mpath2 := modfile.ModulePath(gomod2)
   722  		found2 := err2 == nil && isMajor(mpath2, r.pathMajor)
   723  
   724  		if found1 && found2 {
   725  			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)
   726  		}
   727  		if found2 {
   728  			return rev, dir2, gomod2, nil
   729  		}
   730  		if err2 == nil {
   731  			if mpath2 == "" {
   732  				return "", "", nil, fmt.Errorf("%s/%s is missing module path at revision %s", r.pathPrefix, file2, rev)
   733  			}
   734  			return "", "", nil, fmt.Errorf("%s/%s has non-...%s module path %q at revision %s", r.pathPrefix, file2, r.pathMajor, mpath2, rev)
   735  		}
   736  	}
   737  
   738  	// Not v2/go.mod, so it's either go.mod or nothing. Which is it?
   739  	if found1 {
   740  		// Explicit go.mod with matching major version ok.
   741  		return rev, r.codeDir, gomod1, nil
   742  	}
   743  	if err1 == nil {
   744  		// Explicit go.mod with non-matching major version disallowed.
   745  		suffix := ""
   746  		if file2 != "" {
   747  			suffix = fmt.Sprintf(" (and ...%s/go.mod does not exist)", r.pathMajor)
   748  		}
   749  		if mpath1 == "" {
   750  			return "", "", nil, fmt.Errorf("%s is missing module path%s at revision %s", file1, suffix, rev)
   751  		}
   752  		if r.pathMajor != "" { // ".v1", ".v2" for gopkg.in
   753  			return "", "", nil, fmt.Errorf("%s has non-...%s module path %q%s at revision %s", file1, r.pathMajor, mpath1, suffix, rev)
   754  		}
   755  		if _, _, ok := module.SplitPathVersion(mpath1); !ok {
   756  			return "", "", nil, fmt.Errorf("%s has malformed module path %q%s at revision %s", file1, mpath1, suffix, rev)
   757  		}
   758  		return "", "", nil, fmt.Errorf("%s has post-%s module path %q%s at revision %s", file1, semver.Major(version), mpath1, suffix, rev)
   759  	}
   760  
   761  	if r.codeDir == "" && (r.pathMajor == "" || strings.HasPrefix(r.pathMajor, ".")) {
   762  		// Implicit go.mod at root of repo OK for v0/v1 and for gopkg.in.
   763  		return rev, "", nil, nil
   764  	}
   765  
   766  	// Implicit go.mod below root of repo or at v2+ disallowed.
   767  	// Be clear about possibility of using either location for v2+.
   768  	if file2 != "" {
   769  		return "", "", nil, fmt.Errorf("missing %s/go.mod and ...%s/go.mod at revision %s", r.pathPrefix, r.pathMajor, rev)
   770  	}
   771  	return "", "", nil, fmt.Errorf("missing %s/go.mod at revision %s", r.pathPrefix, rev)
   772  }
   773  
   774  // isMajor reports whether the versions allowed for mpath are compatible with
   775  // the major version(s) implied by pathMajor, or false if mpath has an invalid
   776  // version suffix.
   777  func isMajor(mpath, pathMajor string) bool {
   778  	if mpath == "" {
   779  		// If we don't have a path, we don't know what version(s) it is compatible with.
   780  		return false
   781  	}
   782  	_, mpathMajor, ok := module.SplitPathVersion(mpath)
   783  	if !ok {
   784  		// An invalid module path is not compatible with any version.
   785  		return false
   786  	}
   787  	if pathMajor == "" {
   788  		// All of the valid versions for a gopkg.in module that requires major
   789  		// version v0 or v1 are compatible with the "v0 or v1" implied by an empty
   790  		// pathMajor.
   791  		switch module.PathMajorPrefix(mpathMajor) {
   792  		case "", "v0", "v1":
   793  			return true
   794  		default:
   795  			return false
   796  		}
   797  	}
   798  	if mpathMajor == "" {
   799  		// Even if pathMajor is ".v0" or ".v1", we can't be sure that a module
   800  		// without a suffix is tagged appropriately. Besides, we don't expect clones
   801  		// of non-gopkg.in modules to have gopkg.in paths, so a non-empty,
   802  		// non-gopkg.in mpath is probably the wrong module for any such pathMajor
   803  		// anyway.
   804  		return false
   805  	}
   806  	// If both pathMajor and mpathMajor are non-empty, then we only care that they
   807  	// have the same major-version validation rules. A clone fetched via a /v2
   808  	// path might replace a module with path gopkg.in/foo.v2-unstable, and that's
   809  	// ok.
   810  	return pathMajor[1:] == mpathMajor[1:]
   811  }
   812  
   813  // canReplaceMismatchedVersionDueToBug reports whether versions of r
   814  // could replace versions of mpath with otherwise-mismatched major versions
   815  // due to a historical bug in the Go command (golang.org/issue/34254).
   816  func (r *codeRepo) canReplaceMismatchedVersionDueToBug(mpath string) bool {
   817  	// The bug caused us to erroneously accept unversioned paths as replacements
   818  	// for versioned gopkg.in paths.
   819  	unversioned := r.pathMajor == ""
   820  	replacingGopkgIn := strings.HasPrefix(mpath, "gopkg.in/")
   821  	return unversioned && replacingGopkgIn
   822  }
   823  
   824  func (r *codeRepo) GoMod(version string) (data []byte, err error) {
   825  	if version != module.CanonicalVersion(version) {
   826  		return nil, fmt.Errorf("version %s is not canonical", version)
   827  	}
   828  
   829  	if IsPseudoVersion(version) {
   830  		// findDir ignores the metadata encoded in a pseudo-version,
   831  		// only using the revision at the end.
   832  		// Invoke Stat to verify the metadata explicitly so we don't return
   833  		// a bogus file for an invalid version.
   834  		_, err := r.Stat(version)
   835  		if err != nil {
   836  			return nil, err
   837  		}
   838  	}
   839  
   840  	rev, dir, gomod, err := r.findDir(version)
   841  	if err != nil {
   842  		return nil, err
   843  	}
   844  	if gomod != nil {
   845  		return gomod, nil
   846  	}
   847  	data, err = r.code.ReadFile(rev, path.Join(dir, "go.mod"), codehost.MaxGoMod)
   848  	if err != nil {
   849  		if os.IsNotExist(err) {
   850  			return r.legacyGoMod(rev, dir), nil
   851  		}
   852  		return nil, err
   853  	}
   854  	return data, nil
   855  }
   856  
   857  func (r *codeRepo) legacyGoMod(rev, dir string) []byte {
   858  	// We used to try to build a go.mod reflecting pre-existing
   859  	// package management metadata files, but the conversion
   860  	// was inherently imperfect (because those files don't have
   861  	// exactly the same semantics as go.mod) and, when done
   862  	// for dependencies in the middle of a build, impossible to
   863  	// correct. So we stopped.
   864  	// Return a fake go.mod that simply declares the module path.
   865  	return []byte(fmt.Sprintf("module %s\n", modfile.AutoQuote(r.modPath)))
   866  }
   867  
   868  func (r *codeRepo) modPrefix(rev string) string {
   869  	return r.modPath + "@" + rev
   870  }
   871  
   872  func (r *codeRepo) Zip(dst io.Writer, version string) error {
   873  	if version != module.CanonicalVersion(version) {
   874  		return fmt.Errorf("version %s is not canonical", version)
   875  	}
   876  
   877  	if IsPseudoVersion(version) {
   878  		// findDir ignores the metadata encoded in a pseudo-version,
   879  		// only using the revision at the end.
   880  		// Invoke Stat to verify the metadata explicitly so we don't return
   881  		// a bogus file for an invalid version.
   882  		_, err := r.Stat(version)
   883  		if err != nil {
   884  			return err
   885  		}
   886  	}
   887  
   888  	rev, subdir, _, err := r.findDir(version)
   889  	if err != nil {
   890  		return err
   891  	}
   892  	dl, err := r.code.ReadZip(rev, subdir, codehost.MaxZipFile)
   893  	if err != nil {
   894  		return err
   895  	}
   896  	defer dl.Close()
   897  	subdir = strings.Trim(subdir, "/")
   898  
   899  	// Spool to local file.
   900  	f, err := ioutil.TempFile("", "go-codehost-")
   901  	if err != nil {
   902  		dl.Close()
   903  		return err
   904  	}
   905  	defer os.Remove(f.Name())
   906  	defer f.Close()
   907  	maxSize := int64(codehost.MaxZipFile)
   908  	lr := &io.LimitedReader{R: dl, N: maxSize + 1}
   909  	if _, err := io.Copy(f, lr); err != nil {
   910  		dl.Close()
   911  		return err
   912  	}
   913  	dl.Close()
   914  	if lr.N <= 0 {
   915  		return fmt.Errorf("downloaded zip file too large")
   916  	}
   917  	size := (maxSize + 1) - lr.N
   918  	if _, err := f.Seek(0, 0); err != nil {
   919  		return err
   920  	}
   921  
   922  	// Translate from zip file we have to zip file we want.
   923  	zr, err := zip.NewReader(f, size)
   924  	if err != nil {
   925  		return err
   926  	}
   927  
   928  	var files []modzip.File
   929  	if subdir != "" {
   930  		subdir += "/"
   931  	}
   932  	haveLICENSE := false
   933  	topPrefix := ""
   934  	for _, zf := range zr.File {
   935  		if topPrefix == "" {
   936  			i := strings.Index(zf.Name, "/")
   937  			if i < 0 {
   938  				return fmt.Errorf("missing top-level directory prefix")
   939  			}
   940  			topPrefix = zf.Name[:i+1]
   941  		}
   942  		if !strings.HasPrefix(zf.Name, topPrefix) {
   943  			return fmt.Errorf("zip file contains more than one top-level directory")
   944  		}
   945  		name := strings.TrimPrefix(zf.Name, topPrefix)
   946  		if !strings.HasPrefix(name, subdir) {
   947  			continue
   948  		}
   949  		name = strings.TrimPrefix(name, subdir)
   950  		if name == "" || strings.HasSuffix(name, "/") {
   951  			continue
   952  		}
   953  		files = append(files, zipFile{name: name, f: zf})
   954  		if name == "LICENSE" {
   955  			haveLICENSE = true
   956  		}
   957  	}
   958  
   959  	if !haveLICENSE && subdir != "" {
   960  		data, err := r.code.ReadFile(rev, "LICENSE", codehost.MaxLICENSE)
   961  		if err == nil {
   962  			files = append(files, dataFile{name: "LICENSE", data: data})
   963  		}
   964  	}
   965  
   966  	return modzip.Create(dst, module.Version{Path: r.modPath, Version: version}, files)
   967  }
   968  
   969  type zipFile struct {
   970  	name string
   971  	f    *zip.File
   972  }
   973  
   974  func (f zipFile) Path() string                 { return f.name }
   975  func (f zipFile) Lstat() (os.FileInfo, error)  { return f.f.FileInfo(), nil }
   976  func (f zipFile) Open() (io.ReadCloser, error) { return f.f.Open() }
   977  
   978  type dataFile struct {
   979  	name string
   980  	data []byte
   981  }
   982  
   983  func (f dataFile) Path() string                { return f.name }
   984  func (f dataFile) Lstat() (os.FileInfo, error) { return dataFileInfo{f}, nil }
   985  func (f dataFile) Open() (io.ReadCloser, error) {
   986  	return ioutil.NopCloser(bytes.NewReader(f.data)), nil
   987  }
   988  
   989  type dataFileInfo struct {
   990  	f dataFile
   991  }
   992  
   993  func (fi dataFileInfo) Name() string       { return path.Base(fi.f.name) }
   994  func (fi dataFileInfo) Size() int64        { return int64(len(fi.f.data)) }
   995  func (fi dataFileInfo) Mode() os.FileMode  { return 0644 }
   996  func (fi dataFileInfo) ModTime() time.Time { return time.Time{} }
   997  func (fi dataFileInfo) IsDir() bool        { return false }
   998  func (fi dataFileInfo) Sys() interface{}   { return nil }
   999  
  1000  // hasPathPrefix reports whether the path s begins with the
  1001  // elements in prefix.
  1002  func hasPathPrefix(s, prefix string) bool {
  1003  	switch {
  1004  	default:
  1005  		return false
  1006  	case len(s) == len(prefix):
  1007  		return s == prefix
  1008  	case len(s) > len(prefix):
  1009  		if prefix != "" && prefix[len(prefix)-1] == '/' {
  1010  			return strings.HasPrefix(s, prefix)
  1011  		}
  1012  		return s[len(prefix)] == '/' && s[:len(prefix)] == prefix
  1013  	}
  1014  }
  1015  
  1016  func isVendoredPackage(name string) bool {
  1017  	var i int
  1018  	if strings.HasPrefix(name, "vendor/") {
  1019  		i += len("vendor/")
  1020  	} else if j := strings.Index(name, "/vendor/"); j >= 0 {
  1021  		// This offset looks incorrect; this should probably be
  1022  		//
  1023  		// 	i = j + len("/vendor/")
  1024  		//
  1025  		// (See https://golang.org/issue/31562.)
  1026  		//
  1027  		// Unfortunately, we can't fix it without invalidating checksums.
  1028  		// Fortunately, the error appears to be strictly conservative: we'll retain
  1029  		// vendored packages that we should have pruned, but we won't prune
  1030  		// non-vendored packages that we should have retained.
  1031  		//
  1032  		// Since this defect doesn't seem to break anything, it's not worth fixing
  1033  		// for now.
  1034  		i += len("/vendor/")
  1035  	} else {
  1036  		return false
  1037  	}
  1038  	return strings.Contains(name[i:], "/")
  1039  }