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

  1. cd to an existing directory (e.g. project root) and rerun the command
  2. Open a fresh shell/terminal session to reset a stale CWD
  3. Pass an explicit --cwd pointing at a valid directory
  4. 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

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


AI-assisted analysis of charmbracelet/crush@7944b8e522 (2026-08-29). Data as JSON: /api/errors/194b0fc02a7a458d. Report an issue: GitHub.