{"record":{"id":"908c4969c8eb2dfb","repo":"grafana/k6","slug":"failed-to-encode-test-list-w","errorCode":null,"errorMessage":"failed to encode test list: %w","messagePattern":"failed to encode test list: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/cloud_loadtest_list.go","lineNumber":121,"sourceCode":"\tif id == 0 {\n\t\treturn 0, errNoProjectConfigured\n\t}\n\n\treturn id, nil\n}\n\nfunc (c *cmdCloudTestList) outputJSON(tests []v6cloudapi.LoadTest) error {\n\t// If tests is nil, initialize it to an empty slice to avoid encoding it as null.\n\tif tests == nil {\n\t\ttests = []v6cloudapi.LoadTest{}\n\t}\n\n\tbuf := &bytes.Buffer{}\n\tenc := json.NewEncoder(buf)\n\tenc.SetEscapeHTML(false)\n\tenc.SetIndent(\"\", \"  \")\n\tif err := enc.Encode(tests); err != nil {\n\t\treturn fmt.Errorf(\"failed to encode test list: %w\", err)\n\t}\n\n\tprintToStdout(c.globalState, buf.String())\n\treturn nil\n}\n\nfunc formatLoadTestTable(tests []v6cloudapi.LoadTest) string {\n\tconst dateFormat = \"2006-01-02 15:04\"\n\n\tvar buf strings.Builder\n\tw := tabwriter.NewWriter(&buf, 0, 0, 3, ' ', 0)\n\t_, _ = fmt.Fprintln(w, \"ID\\tNAME\\tCREATED\\tUPDATED\")\n\tfor _, t := range tests {\n\t\t_, _ = fmt.Fprintf(w, \"%d\\t%s\\t%s\\t%s\\n\",\n\t\t\tt.ID, t.Name,\n\t\t\tt.Created.UTC().Format(dateFormat),\n\t\t\tt.Updated.UTC().Format(dateFormat),\n\t\t)","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/grafana/k6/blob/93accf6570dcd306ca5e99cc44c393ee3797761b/internal/cmd/cloud_loadtest_list.go#L103-L139","documentation":"`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.","triggerScenarios":"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.","commonSituations":"Practically unseen; would reproduce on every invocation with --json across environments right after upgrading k6.","solutions":["Run `k6 cloud test list` without --json to verify the API call succeeds","Pin k6 to a version where --json works, or wait for a patch release","File an issue at grafana/k6 including the version and output"],"exampleFix":"# before\nk6 cloud test list --json\n# after\nk6 cloud test list  # table output as a workaround","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if err := cmdCloudTestList.outputJSON(tests); err != nil {\n    // fall back to table output; the API data is fine, encoding is the defect\n    printToStdout(gs, formatLoadTestTable(tests))\n}","preventionTips":["Pin k6 versions and smoke-test --json after upgrades","Keep a table-output fallback in wrapper scripts that parse JSON","Report encoder failures to grafana/k6 with the version"],"tags":["go","json","cli","internal-error"],"backgroundTag":null,"analyzedSha":"93accf6570dcd306ca5e99cc44c393ee3797761b","analyzedAt":"2026-08-15T21:23:27.118Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}