charmbracelet/crush · error
failed to get current working directory: %v
Error message
failed to get current working directory: %v
What it means
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.
Source
Thrown at internal/cmd/root.go:980
case 1:
return matches[0], nil
default:
return session.Session{}, fmt.Errorf("session ID %q is ambiguous (%d matches)", id, len(matches))
}
}
func ResolveCwd(cmd *cobra.Command) (string, error) {
cwd, _ := cmd.Flags().GetString("cwd")
if cwd != "" {
err := os.Chdir(cwd)
if err != nil {
return "", fmt.Errorf("failed to change directory: %v", err)
}
return cwd, nil
}
cwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("failed to get current working directory: %v", err)
}
return cwd, nil
}
func createDotCrushDir(dir string) error {
if err := os.MkdirAll(dir, 0o700); err != nil {
return fmt.Errorf("failed to create data directory: %q %w", dir, err)
}
gitIgnorePath := filepath.Join(dir, ".gitignore")
content, err := os.ReadFile(gitIgnorePath)
// create or update if old version
if os.IsNotExist(err) || string(content) == oldGitIgnore {
if err := os.WriteFile(gitIgnorePath, []byte(defaultGitIgnore), 0o644); err != nil {
return fmt.Errorf("failed to create .gitignore file: %q %w", gitIgnorePath, err)
}
}View on GitHub (pinned to 7944b8e522)
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
Example fix
// before: running from a deleted directory crush run "task" // after: pass an explicit valid cwd crush --cwd /path/to/project run "task"
Defensive patterns
Strategy: validation
Validate before calling
if _, err := os.Getwd(); err != nil {
// stale CWD: bail out early or fall back to an explicit --cwd
return fmt.Errorf("current directory no longer exists; pass --cwd")
} Try / catch
cwd, err := ResolveCwd(cmd)
if err != nil {
if strings.Contains(err.Error(), "current working directory") {
slog.Error("Shell CWD is stale; cd to a valid directory or use --cwd")
}
return err
} Prevention
- 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
When it happens
Trigger: 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.
Common situations: IDE terminals keeping stale CWDs after a repo move; deleted temp/build directories; disconnected NFS/automounted paths.
Related errors
- failed to change directory: %v
- cannot convert CWD to absolute path: %w
- cannot convert stop dir to absolute path: %w
- failed to create parent directories: %w
- failed to create output file: %w
AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29).
Data as JSON: /api/errors/194b0fc02a7a458d.
Report an issue: GitHub.