{"record":{"id":"52111eb42a899807","repo":"JanDeDobbeleer/oh-my-posh","slug":"failed-to-marshal-segment-s-w","errorCode":null,"errorMessage":"failed to marshal segment %s: %w","messagePattern":"failed to marshal segment (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/cli/config_export_data.go","lineNumber":161,"sourceCode":"\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()\n\t\t\tif writer == nil {\n\t\t\t\tcontinue\n\t\t\t}\n\n\t\t\traw, methods, err := recordSegmentData(writer)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to marshal segment %s: %w\", segment.DataKey(), err)\n\t\t\t}\n\n\t\t\trecorded := config.RecordedSegment{Data: raw, Methods: methods, Enabled: segment.Enabled}\n\n\t\t\trecordedRaw, err := json.Marshal(recorded)\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"failed to marshal segment %s: %w\", segment.DataKey(), err)\n\t\t\t}\n\n\t\t\tkey := segment.DataKey()\n\t\t\tif _, exists := segments[key]; exists {\n\t\t\t\tfmt.Fprintf(os.Stderr, \"warning: multiple segments share the data key %q; the last one wins - add an alias to disambiguate\\n\", key)\n\t\t\t}\n\n\t\t\tsegments[key] = recordedRaw\n\t\t}\n\t}\n","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/JanDeDobbeleer/oh-my-posh/blob/0976794618c5ed95de0985dded50de1b4dc914cb/src/cli/config_export_data.go#L143-L179","documentation":"While recording the export data document, each configured segment's writer is serialized via recordSegmentData. If marshaling a segment's data fails, buildDataDocument aborts naming the segment: \"failed to marshal segment <key>: <reason>\". It means one segment type produced data json.Marshal cannot encode (or recordSegmentData hit an internal error).","triggerScenarios":"Running `oh-my-posh config export data` with a config containing a segment whose writer data is unmarshalable — typically a map keyed by a non-string type, an invalid value, or a func/channel field in a segment's response struct.","commonSituations":"Buggy or experimental segment data after an oh-my-posh update; a custom config exercising a rarely recorded segment (e.g. odd API response shapes cached into a segment's data); developing a new segment with an unmarshalable field.","solutions":["Identify the segment named in the message and check the wrapped cause for the exact field","Update oh-my-posh; if recent, this may be a regression — report it with the wrapped error","Temporarily disable the offending segment in the config to unblock the export","For segment developers: give the offending field a json:\"-\" tag or a MarshalJSON implementation"],"exampleFix":"// before\ntype gitData struct { Hooks map[int]string `json:\"hooks\"` }\n\n// after\ntype gitData struct { Hooks map[int]string `json:\"-\"` } // or map[string]string","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"// Go: check segment data encodes cleanly before recording\nfunc segmentDataEncodable(writer segments.SegmentWriter) bool {\n\traw, _, err := recordSegmentData(writer)\n\treturn err == nil && json.Valid(raw)\n}","tryCatchPattern":"if err := runDataExport(cfg); err != nil {\n\tvar segErr interface{ Unwrap() error }\n\tif strings.Contains(err.Error(), \"failed to marshal segment\") {\n\t\tfmt.Fprintf(os.Stderr, \"offending segment identified in: %v\\n\", err)\n\t\tos.Exit(1)\n\t}\n\treturn err\n}","preventionTips":["Bisect by disabling segments in the config until the named segment is isolated","For new segments, unit test that their data marshals to valid JSON","Tag internal fields json:\"-\" so they never reach recorded output","Keep oh-my-posh current; marshal failures in stock segments are usually fixed upstream quickly"],"tags":["json","serialization","segments","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"}