{"record":{"id":"a553a09beb5c3bfa","repo":"getsops/sops","slug":"error-encoding-section-section-values-should-alwa","errorCode":null,"errorMessage":"Error encoding section: Section values should always be TreeBranches","messagePattern":"Error encoding section: Section values should always be TreeBranches","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"stores/ini/store.go","lineNumber":42,"sourceCode":"func (store *Store) Name() string {\n\treturn \"ini\"\n}\n\nfunc (store Store) encodeTree(branches sops.TreeBranches) ([]byte, error) {\n\tiniFile := ini.Empty(ini.LoadOptions{AllowNonUniqueSections: true})\n\tiniFile.DeleteSection(ini.DefaultSection)\n\tfor _, branch := range branches {\n\t\tfor _, item := range branch {\n\t\t\tif _, ok := item.Key.(sops.Comment); ok {\n\t\t\t\tcontinue\n\t\t\t}\n\t\t\tsection, err := iniFile.NewSection(item.Key.(string))\n\t\t\tif err != nil {\n\t\t\t\treturn nil, fmt.Errorf(\"Error encoding section %s: %s\", item.Key, err)\n\t\t\t}\n\t\t\titemTree, ok := item.Value.(sops.TreeBranch)\n\t\t\tif !ok {\n\t\t\t\treturn nil, fmt.Errorf(\"Error encoding section: Section values should always be TreeBranches\")\n\t\t\t}\n\n\t\t\tfirst := 0\n\t\t\tif len(itemTree) > 0 {\n\t\t\t\tif sectionComment, ok := itemTree[0].Key.(sops.Comment); ok {\n\t\t\t\t\tsection.Comment = sectionComment.Value\n\t\t\t\t\tfirst = 1\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tvar lastItem *ini.Key\n\t\t\tfor i := first; i < len(itemTree); i++ {\n\t\t\t\tkeyVal := itemTree[i]\n\t\t\t\tif comment, ok := keyVal.Key.(sops.Comment); ok {\n\t\t\t\t\tif lastItem != nil {\n\t\t\t\t\t\tlastItem.Comment = comment.Value\n\t\t\t\t\t}\n\t\t\t\t} else {","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/getsops/sops/blob/13442bb98183887d7a9ac09ec8ab0564673a59d8/stores/ini/store.go#L24-L60","documentation":"In the INI store, every section's value must be a sops.TreeBranch (the list of key/value items inside that section). encodeTree type-asserts item.Value.(sops.TreeBranch); if the value is a scalar, array, or any other type, this error fires because INI has no way to represent a section whose body is not a key list.","triggerScenarios":"Calling EmitEncryptedFile/EmitPlainFile with a top-level TreeItem whose value is not sops.TreeBranch — e.g. {Key: \"settings\", Value: \"plain string\"} or {Key: \"arr\", Value: []interface{}{...}}.","commonSituations":"Converting flat key=value data or JSON/YAML trees with scalar top-level values into INI; programmatic tree building that forgot to nest section keys under a TreeBranch; reusing branches decoded from a different store format without restructuring.","solutions":["Wrap section values in sops.TreeBranch: move the scalar under a key, e.g. {Key: \"settings\", Value: sops.TreeBranch{{Key: \"value\", Value: \"plain string\"}}}","Convert arrays to a TreeBranch of indexed keys (\"0\", \"1\", ...) or pick a list-capable store (json/yaml)","Validate the tree shape (every top-level value is TreeBranch) before calling the INI store's emit functions"],"exampleFix":"// before\nsops.TreeBranch{{Key: \"settings\", Value: \"plain string\"}}\n\n// after\nsops.TreeBranch{{Key: \"settings\", Value: sops.TreeBranch{{Key: \"value\", Value: \"plain string\"}}}}","handlingStrategy":"type-guard","validationCode":"for _, item := range branch {\n\tif _, ok := item.Value.(sops.TreeBranch); !ok {\n\t\treturn fmt.Errorf(\"section %v value must be a TreeBranch, got %T\", item.Key, item.Value)\n\t}\n}","typeGuard":"func isTreeBranch(v interface{}) bool {\n\t_, ok := v.(sops.TreeBranch)\n\treturn ok\n}","tryCatchPattern":"out, err := store.EmitPlainFile(branches)\nif err != nil {\n\tif strings.Contains(err.Error(), \"should always be TreeBranches\") {\n\t\treturn fmt.Errorf(\"restructure tree: wrap section values in sops.TreeBranch: %w\", err)\n\t}\n\treturn err\n}","preventionTips":["Always nest section contents in sops.TreeBranch","Validate tree shape before calling INI emit","Don't reuse branches from other format stores without restructuring"],"tags":["go","sops","ini","type-mismatch"],"backgroundTag":"invalid-tree-structure","analyzedSha":"13442bb98183887d7a9ac09ec8ab0564673a59d8","analyzedAt":"2026-09-01T03:53:00.447Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}