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