aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/go/internal/modfetch/coderepo.go
diff options
context:
space:
mode:
authorBryan C. Mills <bcmills@google.com>2019-07-15 14:06:58 -0400
committerBryan C. Mills <bcmills@google.com>2019-07-15 21:51:12 +0000
commit24a6ca09d0151f2dc6a524217b8523abd33403bb (patch)
tree84be14588b2701fc2a11da88f642d0bb4ba6c72a /src/cmd/go/internal/modfetch/coderepo.go
parent20e4540e9084528a1b36978882596daa7d8d8800 (diff)
downloadgo-24a6ca09d0151f2dc6a524217b8523abd33403bb.tar.gz
go-24a6ca09d0151f2dc6a524217b8523abd33403bb.zip
cmd/go/internal/modfetch: always check for a go.mod file when fetching from version control
If the module path declared in the go.mod file does not match the path we are trying to resolve, a build using that module is doomed to fail. Since we know that the module path does not match in the underlying repo, we also know that the requested module does not exist at the requested version. Therefore, we should reject that version in Stat with a “not exist” error — sooner rather than later — so that modload.Query will continue to check other candidate paths (for example, with a major-version suffix added or removed). Fixes #33099 Change-Id: I43c980f78ed75fa6ace90f237cc3aad46c22d83a Reviewed-on: https://go-review.googlesource.com/c/go/+/186237 Run-TryBot: Bryan C. Mills <bcmills@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Jay Conrod <jayconrod@google.com>
Diffstat (limited to 'src/cmd/go/internal/modfetch/coderepo.go')
-rw-r--r--src/cmd/go/internal/modfetch/coderepo.go37
1 files changed, 23 insertions, 14 deletions
diff --git a/src/cmd/go/internal/modfetch/coderepo.go b/src/cmd/go/internal/modfetch/coderepo.go
index 267b76349dd..491fe11f50b 100644
--- a/src/cmd/go/internal/modfetch/coderepo.go
+++ b/src/cmd/go/internal/modfetch/coderepo.go
@@ -31,7 +31,7 @@ type codeRepo struct {
codeRoot string
// codeDir is the directory (relative to root) at which we expect to find the module.
// If pathMajor is non-empty and codeRoot is not the full modPath,
- // then we look in both codeDir and codeDir+modPath
+ // then we look in both codeDir and codeDir/pathMajor[1:].
codeDir string
// pathMajor is the suffix of modPath that indicates its major version,
@@ -248,20 +248,25 @@ func (r *codeRepo) convert(info *codehost.RevInfo, statVers string) (*RevInfo, e
// exist as required by info2.Version and the module path represented by r.
checkGoMod := func() (*RevInfo, error) {
// If r.codeDir is non-empty, then the go.mod file must exist: the module
- // author, not the module consumer, gets to decide how to carve up the repo
+ // author — not the module consumer, — gets to decide how to carve up the repo
// into modules.
- if r.codeDir != "" {
- _, _, _, err := r.findDir(info2.Version)
- if err != nil {
- // TODO: It would be nice to return an error like "not a module".
- // Right now we return "missing go.mod", which is a little confusing.
- return nil, &module.ModuleError{
- Path: r.modPath,
- Err: &module.InvalidVersionError{
- Version: info2.Version,
- Err: notExistError(err.Error()),
- },
- }
+ //
+ // Conversely, if the go.mod file exists, the module author — not the module
+ // consumer — gets to determine the module's path
+ //
+ // r.findDir verifies both of these conditions. Execute it now so that
+ // r.Stat will correctly return a notExistError if the go.mod location or
+ // declared module path doesn't match.
+ _, _, _, err := r.findDir(info2.Version)
+ if err != nil {
+ // TODO: It would be nice to return an error like "not a module".
+ // Right now we return "missing go.mod", which is a little confusing.
+ return nil, &module.ModuleError{
+ Path: r.modPath,
+ Err: &module.InvalidVersionError{
+ Version: info2.Version,
+ Err: notExistError(err.Error()),
+ },
}
}
@@ -571,6 +576,10 @@ func (r *codeRepo) versionToRev(version string) (rev string, err error) {
return r.revToRev(version), nil
}
+// findDir locates the directory within the repo containing the module.
+//
+// If r.pathMajor is non-empty, this can be either r.codeDir or — if a go.mod
+// file exists — r.codeDir/r.pathMajor[1:].
func (r *codeRepo) findDir(version string) (rev, dir string, gomod []byte, err error) {
rev, err = r.versionToRev(version)
if err != nil {