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: "index out of range" panic with "list -e" involving multiple packages #59905

Closed
mvdan opened this issue Apr 30, 2023 · 3 comments
Closed
Assignees
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Milestone

Comments

@mvdan
Copy link
Member

mvdan commented Apr 30, 2023

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

$ go version
go version devel go1.21-0fd6ae548f Fri Apr 28 20:33:34 2023 +0000 linux/amd64

Does this issue reproduce with the latest release?

No, this panic does not happen with Go 1.20.3.

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/mvdan/.cache/go-build"
GOENV="/home/mvdan/.config/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GOMODCACHE="/home/mvdan/go/pkg/mod"
GONOPROXY="github.com/cue-unity"
GONOSUMDB="github.com/cue-unity"
GOOS="linux"
GOPATH="/home/mvdan/go"
GOPRIVATE="github.com/cue-unity"
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/home/mvdan/tip"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/home/mvdan/tip/pkg/tool/linux_amd64"
GOVCS=""
GOVERSION="devel go1.21-0fd6ae548f Fri Apr 28 20:33:34 2023 +0000"
GCCGO="gccgo"
GOAMD64="v3"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD="/dev/null"
GOWORK=""
CGO_CFLAGS="-O2 -g"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-O2 -g"
CGO_FFLAGS="-O2 -g"
CGO_LDFLAGS="-O2 -g"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build374947041=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I was writing a test for a tool of mine, and ran into a go list panic. I minimized it as follows, which can be run with https://pkg.go.dev/github.com/rogpeppe/go-internal/cmd/testscript:

exec go list -json -export -e -deps

-- go.mod --
module test/main

go 1.20
-- main.go --
package main

import (
	"test/main/level1a"
	"test/main/level1b"
)

func main() {
	level1a.Print()
	level1b.Print()
}
-- level1a/pkg.go --
package level1a

import (
	"test/main/level1a/level2x"
	"test/main/level1a/level2y"
)

func Print() { println(level2x.Value, level2y.Value) }
-- level1a/level2x/pkg.go --
package level2x

var Value = "1a/2x"
-- level1a/level2y/pkg.go --
package level2x

var Value = "1a/2y"
-- level1b/pkg.go --
package level1b

import (
	"test/main/level1b/level2x"
	"test/main/level1b/level2y"
)

func Print() { println(level2x.Value, level2y.Value) }
-- level1b/level2x/pkg.go --
package level2x

var Value = "1b/2x"
-- level1b/level2y/pkg.go --
package level2x

var Value = "1b/2y"

What did you expect to see?

Note that the input Go packages are broken due to copy-paste mistakes of mine. You can see the valid errors if you use go run instead:

> exec go run .
[stderr]
# test/main/level1a
level1a/pkg.go:5:2: level2x redeclared in this block
	level1a/pkg.go:4:2: other declaration of level2x
level1a/pkg.go:5:2: "test/main/level1a/level2y" imported as level2x and not used
level1a/pkg.go:8:39: undefined: level2y
# test/main/level1b
level1b/pkg.go:5:2: level2x redeclared in this block
	level1b/pkg.go:4:2: other declaration of level2x
level1b/pkg.go:5:2: "test/main/level1b/level2y" imported as level2x and not used
level1b/pkg.go:8:39: undefined: level2y
[exit status 1]

What did you see instead?

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
panic({0xa06e40?, 0xc000115740?})
	/home/mvdan/tip/src/runtime/panic.go:1017 +0x3ac fp=0xc000198ef0 sp=0xc000198e40 pc=0x4347cc
runtime.goPanicIndex(0xffffffffffffffff, 0x0)
	/home/mvdan/tip/src/runtime/panic.go:114 +0x7c fp=0xc000198f30 sp=0xc000198ef0 pc=0x43283c
cmd/go/internal/list.collectDepsErrors.func1(0xc000236f78?, 0xc000199020?)
	/home/mvdan/tip/src/cmd/go/internal/list/list.go:951 +0x96 fp=0xc000198f60 sp=0xc000198f30 pc=0x913b76
sort.insertionSort_func({0xc000199140?, 0xc0002a9240?}, 0x0, 0x2)
	/home/mvdan/tip/src/sort/zsortfunc.go:12 +0xa7 fp=0xc000198fa8 sp=0xc000198f60 pc=0x49a507
