]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/go: be less strict about go version syntax in dependency go.mod files
authorRuss Cox <rsc@golang.org>
Thu, 6 May 2021 18:01:20 +0000 (14:01 -0400)
committerRuss Cox <rsc@golang.org>
Thu, 13 May 2021 15:33:24 +0000 (15:33 +0000)
It is unclear what the future holds for the go line in go.mod files.
Perhaps at some point we will switch to semver numbering.
Perhaps at some point we will allow specifying minor versions
or even betas and release candidates.
Those kinds of changes are difficult today because the go line
is parsed in dependency modules, meaning that older
versions of the Go toolchain need to understand newer go lines.

This CL makes that case - parsing a go line in a dependency's
go.mod file - a bit more lax about how to find the version.
It allows a leading v and any trailing non-digit-prefixed string
after the MAJOR.MINOR section.

There are no concrete plans to make use of any of these changes,
but if in the future we want to make them, having a few Go releases
under out belt that will accept the syntax in dependencies will
make any changes significantly easier.

Change-Id: I79bb84bba4b769048ac4b14d5c275eb9a3f270c4
Reviewed-on: https://go-review.googlesource.com/c/go/+/317690
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
src/cmd/go.mod
src/cmd/go.sum
src/cmd/go/testdata/script/badgo.txt [new file with mode: 0644]
src/cmd/vendor/golang.org/x/mod/modfile/rule.go
src/cmd/vendor/modules.txt

index c3617eea0b1108f04a391332f311514e7d6754b9..88f5f2883a69056f1883feb381867c73304e386a 100644 (file)
@@ -7,7 +7,7 @@ require (
        github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639 // indirect
        golang.org/x/arch v0.0.0-20210502124803-cbf565b21d1e
        golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e // indirect
-       golang.org/x/mod v0.4.3-0.20210504181020-67f1c1edc27a
+       golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd
        golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 // indirect
        golang.org/x/term v0.0.0-20210503060354-a79de5458b56
        golang.org/x/tools v0.1.1-0.20210505014545-7cab0ef2e9a5
index f42aac70d695250806d1295c26714d91cdfee88e..73750802bc15d6f45c035cf89c60a2fae828eb93 100644 (file)
@@ -9,8 +9,8 @@ golang.org/x/arch v0.0.0-20210502124803-cbf565b21d1e h1:pv3V0NlNSh5Q6AX/StwGLBjc
 golang.org/x/arch v0.0.0-20210502124803-cbf565b21d1e/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
 golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e h1:8foAy0aoO5GkqCvAEJ4VC4P3zksTg4X4aJCDpZzmgQI=
 golang.org/x/crypto v0.0.0-20210503195802-e9a32991a82e/go.mod h1:P+XmwS30IXTQdn5tA2iutPOUgjI07+tq3H3K9MVA1s8=
-golang.org/x/mod v0.4.3-0.20210504181020-67f1c1edc27a h1:wbpC/7Wbo5WFVox32n+KjhRRLmTLq8YW/wRlL2iVAhk=
-golang.org/x/mod v0.4.3-0.20210504181020-67f1c1edc27a/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
+golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd h1:CuRnpyMrCCBulv0d/y0CswR4K0vGydgE3DZ2wYPIOo8=
+golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
 golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
 golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744 h1:yhBbb4IRs2HS9PPlAg6DMC6mUOKexJBNsLf4Z+6En1Q=
 golang.org/x/sys v0.0.0-20210511113859-b0526f3d8744/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
diff --git a/src/cmd/go/testdata/script/badgo.txt b/src/cmd/go/testdata/script/badgo.txt
new file mode 100644 (file)
index 0000000..cf4e258
--- /dev/null
@@ -0,0 +1,50 @@
+go get example.net/badgo@v1.0.0
+go get example.net/badgo@v1.1.0
+go get example.net/badgo@v1.2.0
+go get example.net/badgo@v1.3.0
+go get example.net/badgo@v1.4.0
+go get example.net/badgo@v1.5.0
+! go get example.net/badgo@v1.6.0
+stderr 'invalid go version .X.Y.: must match format 1.23'
+
+-- go.mod --
+module m
+
+replace (
+       example.net/badgo v1.0.0 => ./v1.0.0
+       example.net/badgo v1.1.0 => ./v1.1.0
+       example.net/badgo v1.2.0 => ./v1.2.0
+       example.net/badgo v1.3.0 => ./v1.3.0
+       example.net/badgo v1.4.0 => ./v1.4.0
+       example.net/badgo v1.5.0 => ./v1.5.0
+       example.net/badgo v1.6.0 => ./v1.6.0
+)
+
+-- v1.0.0/go.mod --
+module example.net/badgo
+go 1.17.0
+
+-- v1.1.0/go.mod --
+module example.net/badgo
+go 1.17rc2
+
+-- v1.2.0/go.mod --
+module example.net/badgo
+go 1.17.1
+
+-- v1.3.0/go.mod --
+module example.net/badgo
+go v1.17.0
+
+-- v1.4.0/go.mod --
+module example.net/badgo
+go v1.17.0-rc.2
+
+-- v1.5.0/go.mod --
+module example.net/badgo
+go v1.17.1
+
+-- v1.6.0/go.mod --
+module example.net/badgo
+go X.Y
+
index d8242de28065cc5d4c325070098da0cbba5f07b1..7299e15500a85474e5fb96b04df16415897ef249 100644 (file)
@@ -217,6 +217,7 @@ func parseToFile(file string, data []byte, fix VersionFixer, strict bool) (parse
 }
 
 var GoVersionRE = lazyregexp.New(`^([1-9][0-9]*)\.(0|[1-9][0-9]*)$`)
+var laxGoVersionRE = lazyregexp.New(`^v?(([1-9][0-9]*)\.(0|[1-9][0-9]*))([^0-9].*)$`)
 
 func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, args []string, fix VersionFixer, strict bool) {
        // If strict is false, this module is a dependency.
@@ -267,8 +268,17 @@ func (f *File) add(errs *ErrorList, block *LineBlock, line *Line, verb string, a
                        errorf("go directive expects exactly one argument")
                        return
                } else if !GoVersionRE.MatchString(args[0]) {
-                       errorf("invalid go version '%s': must match format 1.23", args[0])
-                       return
+                       fixed := false
+                       if !strict {
+                               if m := laxGoVersionRE.FindStringSubmatch(args[0]); m != nil {
+                                       args[0] = m[1]
+                                       fixed = true
+                               }
+                       }
+                       if !fixed {
+                               errorf("invalid go version '%s': must match format 1.23", args[0])
+                               return
+                       }
                }
 
                f.Go = &Go{Syntax: line}
index 10591f8041be978abb5dd8a5617cecb6070d6063..016ec011a9793bae75121c44503cca1c53b5aca0 100644 (file)
@@ -28,7 +28,7 @@ golang.org/x/arch/x86/x86asm
 ## explicit; go 1.17
 golang.org/x/crypto/ed25519
 golang.org/x/crypto/ed25519/internal/edwards25519
-# golang.org/x/mod v0.4.3-0.20210504181020-67f1c1edc27a
+# golang.org/x/mod v0.4.3-0.20210512182355-6088ed88cecd
 ## explicit; go 1.17
 golang.org/x/mod/internal/lazyregexp
 golang.org/x/mod/modfile