{"record":{"id":"af679ec37a78e9a4","repo":"kubernetes/kops","slug":"error-marshaling-json-v-af679e","errorCode":null,"errorMessage":"error marshaling json: %v","messagePattern":"error marshaling json: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/commands/helpers/kubectl_auth.go","lineNumber":138,"sourceCode":"\t\tklog.Infof(\"cached credential had wrong api version\")\n\t\tcached = nil\n\t}\n\n\tisCached := false\n\tif cached != nil {\n\t\texecCredential = cached\n\t\tisCached = true\n\t} else {\n\t\tstatus, err := buildCredentials(ctx, f, options)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\texecCredential.Status = *status\n\t}\n\n\tb, err := json.MarshalIndent(execCredential, \"\", \"  \")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error marshaling json: %v\", err)\n\t}\n\t_, err = out.Write(b)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error writing to stdout: %v\", err)\n\t}\n\n\tif !isCached {\n\t\tif err := os.MkdirAll(filepath.Dir(cacheFilePath), 0o755); err != nil {\n\t\t\tklog.Warningf(\"failed to make cache directory for %q: %v\", cacheFilePath, err)\n\t\t}\n\t\tif err := os.WriteFile(cacheFilePath, b, 0o600); err != nil {\n\t\t\tklog.Warningf(\"failed to write cache file %q: %v\", cacheFilePath, err)\n\t\t}\n\t}\n\n\treturn nil\n}\n","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/pkg/commands/helpers/kubectl_auth.go#L120-L156","documentation":"After assembling the ExecCredential (cached or freshly issued), the helper serializes it with json.MarshalIndent for output to kubectl. A marshaling failure returns this wrapped error. In practice this is nearly impossible with the fixed ExecCredential struct (only string, time.Time fields), so it almost always indicates corrupt in-memory data such as a cache object with a non-serializable time value.","triggerScenarios":"json.MarshalIndent(execCredential, ...) returns an error — e.g. a cached ExecCredential unmarshaled into the struct carries an invalid/unsupported field state, or an environment where encoding/json is restricted/patched.","commonSituations":"Hand-edited cache files in ~/.kube/cache/kops-authentication feeding pathological values; running a heavily modified/custom build; extremely rare Go runtime issues.","solutions":["Delete the stale cache file under ~/.kube/cache/kops-authentication/ and re-run so credentials are rebuilt.","Check klog output for 'cached credential ... was not valid' and remove the referenced file.","If reproducible in a custom build, inspect the ExecCredential content with a debugger."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"err := helpers.RunKubectlAuthHelper(ctx, f, out, options)\nif err != nil && strings.Contains(err.Error(), \"error marshaling json\") {\n    // clear cache and retry once\n    os.RemoveAll(filepath.Join(homedir.HomeDir(), \".kube\", \"cache\", \"kops-authentication\"))\n    err = helpers.RunKubectlAuthHelper(ctx, f, out, options)\n}","preventionTips":["Do not hand-edit files under ~/.kube/cache/kops-authentication/.","Treat cache-load warnings in klog as signals to clear the cache.","Use official kOps builds."],"tags":["json","serialization","cache"],"backgroundTag":"json-marshal-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T07:17:12.445Z"}