{"record":{"id":"6760fc5a4642c6d6","repo":"charmbracelet/crush","slug":"failed-to-initialize-config-w-6760fc","errorCode":null,"errorMessage":"failed to initialize config: %w","messagePattern":"failed to initialize config: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":161,"sourceCode":"\n\tvar projectStats []ProjectStats\n\tvar err error\n\n\tswitch {\n\tcase crawlDir != \"\":\n\t\tprojectStats, err = crawlForStats(ctx, crawlDir)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to crawl for stats: %w\", err)\n\t\t}\n\tcase useAll:\n\t\tprojectStats, err = gatherStatsFromProjects(ctx)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to gather stats from projects: %w\", err)\n\t\t}\n\tdefault:\n\t\tcfg, err := config.Init(\"\", dataDir, false)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to initialize config: %w\", err)\n\t\t}\n\t\tif dataDir == \"\" {\n\t\t\tdataDir = cfg.Config().Options.DataDirectory\n\t\t}\n\t\tif shouldEnableMetrics(cfg.Config()) {\n\t\t\tevent.Init()\n\t\t}\n\n\t\tevent.StatsViewed()\n\n\t\tconn, err := db.Connect(ctx, dataDir)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to connect to database: %w\", err)\n\t\t}\n\t\tdefer conn.Close()\n\n\t\tstats, err := gatherStats(ctx, conn)\n\t\tif err != nil {","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/stats.go#L143-L179","documentation":"In the default mode (no --crawl-dir / --all), `crush stats` first loads configuration via `config.Init(\"\", dataDir, false)`. This error wraps any config-initialization failure — invalid or unreadable crush config files, malformed JSON/crushrc, or failures resolving the data directory — as `failed to initialize config: <cause>`. The root cause is always in the wrapped `%w` chain.","triggerScenarios":"Running plain `crush stats` where `config.Init(\"\", dataDir, false)` at internal/cmd/stats.go:159 returns an error: a malformed crush.json / .crushrc in the working or parent directories, unreadable global config, or an invalid --data-dir value.","commonSituations":"Hand-edited crush.json with invalid JSON; a crushrc script with a failing builtin or syntax error; config file owned by root/unreadable; invalid --data-dir flag (e.g. path exists as a file); schema changes after upgrading crush with an old config field.","solutions":["Read the wrapped cause after the colon — it names the offending config file and usually the parse problem (line/column).","Validate the JSON config: `jq . crush.json` (or fix syntax errors in your .crushrc script).","Check file permissions on crush.json / .crushrc in the cwd, parent dirs, and the global config location.","If you passed --data-dir, verify it is a valid writable directory path.","Temporarily rename suspect config files (crush.json, .crushrc) and re-run to confirm which one breaks init."],"exampleFix":"// before\ncrush stats --data-dir /etc/passwd\n# failed to initialize config: ...\n// after\nmkdir -p ~/.local/share/crush-data\ncrush stats --data-dir ~/.local/share/crush-data","handlingStrategy":"validation","validationCode":"// Validate config files before running crush stats\nfor _, f := range []string{\"crush.json\", \".crushrc\"} {\n    if _, err := os.Stat(f); err == nil {\n        if strings.HasSuffix(f, \".json\") {\n            data, err := os.ReadFile(f)\n            if err != nil {\n                log.Fatalf(\"config %s unreadable: %v\", f, err)\n            }\n            var v any\n            if err := json.Unmarshal(data, &v); err != nil {\n                log.Fatalf(\"config %s is invalid JSON: %v\", f, err)\n            }\n        }\n    }\n}","typeGuard":"func isReadableFile(path string) bool {\n    f, err := os.Open(path)\n    if err != nil {\n        return false\n    }\n    f.Close()\n    return true\n}","tryCatchPattern":"out, err := exec.Command(\"crush\", \"stats\").CombinedOutput()\nif err != nil {\n    if strings.Contains(string(out), \"failed to initialize config\") {\n        log.Fatalf(\"fix your crush config first: %s\", out)\n    }\n    log.Fatalf(\"stats failed: %v: %s\", err, out)\n}","preventionTips":["Validate crush.json with `jq .` (or lint your .crushrc with `bash -n`) after every edit.","Keep config files owned and readable by the user running crush.","Check cwd and parent directories for stale configs when errors mention a file you did not expect.","After upgrading crush, review config schema changes and remove removed fields.","Pass an explicit, valid --data-dir in scripts to avoid environment-dependent config resolution."],"tags":["config","cli","validation","startup"],"backgroundTag":"config-load-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}