Skip to content

x/tools/go/packages: panic when NeedImports is used without NeedDeps mode #33077

Closed
@fatih

Description

@fatih

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

$ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes. This is from HEAD 9a621aea19f8341c01da59e0d42dd97700f677d0 of the tools repo (https://github.com/golang/tools)

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

go env Output
$ go env
GOARCH="amd64"
GOBIN="/Users/fatih/go/bin"
GOCACHE="/Users/fatih/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/fatih/go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/go"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
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/k_/87syx3r50m93m72hvqj2qqlw0000gn/T/go-build799516690=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

I run the following code:

package main

import (
	"flag"
	"fmt"
	"os"

	"golang.org/x/tools/go/packages"
)

func main() {
	flag.Parse()

	// Many tools pass their command-line arguments (after any flags)
	// uninterpreted to packages.Load so that it can interpret them
	// according to the conventions of the underlying build system.
	cfg := &packages.Config{
		Mode: packages.NeedFiles |
			packages.NeedImports |
			packages.NeedTypes |
			packages.NeedTypesInfo |
			packages.NeedSyntax,
	}
	pkgs, err := packages.Load(cfg, flag.Args()...)
	if err != nil {
		fmt.Fprintf(os.Stderr, "load: %v\n", err)
		os.Exit(1)
	}
	if packages.PrintErrors(pkgs) > 0 {
		os.Exit(1)
	}

	// Print the names of the source files
	// for each package listed on the command line.
	for _, pkg := range pkgs {
		fmt.Println(pkg.ID, pkg.GoFiles)
	}
}

with the following arg:

$ demo.go github.com/fatih/color
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1209c3e]

goroutine 7 [running]:
golang.org/x/tools/go/packages.(*loader).loadRecursive(0xc00010e0b0, 0x0)
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:661 +0x4e
golang.org/x/tools/go/packages.(*loader).loadRecursive.func1.1(0xc00010e0b0, 0xc00001ca80, 0x0)
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:668 +0x35
created by golang.org/x/tools/go/packages.(*loader).loadRecursive.func1
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:667 +0x13f
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x1209c3e]

goroutine 12 [running]:
golang.org/x/tools/go/packages.(*loader).loadRecursive(0xc00010e0b0, 0x0)
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:661 +0x4e
golang.org/x/tools/go/packages.(*loader).loadRecursive.func1.1(0xc00010e0b0, 0xc00001ca80, 0x0)
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:668 +0x35
created by golang.org/x/tools/go/packages.(*loader).loadRecursive.func1
        /Users/fatih/go/src/golang.org/x/tools/go/packages/packages.go:667 +0x13f
exit status 2

What did you expect to see?

The code should not panic when NeedDeps is not included the config.

What did you see instead?

If NeedDeps is included, it doesn't panic anymore:

cfg := &packages.Config{
	Mode: packages.NeedFiles |
		packages.NeedImports |
		packages.NeedDeps |
		packages.NeedTypes |
		packages.NeedTypesInfo |
		packages.NeedSyntax,
}
$demo.go github.com/fatih/color
github.com/fatih/color [/Users/fatih/go/src/github.com/fatih/color/color.go /Users/fatih/go/src/github.com/fatih/color/doc.go]

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions