[go] cmd/go: make mod init disallow invalid major version suffixes

525 views
Skip to first unread message

Paschalis Tsilias (Gerrit)

unread,
Feb 2, 2021, 8:32:13 AM2/2/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Paschalis Tsilias has uploaded this change for review.

View Change

cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes, and return a relevant error
message.

Fixes #44052

Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 27 insertions(+), 0 deletions(-)

diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index bc8d17e..da6483a 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -490,6 +490,10 @@
return errorf("contains disallowed path separator character %q", p[i])
}

+ if _, _, ok := module.SplitPathVersion(p); !ok {
+ return errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later")
+ }
+
// Ensure path.IsAbs and build.IsLocalImport are false, and that the path is
// invariant under path.Clean, also to avoid confusing the module cache.
if path.IsAbs(p) {
diff --git a/src/cmd/go/testdata/script/mod_init_invalid_major.txt b/src/cmd/go/testdata/script/mod_init_invalid_major.txt
new file mode 100644
index 0000000..d3813d1
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_init_invalid_major.txt
@@ -0,0 +1,23 @@
+env GO111MODULE=on
+env GOFLAGS=-mod=mod
+
+! go mod init example.com/user/repo/v0
+stderr '^go: invalid module path "example.com/user/repo/v0": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v02
+stderr '^go: invalid module path "example.com/user/repo/v02": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v1
+stderr '^go: invalid module path "example.com/user/repo/v1": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v2.0
+stderr '^go: invalid module path "example.com/user/repo/v2.0": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v2.1.4
+stderr '^go: invalid module path "example.com/user/repo/v2.1.4": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v2.
+stderr '^go: invalid module path "example.com/user/repo/v2.": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'
+
+! go mod init example.com/user/repo/v.2
+stderr '^go: invalid module path "example.com/user/repo/v.2": major version suffixes must be in the form of /vN and are only allowed for v2 or later$'

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 1
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-MessageType: newchange

Bryan C. Mills (Gerrit)

unread,
Feb 2, 2021, 10:20:24 AM2/2/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Russ Cox, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

    • Patch Set #1, Line 494: errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later")

      This assumes a non- gopkg.in module path.

      That is certainly the more common kind of path (by far!), but perhaps it's worth emitting a different message for a malformed gopkg.in path?

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 1
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Tue, 02 Feb 2021 15:20:20 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Paschalis Tsilias (Gerrit)

unread,
Feb 3, 2021, 8:04:23 AM2/3/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

Paschalis Tsilias uploaded patch set #2 to this change.

View Change

cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes, and return a relevant error
message.

Fixes #44052

Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 51 insertions(+), 0 deletions(-)

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 2
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: newpatchset

Paschalis Tsilias (Gerrit)

unread,
Feb 3, 2021, 8:11:07 AM2/3/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Russ Cox, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

    • Patch Set #1, Line 494: errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later")

    • This assumes a non- gopkg.in module path. […]

      You're absolutely right! I added an alternate error message and some more test cases, according to what splitGopkgIn actually checks, let me know how it looks.

      Do you think this should be moved in a helper function to keep things cleaner?

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 2
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Wed, 03 Feb 2021 13:11:00 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
Gerrit-MessageType: comment

Bryan C. Mills (Gerrit)

unread,
Mar 4, 2021, 9:15:41 PM3/4/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

Bryan C. Mills removed Ian Lance Taylor from this change.

View Change

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 2
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: deleteReviewer

Bryan C. Mills (Gerrit)

unread,
Mar 4, 2021, 9:15:42 PM3/4/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Russ Cox, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

Bryan C. Mills removed Russ Cox from this change.

Bryan C. Mills (Gerrit)

unread,
Mar 4, 2021, 9:15:42 PM3/4/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Russ Cox, Ian Lance Taylor, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/internal/modload/init.go:

    • Patch Set #2, Line 494:

      		if strings.HasPrefix(p, "gopkg.in/") {
      return errorf("module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN")
      }
      return errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later")

      I think we could do one better on these error messages. Could the error message suggest the corrected version of what the user actually entered, so that they can just copy and paste it to try again?

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 2
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-CC: Ian Lance Taylor <ia...@golang.org>
Gerrit-CC: Russ Cox <r...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 05 Mar 2021 02:15:36 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Paschalis Tsilias (Gerrit)

unread,
Apr 28, 2021, 5:17:31 PM4/28/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

Paschalis Tsilias uploaded patch set #3 to this change.

View Change

cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes, and return a relevant error
message along with a suggested fix.


Fixes #44052

Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 127 insertions(+), 0 deletions(-)

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 3
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>

Paschalis Tsilias (Gerrit)

unread,
Apr 28, 2021, 5:23:48 PM4/28/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Michael Matloob.

View Change

2 comments:

  • Patchset:

    • Patch Set #3:

      First off, I apologize for taking nearly two months for responding to a small change 😞

      Secondly, I've amended the PR with better error messages. I feel that there's room for improvement in the way that we build the suggestions, but please let me know if at least that's aligned with what you had in mind.

      Thanks a lot for your time!

  • File src/cmd/go/internal/modload/init.go:

    • Patch Set #2, Line 494:

      		if strings.HasPrefix(p, "gopkg.in/") {
      return errorf("module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN")
      }
      return errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later")

    • I think we could do one better on these error messages. […]

      Sounds like a great idea! I've added a first implementation for this, and modified the test script accordingly.

      The two functions `suggestGopkgIn` and `suggestModulePath` which build the corrected 'suggestions' can definitely be improved a lot; I just wanted to know if that's the direction you were thinking of.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 3
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Wed, 28 Apr 2021 21:23:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Bryan C. Mills (Gerrit)

unread,
Apr 28, 2021, 9:43:40 PM4/28/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

View Change

5 comments:

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 3
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 29 Apr 2021 01:43:35 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Paschalis Tsilias (Gerrit)

unread,
Apr 29, 2021, 5:59:12 PM4/29/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

Paschalis Tsilias uploaded patch set #4 to this change.

View Change

cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes, and return a relevant error
message along with a suggested fix.

Fixes #44052

Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 127 insertions(+), 0 deletions(-)

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: newpatchset

Paschalis Tsilias (Gerrit)

unread,
Apr 29, 2021, 6:06:47 PM4/29/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Michael Matloob.

View Change

5 comments:

  • Patchset:

    • Patch Set #4:

      The newest patchset now separates the suggestion with \n\t.

      I've added a couple of comments for the other remarks; I think your input will be valuable, as I don't have really extensive experience with Go modules.

      Thank you for your time!

  • File src/cmd/go/internal/modload/init.go:

    • If we're suggesting a `go mod init` command for the user to run, separate it with \n\t, like: […]

      Done, it looks much better now!

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • I don't think these two cases should be allowed. […]

      These two cases are indeed not allowed; I've only included them in the test script to make sure that we suggest that the user removes any leading zeroes.

      Am I missing anything?

    • I wanted your input on this, and your example is indeed a good real-world scenario.

      The spec might be a little contradictory here https://golang.org/ref/mod#go-mod-file-ident.

      For a final path element of the form /vN where N looks numeric (ASCII digits and dots), N must not begin with a leading zero, must not be /v1, and must not contain any dots.

      So it both says 'where N looks numeric (ASCII digits and dots)' _and_ 'must not contain any dots'.

      Currently, the SplitPathVersion function rejects the module paths highlighted here, and also gives a similar failure example in its docstring.

      Do you have any more information to clear this up?

      Also, to be honest, while the module itself may be on a version with minor/patch (eg. v3.7.2), I haven't met a module path that includes them (eg. so I need to `go get github.com/user/pkg/v3.4`). I was under the impression that you'd only have a different module path if you bump your major, as stated on https://golang.org/doc/modules/version-numbers.

      I understand that many projects don't respect semantic versioning, so this may be needed, but if that's the case, could the spec also need an amendment?

    • I think this path should be rejected on the grounds that it is not a valid import path (per #45025), […]

      Thanks for catching this. Yes, this is a regression as Go 1.15.7 reports the correct error message, but the tip happily accepts it.

      ```
      $ go version
      go version go1.15.7 darwin/amd64
      $ go mod init example.com/user/repo/v2.
      go: malformed import path "example.com/user/repo/v2.": trailing dot in path element

      $ go version
      go version devel go1.17-fd09593667 Thu Apr 29 20:46:14 2021 +0000 darwin/amd64
      $ go mod init example.com/user/repo/v2.
      go: creating new go.mod: module example.com/user/repo/v2.
      ```

      I'll dig around and find out if it's an easy fix, or if we if #45025 should be done first.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Thu, 29 Apr 2021 22:06:41 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Bryan C. Mills (Gerrit)

unread,
May 4, 2021, 8:52:26 PM5/4/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

Patch set 4:Trust +1

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • These two cases are indeed not allowed; I've only included them in the test script to make sure that […]

      Sorry, I mistyped. I don't think these two cases should be *disallowed*, because there is no ambiguity here: IMO these paths are clearly not intended to be semantic major-versions.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Wed, 05 May 2021 00:52:21 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
Comment-In-Reply-To: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-MessageType: comment

Jay Conrod (Gerrit)

unread,
May 5, 2021, 6:09:32 PM5/5/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Paschalis Tsilias, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • Sorry, I mistyped. […]

      IMO, 'go mod init' should forbid these. module.CheckPath rejects them because they're similar to major version suffixes but aren't valid. Better to let users know earlier.

      (Basically, +1 to Paschalis' comment below)

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Wed, 05 May 2021 22:09:27 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Bryan C. Mills (Gerrit)

unread,
May 7, 2021, 9:11:49 AM5/7/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • IMO, 'go mod init' should forbid these. module. […]

      If module.CheckPath already rejects them, then I'm fine with disallowing them — but I'm not entirely convinced that module.CheckPath ought to be rejecting them. 🤔

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 07 May 2021 13:11:40 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
Comment-In-Reply-To: Paschalis Tsilias <paschali...@gmail.com>
Comment-In-Reply-To: Jay Conrod <jayc...@google.com>
Gerrit-MessageType: comment

Paschalis Tsilias (Gerrit)

unread,
May 7, 2021, 10:50:17 AM5/7/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • If module. […]

      For consistency, this CL reuses `SplitPathVersion` from `module.CheckPath` and that's why both these and the ones in the other unresolved comment (like `example.com/user/repo/v4.1.4`) are failing.

      I, too, think that we shouldn't allow initializing modules which then module.CheckPath would fail, but I wouldn't know which is the correct decision here.

      My 2c, is that allowing `v02` is like allowing a loophole for people to not use a 'proper' v2, which might be confusing.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 07 May 2021 14:50:10 +0000

Paschalis Tsilias (Gerrit)

unread,
May 7, 2021, 10:51:04 AM5/7/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Jay Conrod, Michael Matloob.

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • For consistency, this CL reuses `SplitPathVersion` from `module. […]

      Sorry, accidentally resolved this.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 07 May 2021 14:50:57 +0000

Bryan C. Mills (Gerrit)

unread,
May 7, 2021, 4:27:10 PM5/7/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Jay Conrod, Michael Matloob.

Patch set 4:Code-Review +1

View Change

3 comments:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • Sorry, accidentally resolved this.

      Ok, so it seems like things are at least consistent for now. Let's go with this and see if any issues turn up during the beta.

    • I wanted your input on this, and your example is indeed a good real-world scenario. […]

      From that text it seems that we are correct to reject these.

    • stderr '^go: invalid module path "example.com/user/repo/v2.": major version suffixes must be in the form of /vN and are only allowed for v2 or later; eg. go mod init example.com/user/repo/v2$'

      Thanks for catching this. Yes, this is a regression as Go 1.15. […]

      Any luck figuring this one out?

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 07 May 2021 20:27:06 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

Paschalis Tsilias (Gerrit)

unread,
May 9, 2021, 4:50:12 PM5/9/21
to goph...@pubsubhelper.golang.org, Bryan C. Mills, Jay Conrod, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Jay Conrod, Michael Matloob.

View Change

1 comment:

    • Any luck figuring this one out?

      I found this behavior changing in CL 264621.

      I think it's because the call to `module.CheckImportPath` that includes `checkElem` was replaced with a call to `checkModulePathLax`. As the major version checks implemented in the current CL are located in `checkModulePathLax`, it's currently failing again, but with this 'new' message.

      I tried to change the call to `checkModulePathLax` back to `module.CheckImportPath` and it fails with the 'old' error message, but it also causes side effects. For example, `go mod init .` would fail with 'malformed import path' instead of 'invalid module path ".": is a local import path'.

      There is a relevant-ish TODO comment under `checkModulePathLax`, that says that in the future we might switch back to calling `module.CheckImportPath` but that discussion has since been closed.

      ```
      func checkModulePathLax(p string) error {
      // TODO(matloob): Replace calls of this function in this CL with calls
      // to module.CheckImportPath once it's been laxened, if it becomes laxened.
      // See golang.org/issue/29101 for a discussion about whether to make CheckImportPath
      // more lax or more strict.
      ```


      Do you have any feedback on what's the best way to proceed?

      One option would be to replace this specific call to `checkModulePathLax` with `module.CheckImportPath`, and implement these new checks there, while fixing any other rising behaviors.

      Another option would be to enhance `checkModulePathLax` with some new checks to emulate this error message, or to fix this issue first in #45025.

      Please let me know what you think!

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Sun, 09 May 2021 20:50:05 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
Comment-In-Reply-To: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-MessageType: comment

Jay Conrod (Gerrit)

unread,
May 10, 2021, 1:51:05 PM5/10/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Paschalis Tsilias, Michael Matloob.

Patch set 4:Trust +1

View Change

1 comment:

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • Patch Set #4, Line 66:

      Also check a path with a space in the name.

      If that works, please add "Fixes #46085" to the commit message.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Mon, 10 May 2021 17:51:01 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes
Gerrit-MessageType: comment

Paschalis Tsilias (Gerrit)

unread,
Jul 26, 2021, 4:32:27 PM7/26/21
to goph...@pubsubhelper.golang.org, Jay Conrod, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Michael Matloob.

View Change

1 comment:

  • Patchset:

    • Patch Set #4:

      Since the latest changes in CL 320310 which deprecated `checkModulePathLax` in favor of `module.CheckImportPath` I'm not sure if the proposed changes here make sense, or if they should be applied on the external `module` package.

      In any case, I'll have to take another look. Since this is one of the few tickets left for 1.17, maybe it should get bumped for 1.18 to trim down what's left for the milestone.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Mon, 26 Jul 2021 20:32:20 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Gerrit-MessageType: comment

Jay Conrod (Gerrit)

unread,
Jul 27, 2021, 4:08:21 PM7/27/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

View Change

1 comment:

  • Patchset:

    • Patch Set #4:

      Since the latest changes in CL 320310 which deprecated `checkModulePathLax` in favor of `module. […]

      I think this change still makes sense, but it will need to be updated.

      'go mod init' is a bit strange in that it needs to be less strict than module.CheckModulePath but more strict than module.CheckImportPath: the module path definitely needs to be a valid import path, but paths like "example" or "test" are allowed for local modules, even though they aren't technically valid and won't work with 'go get'.

      To move this forward, in modload.CreateModFile, after calling CheckImportPath, let's check module.SplitPathVersion and return those errors if it fails.

      I'll bump the issue for 1.18.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 4
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Tue, 27 Jul 2021 20:08:15 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No

Paschalis Tsilias (Gerrit)

unread,
Aug 13, 2021, 9:56:30 AM8/13/21
to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

Attention is currently required from: Paschalis Tsilias, Michael Matloob.

Paschalis Tsilias uploaded patch set #5 to this change.

View Change

cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes and return a relevant error

message along with a suggested fix.

Fixes #44052
Fixes #46085


Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 142 insertions(+), 0 deletions(-)

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 5
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: newpatchset

Paschalis Tsilias (Gerrit)

unread,
Aug 13, 2021, 10:07:40 AM8/13/21
to goph...@pubsubhelper.golang.org, Jay Conrod, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Jay Conrod, Michael Matloob.

View Change

4 comments:

  • Patchset:

    • Patch Set #4:

      I think this change still makes sense, but it will need to be updated. […]

      Thanks for the guidance and for explaining the difference. I think that it fit nicely there after CheckModulePath, let me know how it looks!

  • Patchset:

    • Patch Set #5:

      Back with a new patchset after rebasing with the changes in CL 320310. Following Jay's advice, it worked in the same way without any fuss.

      In the latest patchset

      • Used `module.SplitPathVersion` in CreateModFile
      • Added new test cases for paths with a trailing dot
      • Added new test case for paths with spaces, so that we can also close #46085

      Do you have any other edge cases in mind that we should check?

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • I found this behavior changing in CL 264621. […]

      This seems to be fixed somewhere around the CL 320310 revamp.

      I've added a testcase to make sure that any such path is rejected as a 'malformed module path' instead.

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • Also check a path with a space in the name. […]

      Done. Do you think we should change the name of the test script to something more generic now?

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 5
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Jay Conrod <jayc...@google.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 13 Aug 2021 14:07:34 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: No
Comment-In-Reply-To: Bryan C. Mills <bcm...@google.com>
Comment-In-Reply-To: Paschalis Tsilias <paschali...@gmail.com>

Jay Conrod (Gerrit)

unread,
Aug 13, 2021, 1:42:50 PM8/13/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, Bryan C. Mills, Michael Matloob, Go Bot, golang-co...@googlegroups.com

Attention is currently required from: Bryan C. Mills, Paschalis Tsilias, Michael Matloob.

Patch set 5:Run-TryBot +1Code-Review +2Trust +1

View Change

2 comments:

  • Patchset:

    • Patch Set #5:

      TRY=linux-amd64-longtest

      Looks good, thanks for working on this.

  • File src/cmd/go/testdata/script/mod_init_invalid_major.txt:

    • Done. […]

      Test name seems fine to me.

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 5
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-CC: Go Bot <go...@golang.org>
Gerrit-Attention: Bryan C. Mills <bcm...@google.com>
Gerrit-Attention: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Attention: Michael Matloob <mat...@golang.org>
Gerrit-Comment-Date: Fri, 13 Aug 2021 17:42:45 +0000
Gerrit-HasComments: Yes
Gerrit-Has-Labels: Yes

Jay Conrod (Gerrit)

unread,
Aug 17, 2021, 5:34:37 PM8/17/21
to Paschalis Tsilias, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Go Bot, Bryan C. Mills, Michael Matloob, golang-co...@googlegroups.com

Jay Conrod submitted this change.

View Change

Approvals: Jay Conrod: Looks good to me, approved; Trusted; Run TryBots Bryan C. Mills: Trusted Go Bot: TryBots succeeded
cmd/go: make mod init disallow invalid major version suffixes

This CL reuses the SplitPathVersion function from the module package to
detect invalid major version suffixes and return a relevant error
message along with a suggested fix.

Fixes #44052
Fixes #46085

Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Reviewed-on: https://go-review.googlesource.com/c/go/+/288712
Reviewed-by: Jay Conrod <jayc...@google.com>
Trust: Jay Conrod <jayc...@google.com>
Trust: Bryan C. Mills <bcm...@google.com>
Run-TryBot: Jay Conrod <jayc...@google.com>
TryBot-Result: Go Bot <go...@golang.org>

---
M src/cmd/go/internal/modload/init.go
A src/cmd/go/testdata/script/mod_init_invalid_major.txt
2 files changed, 142 insertions(+), 0 deletions(-)

diff --git a/src/cmd/go/internal/modload/init.go b/src/cmd/go/internal/modload/init.go
index d5f9d10..862b007 100644
--- a/src/cmd/go/internal/modload/init.go
+++ b/src/cmd/go/internal/modload/init.go
@@ -523,6 +523,13 @@
}
}
base.Fatalf("go: %v", err)
+ } else if _, _, ok := module.SplitPathVersion(modPath); !ok {
+ if strings.HasPrefix(modPath, "gopkg.in/") {
+ invalidMajorVersionMsg := fmt.Errorf("module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN:\n\tgo mod init %s", suggestGopkgIn(modPath))
+ base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
+ }
+ invalidMajorVersionMsg := fmt.Errorf("major version suffixes must be in the form of /vN and are only allowed for v2 or later:\n\tgo mod init %s", suggestModulePath(modPath))
+ base.Fatalf(`go: invalid module path "%v": %v`, modPath, invalidMajorVersionMsg)
}

fmt.Fprintf(os.Stderr, "go: creating new go.mod: module %s\n", modPath)
@@ -1197,3 +1204,56 @@
func modkey(m module.Version) module.Version {
return module.Version{Path: m.Path, Version: m.Version + "/go.mod"}
}
+
+func suggestModulePath(path string) string {
+ var m string
+
+ i := len(path)
+ for i > 0 && ('0' <= path[i-1] && path[i-1] <= '9' || path[i-1] == '.') {
+ i--
+ }
+ url := path[:i]
+ url = strings.TrimSuffix(url, "/v")
+ url = strings.TrimSuffix(url, "/")
+
+ f := func(c rune) bool {
+ return c > '9' || c < '0'
+ }
+ s := strings.FieldsFunc(path[i:], f)
+ if len(s) > 0 {
+ m = s[0]
+ }
+ m = strings.TrimLeft(m, "0")
+ if m == "" || m == "1" {
+ return url + "/v2"
+ }
+
+ return url + "/v" + m
+}
+
+func suggestGopkgIn(path string) string {
+ var m string
+ i := len(path)
+ for i > 0 && (('0' <= path[i-1] && path[i-1] <= '9') || (path[i-1] == '.')) {
+ i--
+ }
+ url := path[:i]
+ url = strings.TrimSuffix(url, ".v")
+ url = strings.TrimSuffix(url, "/v")
+ url = strings.TrimSuffix(url, "/")
+
+ f := func(c rune) bool {
+ return c > '9' || c < '0'
+ }
+ s := strings.FieldsFunc(path, f)
+ if len(s) > 0 {
+ m = s[0]
+ }
+
+ m = strings.TrimLeft(m, "0")
+
+ if m == "" {
+ return url + ".v1"
+ }
+ return url + ".v" + m
+}

diff --git a/src/cmd/go/testdata/script/mod_init_invalid_major.txt b/src/cmd/go/testdata/script/mod_init_invalid_major.txt
new file mode 100644
index 0000000..ae93e70
--- /dev/null
+++ b/src/cmd/go/testdata/script/mod_init_invalid_major.txt
@@ -0,0 +1,82 @@

+env GO111MODULE=on
+env GOFLAGS=-mod=mod
+
+! go mod init example.com/user/repo/v0
+stderr '(?s)^go: invalid module path "example.com/user/repo/v0": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+stderr '(?s)^go: invalid module path "example.com/user/repo/v02": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+
+! go mod init example.com/user/repo/v023
+stderr '(?s)^go: invalid module path "example.com/user/repo/v023": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v23$'

+
+! go mod init example.com/user/repo/v1
+stderr '(?s)^go: invalid module path "example.com/user/repo/v1": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+stderr '(?s)^go: invalid module path "example.com/user/repo/v2.0": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+stderr '(?s)^go: invalid module path "example.com/user/repo/v2.1.4": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+
+! go mod init example.com/user/repo/v3.5
+stderr '(?s)^go: invalid module path "example.com/user/repo/v3.5": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v3$'
+
+! go mod init example.com/user/repo/v4.1.4
+stderr '(?s)^go: invalid module path "example.com/user/repo/v4.1.4": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v4$'
+
+! go mod init example.com/user/repo/v.2.3
+stderr '(?s)^go: invalid module path "example.com/user/repo/v.2.3": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v2$'
+
+! go mod init example.com/user/repo/v.5.3
+stderr '(?s)^go: invalid module path "example.com/user/repo/v.5.3": major version suffixes must be in the form of /vN and are only allowed for v2 or later(.*)go mod init example.com/user/repo/v5$'
+
+! go mod init gopkg.in/pkg
+stderr '(?s)^go: invalid module path "gopkg.in/pkg": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg/v0
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg/v0": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg/v1
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg/v1": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg/v2
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg/v2": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v2$'
+
+! go mod init gopkg.in/user/pkg.v
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg.v": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg.v0.1
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg.v0.1": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg.v.1
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg.v.1": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg.v01
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg.v01": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v1$'
+
+! go mod init gopkg.in/user/pkg.v.2.3
+stderr '(?s)^go: invalid module path "gopkg.in/user/pkg.v.2.3": module paths beginning with gopkg.in/ must always have a major version suffix in the form of .vN(.*)go mod init gopkg.in/user/pkg.v2$'
+
+# module paths with a trailing dot are rejected as invalid import paths

+! go mod init example.com/user/repo/v2.
+stderr '(?s)^go: malformed module path "example.com/user/repo/v2.": trailing dot in path element$'
+
+! go mod init example.com/user/repo/v2..
+stderr '(?s)^go: malformed module path "example.com/user/repo/v2..": trailing dot in path element$'
+
+! go mod init gopkg.in/user/pkg.v.2.
+stderr '(?s)^go: malformed module path "gopkg.in/user/pkg.v.2.": trailing dot in path element$'
+
+! go mod init gopkg.in/user/pkg.v.2..
+stderr '(?s)^go: malformed module path "gopkg.in/user/pkg.v.2..": trailing dot in path element$'
+
+# module paths with spaces are also rejected
+! go mod init 'foo bar'
+stderr '(?s)^go: malformed module path "foo bar": invalid char '' ''$'
+
+! go mod init 'foo bar baz'
+stderr '(?s)^go: malformed module path "foo bar baz": invalid char '' ''$'

To view, visit change 288712. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: I6c06f31a134e864a1d9b6e00c048ca1c59b4365e
Gerrit-Change-Number: 288712
Gerrit-PatchSet: 6
Gerrit-Owner: Paschalis Tsilias <paschali...@gmail.com>
Gerrit-Reviewer: Bryan C. Mills <bcm...@google.com>
Gerrit-Reviewer: Go Bot <go...@golang.org>
Gerrit-Reviewer: Jay Conrod <jayc...@google.com>
Gerrit-Reviewer: Michael Matloob <mat...@golang.org>
Gerrit-MessageType: merged
Reply all
Reply to author
Forward
0 new messages