{"record":{"id":"bb6372ed51ec62f2","repo":"charmbracelet/crush","slug":"failed-to-get-current-directory-w","errorCode":null,"errorMessage":"failed to get current directory: %w","messagePattern":"failed to get current directory: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/stats.go","lineNumber":212,"sourceCode":"\t\treturn fmt.Errorf(\"no data available: no sessions found in database\")\n\t}\n\n\tcurrentUser, err := user.Current()\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to get current user: %w\", err)\n\t}\n\tusername := currentUser.Username\n\n\tvar projName string\n\tswitch {\n\tcase crawlDir != \"\":\n\t\tprojName = crawlDir\n\tcase useAll:\n\t\tprojName = \"all projects\"\n\tdefault:\n\t\tproject, err := os.Getwd()\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed to get current directory: %w\", err)\n\t\t}\n\t\tprojName = strings.Replace(project, currentUser.HomeDir, \"~\", 1)\n\t}\n\n\toutputDataDir := dataDir\n\tif outputDataDir == \"\" {\n\t\tcfg, err := config.Init(\"\", \"\", false)\n\t\tif err == nil {\n\t\t\toutputDataDir = cfg.Config().Options.DataDirectory\n\t\t}\n\t}\n\tif outputDataDir == \"\" {\n\t\toutputDataDir = \".crush\"\n\t}\n\n\thtmlPath := filepath.Join(outputDataDir, \"stats/index.html\")\n\tif err := generateHTML(mergedStats, projectStats, projName, username, htmlPath); err != nil {\n\t\treturn fmt.Errorf(\"failed to generate HTML: %w\", err)","sourceCodeStart":194,"sourceCodeEnd":230,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/stats.go#L194-L230","documentation":"This error wraps a failure from os.Getwd() while the `crush stats` command determines the project name for the current working directory. It is thrown because the process cannot resolve its own working directory, which stats needs to label the report. The underlying OS error is preserved via %w so the exact cause (e.g. deleted directory) is visible.","triggerScenarios":"Running `crush stats` (without --project or --all flags) when os.Getwd() fails — typically because the current working directory has been deleted or renamed while the shell still sits in it, or on systems where the inode chain to the mount point is broken.","commonSituations":"Deleting or `git clean`-ing the directory you are cd'd into from another shell; a CI container removing its workdir; stale shell cwd after a tmpfs cleanup; very deep deleted paths on Linux (getcwd ENOENT).","solutions":["cd out of the deleted/stale directory into a valid one and rerun `crush stats`","Use `crush stats --project <name>` or the --all flag to skip the Getwd call entirely","If in a container, recreate the working directory before invoking the command"],"exampleFix":"// before\ncd /tmp/build && rm -rf /tmp/build && crush stats\n// after\ncd /valid/project/dir && crush stats","handlingStrategy":"validation","validationCode":"// Verify the working directory is resolvable before invoking crush stats\nif [ ! -d \"$(pwd)\" ] || [ \"$(pwd)\" != \"$(cd \"$(pwd)\" 2>/dev/null && pwd)\" ]; then\n  echo \"Current directory is invalid; cd to a real project directory first\" >&2\n  exit 1\nfi\ncrush stats","typeGuard":null,"tryCatchPattern":"if err := runStats(...); err != nil {\n    var pwdErr *os.PathError\n    if errors.As(err, &pwdErr) && errors.Is(err, os.ErrNotExist) {\n        // getwd failed: recover by using an explicit project flag\n        return runStatsWithProject(\"--all\")\n    }\n    return err\n}","preventionTips":["Avoid deleting the directory your shell is cd'd into; cd elsewhere first","Prefer passing --project or --all so stats never needs os.Getwd","In scripts, resolve a real absolute path (readlink -f) before running crush","In containers, keep the workdir intact for the life of the process"],"tags":["filesystem","cli","os-getwd"],"backgroundTag":"getcwd-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}