Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cmd/go: panic during 'go mod tidy' if a line ends with an empty comment #35737

Closed
MToolMakerJB opened this issue Nov 21, 2019 · 4 comments
Closed
Labels
FrozenDueToAge help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Milestone

Comments

@MToolMakerJB
Copy link

MToolMakerJB commented Nov 21, 2019

What version of Go are you using (go version)?

$ go version
go version go1.13.4 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/ivan.yarkov/Library/Caches/go-build"
GOENV="/Users/ivan.yarkov/Library/Application Support/go/env"
GOEXE=""
GOFLAGS=" -mod="
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/ivan.yarkov/go"
GOPRIVATE=""
GOPROXY="direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/ivan.yarkov/awesomeProject11/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/bf/qzrd9gxj04j10kx_8lt3pbrm0000gp/T/go-build568351457=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

module awesomeProject11
Run go mod tidy on the following example:

go 1.13

require (
	github.com/inconshreveable/mousetrap v1.0.0 //
	github.com/spf13/cobra v0.0.3
	github.com/spf13/pflag v1.0.5 // indirect
)

What did you expect to see?

I expect it work without failing.

What did you see instead?

I get:

panic: runtime error: index out of range [2] with length 2

goroutine 1 [running]:
cmd/go/internal/modfile.setIndirect(0xc0000c3900, 0xc00015f501)
        /usr/local/go/src/cmd/go/internal/modfile/rule.go:335 +0x31b
cmd/go/internal/modfile.(*File).SetRequire(0xc000032480, 0xc0002f5640, 0x3, 0x4)
        /usr/local/go/src/cmd/go/internal/modfile/rule.go:571 +0xb0a
cmd/go/internal/modload.WriteGoMod()
        /usr/local/go/src/cmd/go/internal/modload/init.go:673 +0x2ea
cmd/go/internal/modload.loadAll(0x78a00101, 0x7, 0xc000024048, 0x6)
        /usr/local/go/src/cmd/go/internal/modload/load.go:386 +0x215
cmd/go/internal/modload.LoadALL(...)
        /usr/local/go/src/cmd/go/internal/modload/load.go:364
cmd/go/internal/modcmd.runTidy(0x1aa5680, 0xc0000201d0, 0x0, 0x0)
        /usr/local/go/src/cmd/go/internal/modcmd/tidy.go:48 +0xb1
main.main()
        /usr/local/go/src/cmd/go/main.go:189 +0x57f

I think the problem is absence parentheses:

if len(com.Token) > 2 && com.Token[2] == ' ' || com.Token[2] == '\t' {
     space = ""
}

It should probably be:

if len(com.Token) > 2 && (com.Token[2] == ' ' || com.Token[2] == '\t') {
     space = ""
}

@bcmills bcmills added help wanted NeedsFix The path to resolution is known, but the work has not been done. labels Nov 21, 2019
@bcmills bcmills changed the title go mod tidy fails cmd/go: panic during 'go mod tidy' if a line ends with an empty comment Nov 21, 2019
@bcmills bcmills added this to the Go1.14 milestone Nov 21, 2019
@gopherbot
Copy link

Change https://golang.org/cl/208273 mentions this issue: modfile: handle indirect dependencies with an empty comment

@fsouza
Copy link
Contributor

fsouza commented Nov 22, 2019

@bcmills I've started working on this, I have a reproducer script in the main repo and a proposed fix in x/mod.

I haven't used gerrit in a while though, let me know if I do anything wrong 🙈

@jayconrod jayconrod self-assigned this Nov 25, 2019
gopherbot pushed a commit to golang/mod that referenced this issue Nov 25, 2019
This was reported in golang/go#35737.

Change-Id: I67383fbf668adea1c22feb7f8d25cd1652edcea5
Reviewed-on: https://go-review.googlesource.com/c/mod/+/208273
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
@gopherbot
Copy link

Change https://golang.org/cl/208671 mentions this issue: modfile: replace empty comments when adding indirect

gopherbot pushed a commit to golang/mod that referenced this issue Nov 26, 2019
When we add an "indirect" token to a line with an empty comment (only
whitespace after //), we should replace the comment text.

In CL 208273, we inserted "indirect;". The isIndirect predicate returns
false for "// indirect;" because there's only one word. When
modload.WriteGoMod is called multiple times (as it is in
'go mod tidy'), this caused us to write "// indirect; indirect".

Updates golang/go#35737

Change-Id: Ic2531a61ab15df7a0d4462ea3b1d25753d06d1d1
Reviewed-on: https://go-review.googlesource.com/c/mod/+/208671
Run-TryBot: Jay Conrod <jayconrod@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
@gopherbot
Copy link

Change https://golang.org/cl/208977 mentions this issue: cmd: update golang.org/x/mod and vendor

@golang golang locked and limited conversation to collaborators Nov 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
FrozenDueToAge help wanted modules NeedsFix The path to resolution is known, but the work has not been done.
Projects
None yet
Development

No branches or pull requests

5 participants