golang/go · error

unexpected stdout: %q

Error message

unexpected stdout: %q

What it means

Returned by the doc/dirs module-info probe when `go list -m -f <format>` produces fewer than 5 lines of stdout. The fixed template emits Path/Dir/GoVersion plus two trailing lines, so a short output means the module listing did not match the expected shape — usually because there is no module, the go command is the wrong version, or list failed silently (stderr already printed).

Source

Thrown at src/cmd/go/internal/doc/dirs.go:306

// getMainModuleAnd114 gets the main module's information and whether the
// go command in use is 1.14+. This is the information needed to figure out
// if vendoring should be enabled.
func getMainModuleAnd114() (*moduleJSON, bool, error) {
	const format = `{{.Path}}
{{.Dir}}
{{.GoVersion}}
{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}
`
	cmd := exec.Command(goCmd(), "list", "-m", "-f", format)
	cmd.Stderr = os.Stderr
	stdout, err := cmd.Output()
	if err != nil {
		return nil, false, nil
	}
	lines := strings.Split(string(stdout), "\n")
	if len(lines) < 5 {
		return nil, false, fmt.Errorf("unexpected stdout: %q", stdout)
	}
	mod := &moduleJSON{
		Path:      lines[0],
		Dir:       lines[1],
		GoVersion: lines[2],
	}
	return mod, lines[3] == "go1.14", nil
}

View on GitHub (pinned to b6b368adc5)

Solutions

  1. Ensure you are inside a module (`go mod init` if needed) or upgrade the project to modules.
  2. Run `go list -m` manually to see what it emits and fix the underlying module state.
  3. Run `go mod tidy` to repair module metadata.
  4. Confirm the go toolchain version matches what the project expects.

Example fix

# before: no go.mod in the tree
$ go doc ./...
# -> unexpected stdout

# after
$ go mod init example.com/mymodule
$ go doc ./...
Defensive patterns

Strategy: validation

Validate before calling

// before relying on the module probe, confirm a module exists
if _, err := os.Stat("go.mod"); err != nil {
    return errors.New("no go.mod: initialize a module first")
}

Try / catch

mod, ok, err := dirs.ModuleInfo(ctx)
if err != nil && strings.Contains(err.Error(), "unexpected stdout") {
    // run `go list -m` to surface the real cause, then retry
    _ = runShell("go", "list", "-m")
}

Prevention

When it happens

Trigger: Running go doc's module probe in a directory with no go.mod; with an old/new go version whose `go list -m` output differs; when go list exits 0 but emits only a partial block. The code path checks `len(lines) < 5` after splitting on newlines.

Common situations: GOPATH-mode projects (no module); mixed Go toolchain versions; corrupted module state; go list hitting an internal error that still returns 0.

Related errors


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