{"record":{"id":"ff2408d78bb02cf7","repo":"JanDeDobbeleer/oh-my-posh","slug":"failed-to-marshal-template-cache-w","errorCode":null,"errorMessage":"failed to marshal template cache: %w","messagePattern":"failed to marshal template cache: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli/config_export_data.go","lineNumber":136,"sourceCode":"// writeDataOutput prints doc to stdout, or writes it to --output when set.\n// Shared by the single-config path and the --themes merge path.\nfunc writeDataOutput(doc []byte) {\n\tif outputData == \"\" {\n\t\tfmt.Println(string(doc))\n\t\treturn\n\t}\n\n\tif err := os.WriteFile(cleanOutputPath(outputData), doc, 0o644); err != nil {\n\t\texitcode = 666\n\t\tfmt.Println(err.Error())\n\t}\n}\n\n// Extracted from dataCmd's Run so it can be unit tested without a real environment.\nfunc buildDataDocument(cfg *config.Config) ([]byte, error) {\n\tenvRaw, err := json.Marshal(template.Cache.SimpleTemplate)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal template cache: %w\", err)\n\t}\n\n\tvar envFields map[string]json.RawMessage\n\tif err := json.Unmarshal(envRaw, &envFields); err != nil {\n\t\treturn nil, fmt.Errorf(\"failed to marshal template cache: %w\", err)\n\t}\n\n\t// SegmentsCache is internal cache plumbing, and Var is already covered\n\t// by the config's own \"var\" section - neither belongs in a recorded\n\t// data file.\n\tdelete(envFields, \"SegmentsCache\")\n\tdelete(envFields, \"Var\")\n\n\tsegments := make(map[string]json.RawMessage)\n\n\tfor _, block := range cfg.Blocks {\n\t\tfor _, segment := range block.Segments {\n\t\t\twriter := segment.Writer()","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/cli/config_export_data.go#L118-L154","documentation":"buildDataDocument (backing `oh-my-posh config export data`) serializes the template cache's SimpleTemplate struct to JSON to build the \"env\" section. If json.Marshal on that struct fails, the command aborts with \"failed to marshal template cache: %w\". This is essentially a programming/struct-shape problem: the SimpleTemplate type contains data json.Marshal cannot represent.","triggerScenarios":"Running `oh-my-posh config export data` (or --themes/--sanitize merge path via recordThemeSanitized -> buildDataDocument) when template.Cache.SimpleTemplate holds a value json.Marshal rejects, such as an unexported-only shape change, an invalid value, or a struct containing a channel/func after an internal refactor.","commonSituations":"Developers working on the template package after changing SimpleTemplate fields; running a mismatched binary/version; custom builds where the template cache was populated with unexpected types. End users should essentially never see this.","solutions":["Update/rebuild oh-my-posh from a clean checkout; this indicates a build or version mismatch in the template package","Clear the device/cache state (oh-my-posh cache clear) and rerun the export","If you develop oh-my-posh: inspect the wrapped error to find which SimpleTemplate field is unmarshalable and fix its type or add a MarshalJSON","Report the wrapped error message upstream if it occurs on a released binary"],"exampleFix":"// before (field json.Marshal cannot encode)\ntype SimpleTemplate struct { Fn func() string `json:\"fn\"` }\n\n// after (make the field marshalable or skip it)\ntype SimpleTemplate struct { FnName string `json:\"fn\"` }","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"// Go: verify the template cache marshals to a JSON object before export\nfunc templateCacheIsObject(v any) bool {\n\traw, err := json.Marshal(v)\n\treturn err == nil && len(raw) > 0 && raw[0] == '{'\n}","tryCatchPattern":"doc, err := buildDataDocument(cfg)\nif err != nil {\n\tif strings.Contains(err.Error(), \"failed to marshal template cache\") {\n\t\tfmt.Fprintf(os.Stderr, \"template cache is not encodable: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\treturn err\n}","preventionTips":["Run released oh-my-posh builds; avoid mixing binary and library versions","When changing SimpleTemplate in a fork, add a marshal round-trip test","Keep json.Marshal-unfriendly fields (func, chan) out of template cache structs","Clear the oh-my-posh cache after upgrades before exporting data"],"tags":["json","serialization","template-cache","export"],"backgroundTag":"json-marshal-failed","analyzedSha":"0976794618c5ed95de0985dded50de1b4dc914cb","analyzedAt":"2026-08-31T23:41:19.708Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}