grafana/k6 · error

failed to encode project list: %w

Error message

failed to encode project list: %w

What it means

`k6 cloud project list --json` (cmdCloudProjectList.outputJSON) encodes the fetched project slice with encoding/json and wraps an encoder failure here. The Project model contains only serializable fields, so Encode failing is a near-impossible internal condition rather than a response to bad input.

Source

Thrown at internal/cmd/cloud_project_list.go:79

	if len(projects) == 0 {
		printToStdout(c.globalState, stackHeader+
			"No projects found.\n"+
			"To create a project, visit https://grafana.com/docs/grafana-cloud/testing/k6/projects-and-users/projects/\n")
		return nil
	}

	printToStdout(c.globalState, stackHeader+formatProjectTable(projects))
	return nil
}

func (c *cmdCloudProjectList) outputJSON(projects []cloudapiv6.Project) error {
	buf := &bytes.Buffer{}
	enc := json.NewEncoder(buf)
	enc.SetEscapeHTML(false)
	enc.SetIndent("", "  ")
	if err := enc.Encode(projects); err != nil {
		return fmt.Errorf("failed to encode project list: %w", err)
	}

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

func formatProjectTable(projects []cloudapiv6.Project) string {
	var buf strings.Builder
	w := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)
	_, _ = fmt.Fprintln(w, "ID\tNAME\tDEFAULT")
	for _, p := range projects {
		def := "no"
		if p.IsDefault {
			def = "yes"
		}
		_, _ = fmt.Fprintf(w, "%d\t%s\t%s\n", p.ID, p.Name, def)
	}
	_ = w.Flush()

View on GitHub (pinned to 93accf6570)

Solutions

  1. Run `k6 cloud project list` without --json to confirm data retrieval works
  2. Switch to a known-good k6 version
  3. Report the defect to grafana/k6 with version and output

Example fix

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

Strategy: try-catch

Try / catch

if err := cmdCloudProjectList.outputJSON(projects); err != nil {
    // encoder failure is internal: fall back to the table formatter
    printToStdout(gs, formatProjectTable(projects))
}

Prevention

When it happens

Trigger: Running `k6 cloud project list --json` when json.Encoder.Encode errors; realistically only after a k6 change introduces a non-serializable field to the Project type.

Common situations: Practically never hit; would occur uniformly across environments immediately after upgrading k6.

Related errors


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