grafana/k6 · error

failed to encode test list: %w

Error message

failed to encode test list: %w

What it means

`k6 cloud test list --json` (cmdCloudTestList.outputJSON) encodes the fetched test slice with encoding/json, initializing nil to an empty slice first so the output is never null, and wraps any Encode failure here. Like the other list commands, the LoadTest model is plain data, so an encoder failure points to an internal defect, not user configuration.

Source

Thrown at internal/cmd/cloud_loadtest_list.go:121

	if id == 0 {
		return 0, errNoProjectConfigured
	}

	return id, nil
}

func (c *cmdCloudTestList) outputJSON(tests []v6cloudapi.LoadTest) error {
	// If tests is nil, initialize it to an empty slice to avoid encoding it as null.
	if tests == nil {
		tests = []v6cloudapi.LoadTest{}
	}

	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", "  ")
	if err := enc.Encode(tests); err != nil {
		return fmt.Errorf("failed to encode test list: %w", err)
	}

	printToStdout(c.globalState, buf.String())
	return nil
}

func formatLoadTestTable(tests []v6cloudapi.LoadTest) string {
	const dateFormat = "2006-01-02 15:04"

	var buf strings.Builder
	w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
	_, _ = fmt.Fprintln(w, "ID\tNAME\tCREATED\tUPDATED")
	for _, t := range tests {
		_, _ = fmt.Fprintf(w, "%d\t%s\t%s\t%s\n",
			t.ID, t.Name,
			t.Created.UTC().Format(dateFormat),
			t.Updated.UTC().Format(dateFormat),
		)

View on GitHub (pinned to 93accf6570)

Solutions

  1. Run `k6 cloud test list` without --json to verify the API call succeeds
  2. Pin k6 to a version where --json works, or wait for a patch release
  3. File an issue at grafana/k6 including the version and output

Example fix

# before
k6 cloud test list --json
# after
k6 cloud test list  # table output as a workaround
Defensive patterns

Strategy: try-catch

Try / catch

if err := cmdCloudTestList.outputJSON(tests); err != nil {
    // fall back to table output; the API data is fine, encoding is the defect
    printToStdout(gs, formatLoadTestTable(tests))
}

Prevention

When it happens

Trigger: Running `k6 cloud test list --json` when json.Encoder.Encode returns an error; only plausible if a LoadTest field becomes non-serializable after a k6 upgrade.

Common situations: Practically unseen; would reproduce on every invocation with --json across environments right after upgrading k6.

Related errors


AI-assisted analysis of grafana/k6@93accf6570 (2026-08-15). Data as JSON: /api/errors/908c4969c8eb2dfb. Report an issue: GitHub.