golang/go · error

cannot run go list: %v %s

Error message

cannot run go list: %v
%s

What it means

Thrown by 'go tool cover' (func.go) when invoking `go list -e -json <pkgs>` fails (non-zero exit / exec error). The cover tool runs the go binary at $GOROOT/bin/go to resolve package metadata for source-file lookup during -func/-html report generation. The error wraps both the exec error and the command's stderr output.

Source

Thrown at src/cmd/cover/func.go:208

		if _, ok := pkgs[pkg]; !ok {
			pkgs[pkg] = nil
			list = append(list, pkg)
		}
	}

	if len(list) == 0 {
		return pkgs, nil
	}

	// Note: usually run as "go tool cover" in which case $GOROOT is set,
	// in which case runtime.GOROOT() does exactly what we want.
	goTool := filepath.Join(runtime.GOROOT(), "bin/go")
	cmd := exec.Command(goTool, append([]string{"list", "-e", "-json"}, list...)...)
	var stderr bytes.Buffer
	cmd.Stderr = &stderr
	stdout, err := cmd.Output()
	if err != nil {
		return nil, fmt.Errorf("cannot run go list: %v\n%s", err, stderr.Bytes())
	}
	dec := json.NewDecoder(bytes.NewReader(stdout))
	for {
		var pkg Pkg
		err := dec.Decode(&pkg)
		if err == io.EOF {
			break
		}
		if err != nil {
			return nil, fmt.Errorf("decoding go list json: %v", err)
		}
		pkgs[pkg.ImportPath] = &pkg
	}
	return pkgs, nil
}

// findFile finds the location of the named file in GOROOT, GOPATH etc.
func findFile(pkgs map[string]*Pkg, file string) (string, error) {

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure GOROOT is correct and $GOROOT/bin/go exists and runs (`go env GOROOT`)
  2. Run the equivalent `go list -e -json <pkg>` manually to see the real error from stderr
  3. Fix module resolution: `go mod tidy`, GOPROXY/GOPRIVATE settings, or restore the module cache
  4. Re-run `go test -coverprofile` to regenerate a fresh profile against the current module set

Example fix

# diagnose
GOROOT=/bad go tool cover -func=c.out  # fails
# fix
go env -w GOROOT=$(go env GOROOT)  # or reinstall Go
go list -e -json $(awk '{print $1}' c.out | cut -d: -f1 | xargs -n1 dirname | sort -u)
Defensive patterns

Strategy: validation

Validate before calling

# Pre-flight: ensure go binary exists and go list works
if [ ! -x "$(go env GOROOT)/bin/go" ]; then echo "missing go binary" >&2; exit 2; fi
go list -e -json ./... >/dev/null || { echo "go list failed" >&2; exit 2; }

Prevention

When it happens

Trigger: `go tool cover -func=profile.out` or `-html=profile.out` where the spawned `go list` fails: corrupt GOROOT, missing go binary, network/module download failure for a package referenced in the profile, or `go list` itself errors on a broken module.

Common situations: GOROOT unset or pointing at a deleted install. Profile referencing packages from a module that can no longer be resolved (deleted cache, GOPROXY issues, private module auth). Broken go.mod/go.sum. go binary not executable.

Related errors


AI-assisted analysis of golang/go@b6b368adc5 (2026-08-12). Data as JSON: /api/errors/4f94fb049965c252. Report an issue: GitHub.