{"record":{"id":"dcdc863759ffaa61","repo":"abiosoft/colima","slug":"unexpected-error-nested-value-encoding-w","errorCode":null,"errorMessage":"unexpected error nested value encoding: %w","messagePattern":"unexpected error nested value encoding: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"util/yamlutil/yaml.go","lineNumber":91,"sourceCode":"\t\t\tcase map[string]any:\n\t\t\tcase map[string]string:\n\n\t\t\tdefault:\n\t\t\t\tcontinue\n\t\t\t}\n\t\t}\n\n\t\t// nil slices are converted to untyped nil to encode as `null` instead of `[]`.\n\t\t// this preserves nil vs empty slice distinction when the yaml is loaded back.\n\t\tif v := reflect.ValueOf(val); v.Kind() == reflect.Slice && v.IsNil() {\n\t\t\tval = nil\n\t\t}\n\n\t\t// lazy way, delegate node construction to the yaml library via a roundtrip.\n\t\t// no performance concern as only one file is being read\n\t\tb, err := yaml.Marshal(val)\n\t\tif err != nil {\n\t\t\treturn nil, fmt.Errorf(\"unexpected error nested value encoding: %w\", err)\n\t\t}\n\t\tvar newNode yaml.Node\n\t\tif err := yaml.Unmarshal(b, &newNode); err != nil {\n\t\t\treturn nil, fmt.Errorf(\"unexpected error during yaml node traversal: %w\", err)\n\t\t}\n\n\t\tif l := len(newNode.Content); l != 1 {\n\t\t\treturn nil, fmt.Errorf(\"unexpected error during yaml node traversal: doc has multiple children of len %d\", l)\n\t\t}\n\t\t*node = *newNode.Content[0]\n\t}\n\n\tb, err := encode(root)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error encoding yaml file: %w\", err)\n\t}\n\n\treturn b, nil","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/abiosoft/colima/blob/c3a5f9184d83a197184f897a9f07eb3c01b3bc88/util/yamlutil/yaml.go#L73-L109","documentation":"encodeYAML (util/yamlutil/yaml.go:91) applies each config value to the node tree via a Marshal/Unmarshal roundtrip; this error is the Marshal leg failing for an individual value. It means one of the config struct's leaf values is of a type yaml.v3 cannot encode — chan, func, or a type whose MarshalYAML errors. Stock colima config uses plain scalars/slices/maps, so this arises from fork-added fields of unsupported types.","triggerScenarios":"Adding a config field of type chan, func, or a struct with a failing MarshalYAML, then running any code path that saves the config (colima start/stop with settings changes).","commonSituations":"Forks extending config.Config with runtime handles (loggers, cancellations) instead of serializable settings; embedding interfaces backed by non-serializable implementations.","solutions":["Find the offending field from the wrapped error (it names the type) and change it to a serializable type (string, int, bool, slices, maps)","Tag non-serialized fields with `yaml:\"-\"` so they are skipped during config save","Implement yaml.Marshaler on the custom type to control its encoding","Keep config.Config limited to plain data; keep handles in a separate runtime struct"],"exampleFix":"// before\ntype Config struct {\n    Runtime chan string `yaml:\"runtime\"` // marshal fails\n}\n\n// after\ntype Config struct {\n    Runtime string `yaml:\"runtime\"`\n    dispatch chan string `yaml:\"-\"`\n}","handlingStrategy":"validation","validationCode":"// assert a config value is YAML-encodable before saving\nif _, err := yaml.Marshal(val); err != nil {\n    return fmt.Errorf(\"config field of unsupported type: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := util.Save(cfg, file); err != nil {\n    if strings.Contains(err.Error(), \"nested value encoding\") {\n        // a config value has an unsupported type; the wrapped cause names it\n        return fmt.Errorf(\"config contains unserializable value — see cause: %w\", err)\n    }\n    return err\n}","preventionTips":["Restrict config.Config fields to scalars, strings, slices, and maps","Add `yaml:\"-\"` to every non-data field","Round-trip test Save() with a fully populated config in CI","Review new config fields for serializability in code review"],"tags":["yaml","reflection","config","types"],"backgroundTag":null,"analyzedSha":"c3a5f9184d83a197184f897a9f07eb3c01b3bc88","analyzedAt":"2026-08-15T18:58:08.334Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}