{"record":{"id":"194b0fc02a7a458d","repo":"charmbracelet/crush","slug":"failed-to-get-current-working-directory-v-194b0f","errorCode":null,"errorMessage":"failed to get current working directory: %v","messagePattern":"failed to get current working directory: (.+?)","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/cmd/root.go","lineNumber":980,"sourceCode":"\tcase 1:\n\t\treturn matches[0], nil\n\tdefault:\n\t\treturn session.Session{}, fmt.Errorf(\"session ID %q is ambiguous (%d matches)\", id, len(matches))\n\t}\n}\n\nfunc ResolveCwd(cmd *cobra.Command) (string, error) {\n\tcwd, _ := cmd.Flags().GetString(\"cwd\")\n\tif cwd != \"\" {\n\t\terr := os.Chdir(cwd)\n\t\tif err != nil {\n\t\t\treturn \"\", fmt.Errorf(\"failed to change directory: %v\", err)\n\t\t}\n\t\treturn cwd, nil\n\t}\n\tcwd, err := os.Getwd()\n\tif err != nil {\n\t\treturn \"\", fmt.Errorf(\"failed to get current working directory: %v\", err)\n\t}\n\treturn cwd, nil\n}\n\nfunc createDotCrushDir(dir string) error {\n\tif err := os.MkdirAll(dir, 0o700); err != nil {\n\t\treturn fmt.Errorf(\"failed to create data directory: %q %w\", dir, err)\n\t}\n\n\tgitIgnorePath := filepath.Join(dir, \".gitignore\")\n\tcontent, err := os.ReadFile(gitIgnorePath)\n\n\t// create or update if old version\n\tif os.IsNotExist(err) || string(content) == oldGitIgnore {\n\t\tif err := os.WriteFile(gitIgnorePath, []byte(defaultGitIgnore), 0o644); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to create .gitignore file: %q %w\", gitIgnorePath, err)\n\t\t}\n\t}","sourceCodeStart":962,"sourceCodeEnd":998,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/cmd/root.go#L962-L998","documentation":"Wraps os.Getwd() failure when no --cwd flag is given. Getwd returns the current working directory of the process; it fails when the directory the shell was launched from has been deleted or renamed, or when permission prevents resolution.","triggerScenarios":"Running crush from a terminal whose working directory was deleted (e.g. after a git worktree removal or a project rename); restricted environments where the getwd syscall fails.","commonSituations":"IDE terminals keeping stale CWDs after a repo move; deleted temp/build directories; disconnected NFS/automounted paths.","solutions":["cd to an existing directory (e.g. project root) and rerun the command","Open a fresh shell/terminal session to reset a stale CWD","Pass an explicit --cwd pointing at a valid directory","Restore or recreate the deleted directory if the shell must stay there"],"exampleFix":"// before: running from a deleted directory\ncrush run \"task\"\n// after: pass an explicit valid cwd\ncrush --cwd /path/to/project run \"task\"","handlingStrategy":"validation","validationCode":"if _, err := os.Getwd(); err != nil {\n\t// stale CWD: bail out early or fall back to an explicit --cwd\n\treturn fmt.Errorf(\"current directory no longer exists; pass --cwd\")\n}","typeGuard":null,"tryCatchPattern":"cwd, err := ResolveCwd(cmd)\nif err != nil {\n\tif strings.Contains(err.Error(), \"current working directory\") {\n\t\tslog.Error(\"Shell CWD is stale; cd to a valid directory or use --cwd\")\n\t}\n\treturn err\n}","preventionTips":["Reopen shells/IDE terminals after deleting or renaming project directories","Pass --cwd explicitly in automation instead of relying on inherited CWD","Avoid running commands from removed tmp/worktree paths","Sanity-check `pwd` in shell wrappers before invoking"],"tags":["filesystem","getwd","cwd"],"backgroundTag":"deleted-working-directory","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}