sort.pdqsort_func({0xc000199140?, 0xc0002a9240?}, 0x7f6c22f8d5b8?, 0x18?, 0xc000100000?)
	/home/mvdan/tip/src/sort/zsortfunc.go:73 +0x31b fp=0xc000199050 sp=0xc000198fa8 pc=0x49ab1b
sort.Slice({0x977ac0?, 0xc000236f78?}, 0x2?)
	/home/mvdan/tip/src/sort/slice.go:26 +0xf2 fp=0xc0001990c0 sp=0xc000199050 pc=0x499eb2
cmd/go/internal/list.collectDepsErrors(0xc000252000)
	/home/mvdan/tip/src/cmd/go/internal/list/list.go:949 +0x2fe fp=0xc000199248 sp=0xc0001990c0 pc=0x913a5e
cmd/go/internal/list.runList({0xb3fc38?, 0xead0e0}, 0xe71d40?, {0xc00001e230?, 0x0, 0x0})
	/home/mvdan/tip/src/cmd/go/internal/list/list.go:805 +0x21a5 fp=0xc000199d20 sp=0xc000199248 pc=0x911565
main.invoke(0xe71d40, {0xc00001e1f0, 0x5, 0x5})
	/home/mvdan/tip/src/cmd/go/main.go:243 +0x4d6 fp=0xc000199e10 sp=0xc000199d20 pc=0x942576
main.main()
	/home/mvdan/tip/src/cmd/go/main.go:183 +0x7d1 fp=0xc000199f40 sp=0xc000199e10 pc=0x941d71
runtime.main()
	/home/mvdan/tip/src/runtime/proc.go:267 +0x2d2 fp=0xc000199fe0 sp=0xc000199f40 pc=0x437472
runtime.goexit()
	/home/mvdan/tip/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc000199fe8 sp=0xc000199fe0 pc=0x4696a1

I guess there's no guarantee that ImportStack is non-empty?

FYI @matloob, as the code was recently added in https://go-review.googlesource.com/c/go/+/483515.

@cagedmantis cagedmantis added NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. GoCommand cmd/go labels May 1, 2023
@cagedmantis cagedmantis added this to the Backlog milestone May 1, 2023
@bcmills bcmills modified the milestones: Backlog, Go1.21 Jun 8, 2023
@gopherbot
Copy link

Change https://go.dev/cl/501978 mentions this issue: cmd/go: don't assume ImportStack always set on PackageError

@matloob matloob self-assigned this Jun 10, 2023
@dmitshur dmitshur added NeedsFix The path to resolution is known, but the work has not been done. and removed NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Aug 7, 2023
@gopherbot
Copy link

Change https://go.dev/cl/516739 mentions this issue: cmd/go: fix missing case checking for empty slice

gopherbot pushed a commit that referenced this issue Aug 9, 2023
When we were comparing the first element of import stacks when sorting
depserrors we checked if the first stack was non empty, but not the
second one. Do the check for both stacks.

Fixes #61816
For #59905

Change-Id: Id5c11c2b1104eec93196a08c53372ee2ba97c701
Reviewed-on: https://go-review.googlesource.com/c/go/+/516739
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
@gopherbot
Copy link

Change https://go.dev/cl/519658 mentions this issue: [release-branch.go1.21] cmd/go: fix missing case checking for empty slice

gopherbot pushed a commit that referenced this issue Aug 15, 2023
…lice

When we were comparing the first element of import stacks when sorting
depserrors we checked if the first stack was non empty, but not the
second one. Do the check for both stacks.

Fixes #61818
Updates #61816
For #59905

Change-Id: Id5c11c2b1104eec93196a08c53372ee2ba97c701
Reviewed-on: https://go-review.googlesource.com/c/go/+/516739
Reviewed-by: Bryan Mills <bcmills@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
(cherry picked from commit 58447d7)
Reviewed-on: https://go-review.googlesource.com/c/go/+/519658
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
GoCommand cmd/go NeedsFix The path to resolution is known, but the work has not been done. release-blocker
Projects
None yet
Development

No branches or pull requests

6 participants