kubernetes/kubernetes · error

json unmarshal of `go list` failed: %w

Error message

json unmarshal of `go list` failed: %w

What it means

Returned by findPkgDir() in hack/tools/instrumentation/main.go:301 when json.Unmarshal fails parsing the JSON output of `go list`. The tool expects a JSON object with a Dir field; a parse failure means go list produced output that is not valid JSON (or not the expected shape) despite exiting zero.

Source

Thrown at hack/tools/instrumentation/main.go:301

				}
			}
		}
	}
	return consts
}

func findPkgDir(pkg string) (string, error) {
	// Use Go's module mechanism.
	cmd := exec.Command("go", "list", "-find", "-json=Dir", pkg)
	out, err := cmd.CombinedOutput()
	if err != nil {
		return "", fmt.Errorf("running `go list` failed: %w\n\n%s", err, string(out))
	}
	result := struct {
		Dir string
	}{}
	if err := json.Unmarshal(out, &result); err != nil {
		return "", fmt.Errorf("json unmarshal of `go list` failed: %w", err)
	}
	if result.Dir != "" {
		return result.Dir, nil
	}

	return "", fmt.Errorf("empty respose from `go list`")
}

func importedGlobalVariableDeclaration(localVariables map[string]ast.Expr, imports []*ast.ImportSpec) (map[string]ast.Expr, error) {
	for _, im := range imports {
		// get imported label
		var importAlias string
		if im.Name == nil {
			pathSegments := strings.Split(im.Path.Value, "/")
			importAlias = strings.Trim(pathSegments[len(pathSegments)-1], "\"")
		} else {
			importAlias = im.Name.String()
		}

View on GitHub (pinned to 94c1367642)

Solutions

  1. Run `go list -find -json=Dir <pkg>` and inspect the raw output for non-JSON lines.
  2. Pin/align the Go toolchain version with what the repo expects (see hack/*.go-version or .go-version).
  3. Remove any `go` wrapper/alias that adds stdout output.
Defensive patterns

Strategy: validation

Validate before calling

// Detect non-JSON stdout from go list before running the tool.
out, _ := exec.Command("go", "list", "-find", "-json=Dir", pkg).Output()
if !json.Valid(out) { return fmt.Errorf("go list emits non-JSON; check toolchain/GOFLAGS") }

Prevention

When it happens

Trigger: A Go toolchain version that changes the `go list -json=Dir` output format, or interleaved non-JSON log lines (e.g. build warnings printed to stdout instead of stderr) corrupting the buffer.

Common situations: Upgrading the Go toolchain to a version with a different go list JSON schema; GOFLAGS/verbose logging that pollutes stdout; a wrapper around `go` (e.g. a shell alias) emitting extra text.

Related errors


AI-assisted analysis of kubernetes/kubernetes@94c1367642 (2026-08-08). Data as JSON: /api/errors/b2af5c3fae69b769. Report an issue: GitHub.