{"record":{"id":"225ccc82c0f8069f","repo":"AlexxIT/go2rtc","slug":"yaml-can-t-patch","errorCode":null,"errorMessage":"yaml: can't patch","messagePattern":"yaml: can't patch","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/yaml/yaml.go","lineNumber":54,"sourceCode":"\n\treturn out, nil\n}\n\nfunc patch(in []byte, path []string, value any) ([]byte, error) {\n\tvar root yaml.Node\n\tif err := yaml.Unmarshal(in, &root); err != nil {\n\t\t// invalid yaml\n\t\treturn nil, err\n\t}\n\n\t// empty in\n\tif len(root.Content) != 1 {\n\t\treturn addToEnd(in, path, value)\n\t}\n\n\t// yaml is not dict\n\tif root.Content[0].Kind != yaml.MappingNode {\n\t\treturn nil, errors.New(\"yaml: can't patch\")\n\t}\n\n\t// dict items list\n\tnodes := root.Content[0].Content\n\n\tn := len(path) - 1\n\n\t// parent node key/value\n\tpKey, pVal := findNode(nodes, path[:n])\n\tif pKey == nil {\n\t\t// no parent node\n\t\treturn addToEnd(in, path, value)\n\t}\n\n\tvar paste []byte\n\n\tif value != nil {\n\t\t// nil value means delete key","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/AlexxIT/go2rtc/blob/c245815e75e2a5fd60b4290f12bfc04e55a984d3/pkg/yaml/yaml.go#L36-L72","documentation":"pkg/yaml patch updates a value at a path inside a parsed YAML node tree. After confirming the document has exactly one root content node, it requires that node to be a mapping (dict); if the root is a scalar or sequence, a path-based patch cannot be applied and the function returns this error instead of corrupting the document. It is called by the exported Patch.","triggerScenarios":"Calling yaml Patch with a path on a YAML document whose root is a plain scalar (e.g. the file contains just '42' or 'some string') or a top-level list rather than a mapping.","commonSituations":"Pointing the patcher at a config file that unexpectedly contains an empty document or a top-level array; a templating step flattening the YAML to a single scalar; wrong file selected for patching.","solutions":["Ensure the target YAML document's root is a mapping (key: value pairs at top level)","If the document is empty, write an initial mapping structure before patching","Check you are patching the intended file — the error implies a scalar/sequence root"],"exampleFix":"// before (target doc: just '- item1\\n- item2')\nyaml.Patch(doc, []string{\"key\"}, \"value\") // error\n// after (target doc root mapping)\n// key: old\nyaml.Patch(doc, []string{\"key\"}, \"value\") // ok","handlingStrategy":"type-guard","validationCode":"root, err := yaml.Parse(docBytes)\nif err != nil || root == nil || len(root.Content) != 1 || root.Content[0].Kind != yaml.MappingNode {\n    // initialize document as a mapping before patching\n}","typeGuard":"func isMappingRoot(root *yaml.Node) bool {\n    return root != nil && len(root.Content) == 1 && root.Content[0].Kind == yaml.MappingNode\n}","tryCatchPattern":"if _, err := yaml.Patch(doc, path, value); err != nil {\n    if strings.Contains(err.Error(), \"can't patch\") {\n        // rebuild the document as a mapping and re-apply\n    }\n    return err\n}","preventionTips":["Guarantee patched config files start with a top-level mapping","Check document emptiness after template processing before patching"],"tags":["yaml","patch","document-structure"],"backgroundTag":"unsupported-operation","analyzedSha":"c245815e75e2a5fd60b4290f12bfc04e55a984d3","analyzedAt":"2026-09-07T11:47:02.965Z","contentChangedAt":"2026-09-07T11:47:02.965Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}