@@ -12,6 +12,9 @@ package module
12
12
// There are many subtle considerations, including Unicode ambiguity,
13
13
// security, network, and file system representations.
14
14
//
15
+ // This file also defines the set of valid module path and version combinations,
16
+ // another topic with many subtle considerations.
17
+ //
15
18
// Changes to the semantics in this file require approval from rsc.
16
19
17
20
import (
@@ -50,31 +53,17 @@ func Check(path, version string) error {
50
53
if ! semver .IsValid (version ) {
51
54
return fmt .Errorf ("malformed semantic version %v" , version )
52
55
}
53
- vm := semver .Major (version )
54
- _ , pathVersion , _ := SplitPathVersion (path )
55
-
56
- if strings .HasPrefix (pathVersion , "." ) {
57
- // Special-case gopkg.in path requirements.
58
- pathVersion = pathVersion [1 :] // cut .
59
- if vm == pathVersion {
60
- return nil
61
- }
62
- } else {
63
- // Standard path requirements.
64
- if pathVersion != "" {
65
- pathVersion = pathVersion [1 :] // cut /
66
- }
67
- if vm == "v0" || vm == "v1" {
68
- vm = ""
56
+ _ , pathMajor , _ := SplitPathVersion (path )
57
+ if ! MatchPathMajor (version , pathMajor ) {
58
+ if pathMajor == "" {
59
+ pathMajor = "v0 or v1"
69
60
}
70
- if vm == pathVersion {
71
- return nil
72
- }
73
- if pathVersion == "" {
74
- pathVersion = "v0 or v1"
61
+ if pathMajor [0 ] == '.' { // .v1
62
+ pathMajor = pathMajor [1 :]
75
63
}
64
+ return fmt .Errorf ("mismatched module path %v and version %v (want %v)" , path , version , pathMajor )
76
65
}
77
- return fmt . Errorf ( "mismatched module path %v and version %v (want %v)" , path , version , pathVersion )
66
+ return nil
78
67
}
79
68
80
69
// firstPathOK reports whether r can appear in the first element of a module path.
@@ -328,6 +317,11 @@ func splitGopkgIn(path string) (prefix, pathMajor string, ok bool) {
328
317
// MatchPathMajor reports whether the semantic version v
329
318
// matches the path major version pathMajor.
330
319
func MatchPathMajor (v , pathMajor string ) bool {
320
+ if strings .HasPrefix (v , "v0.0.0-" ) && pathMajor == ".v1" {
321
+ // Allow old bug in pseudo-versions that generated v0.0.0- pseudoversion for gopkg .v1.
322
+ // For example, gopkg.in/[email protected] 's go.mod requires gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405.
323
+ return true
324
+ }
331
325
m := semver .Major (v )
332
326
if pathMajor == "" {
333
327
return m == "v0" || m == "v1"
0 commit comments