{"record":{"id":"1ed30f57b568e9d4","repo":"ipfs/kubo","slug":"failure-to-decode-config-w-1ed30f","errorCode":null,"errorMessage":"failure to decode config: %w","messagePattern":"failure to decode config: %w","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"config/serialize/serialize.go","lineNumber":31,"sourceCode":"\t\"github.com/facebookgo/atomicfile\"\n)\n\n// ErrNotInitialized is returned when we fail to read the config because the\n// repo doesn't exist.\nvar ErrNotInitialized = errors.New(\"ipfs not initialized, please run 'ipfs init'\")\n\n// ReadConfigFile reads the config from `filename` into `cfg`.\nfunc ReadConfigFile(filename string, cfg any) error {\n\tf, err := os.Open(filename)\n\tif err != nil {\n\t\tif os.IsNotExist(err) {\n\t\t\terr = ErrNotInitialized\n\t\t}\n\t\treturn err\n\t}\n\tdefer f.Close()\n\tif err := json.NewDecoder(f).Decode(cfg); err != nil {\n\t\treturn fmt.Errorf(\"failure to decode config: %w\", err)\n\t}\n\treturn nil\n}\n\n// WriteConfigFile writes the config from `cfg` into `filename`.\nfunc WriteConfigFile(filename string, cfg any) error {\n\terr := os.MkdirAll(filepath.Dir(filename), 0o755)\n\tif err != nil {\n\t\treturn err\n\t}\n\n\tf, err := atomicfile.New(filename, 0o600)\n\tif err != nil {\n\t\treturn err\n\t}\n\tdefer f.Close()\n\n\treturn encode(f, cfg)","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/ipfs/kubo/blob/329838acdfafae224582930457efe80aa217afc0/config/serialize/serialize.go#L13-L49","documentation":"ReadConfigFile wraps JSON decoding failures with \"failure to decode config: %w\". The file exists and opens, but its contents are not valid JSON or do not match the expected config structure. The wrapped error preserves the exact json decoder problem (offset, type mismatch).","triggerScenarios":"Any caller (Load, SetConfig, GetConfigKey, SetConfigKey, openUserResourceOverrides) reading a config file whose content is malformed JSON, truncated after a crash, or whose fields have incompatible types (e.g. a string where a number is expected).","commonSituations":"Hand-editing config.json and leaving a syntax error, interrupted `ipfs config` writes or power loss leaving a partial file, using non-JSON values with `ipfs config --json` incorrectly, or template tools writing placeholders into the file.","solutions":["Validate the file with `jq . $IPFS_PATH/config` (or config.json / libp2p-resource-limit-overrides.json) to find the syntax error and fix it.","Restore from a backup or re-run the failed `ipfs config` command.","As a last resort, run `ipfs init` in a fresh IPFS_PATH and re-apply settings; the wrapped json error message shows the byte offset to inspect."],"exampleFix":"// before: $IPFS_PATH/config\n{\"Identity\": {\"PeerID\": \"12D3...,}   // trailing comma/missing brace\n// after (valid JSON)\n{\"Identity\": {\"PeerID\": \"12D3K...\"}}","handlingStrategy":"try-catch","validationCode":"raw, err := os.ReadFile(cfgPath)\nif err == nil {\n    var v map[string]any\n    if jerr := json.Unmarshal(raw, &v); jerr != nil {\n        return fmt.Errorf(\"config file %s is not valid JSON: %w\", cfgPath, jerr)\n    }\n}","typeGuard":null,"tryCatchPattern":"var cfg config.Config\nif err := serialize.ReadConfigFile(path, &cfg); err != nil {\n    var dec *json.SyntaxError\n    if errors.As(err, &dec) { log.Fatalf(\"fix config JSON at offset %d: %v\", dec.Offset, dec) }\n    return err\n}","preventionTips":["Validate config.json with `jq .` after any hand edit","Avoid killing the daemon/config writers mid-write; use `ipfs config` commands","Keep backups of config files before bulk edits"],"tags":["config","json","go","decode"],"backgroundTag":"config-decode-failed","analyzedSha":"329838acdfafae224582930457efe80aa217afc0","analyzedAt":"2026-09-03T18:30:52.135Z","contentChangedAt":"2026-09-03T18:30:52.135Z","schemaVersion":2},"datasetVersion":"2026-09-11T00:17:11.886Z"}