{"record":{"id":"fbb1ba3ad5a08908","repo":"charmbracelet/crush","slug":"failed-to-gather-stats-from-projects-w","errorCode":null,"errorMessage":"failed to gather stats from projects: %w","messagePattern":"failed to gather stats from projects: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":156,"sourceCode":"func runStats(cmd *cobra.Command, _ []string) error {\n\tctx := cmd.Context()\n\tdataDir, _ := cmd.Flags().GetString(\"data-dir\")\n\tcrawlDir, _ := cmd.Flags().GetString(\"crawl-dir\")\n\tuseAll, _ := cmd.Flags().GetBool(\"all\")\n\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)","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/stats.go#L138-L174","documentation":"`crush stats --all` aggregates statistics across all known projects via `gatherStatsFromProjects`. This error wraps any failure of that aggregation — e.g. failing to discover project directories, or errors while reading per-project data — as `failed to gather stats from projects: <cause>`. The underlying cause is in the wrapped error chain.","triggerScenarios":"Running `crush stats --all` where `gatherStatsFromProjects(ctx)` at internal/cmd/stats.go:154 returns an error, typically because it cannot enumerate or read the project directories / their data under the user's data or home directories.","commonSituations":"Home directory or data directory unreadable (permissions, read-only mount); projects registry missing or malformed; disk I/O errors while reading many project DBs; running under a service account with a different HOME.","solutions":["Read the wrapped cause after the colon to identify the failing project or path.","Verify your data directory and project directories are readable: check $HOME / --data-dir permissions.","Retry with a single project via the default mode (no flags) to isolate whether only one project's data is bad.","Run `crush stats --crawl-dir <specific-dir>` as a fallback to compute stats for a directory you control."],"exampleFix":"// before\ncrush stats --all\n# failed to gather stats from projects: permission denied\n// after\ncrush stats --crawl-dir ~/work/project-a  # scope to an accessible project","handlingStrategy":"fallback","validationCode":"// Verify HOME/data dir is usable before --all\nhome, err := os.UserHomeDir()\nif err != nil {\n    log.Fatalf(\"cannot resolve home dir: %v\", err)\n}\nif f, err := os.Open(home); err != nil {\n    log.Fatalf(\"home dir not readable: %v\", err)\n} else {\n    f.Close()\n}","typeGuard":"func isAccessibleDir(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\", \"--all\").CombinedOutput()\nif err != nil {\n    if strings.Contains(string(out), \"failed to gather stats from projects\") {\n        log.Println(\"--all aggregation failed; falling back to crawl-dir\")\n        out, err = exec.Command(\"crush\", \"stats\", \"--crawl-dir\", \".\").CombinedOutput()\n    }\n    if err != nil {\n        log.Fatalf(\"stats failed: %v: %s\", err, out)\n    }\n}","preventionTips":["Run as a user with read access to the data and project directories.","If HOME differs (service accounts), pass --data-dir explicitly.","Isolate bad project data by running single-project stats before --all.","Check filesystem mount health (read-only remounts) before large aggregations."],"tags":["stats","filesystem","projects","cli"],"backgroundTag":"path-not-found","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}