charmbracelet/crush · error

failed to change directory: %v

Error message

failed to change directory: %v

What it means

Wraps os.Chdir failure when the --cwd flag is provided. The OS refused to switch into the given directory - typically because it does not exist, the path is a file, or the process lacks search (execute) permission on a path component.

Source

Thrown at internal/cmd/root.go:974

		}
	}

	switch len(matches) {
	case 0:
		return session.Session{}, fmt.Errorf("session not found: %s", id)
	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)

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Verify the path exists and is a directory: `test -d /some/path`
  2. Correct the --cwd value or use an absolute path
  3. Fix permissions on the directory and its parents (chmod/chown)
  4. Remove the --cwd flag and cd manually before running crush

Example fix

// before
crush --cwd ~/projects/app run "build"
// after: validate first
test -d ~/projects/app && crush --cwd ~/projects/app run "build"
Defensive patterns

Strategy: validation

Validate before calling

func dirExists(p string) bool {
	info, err := os.Stat(p)
	return err == nil && info.IsDir()
}
// before invoking: crush --cwd <dir>
if !dirExists(target) {
	return fmt.Errorf("--cwd target is not a directory: %s", target)
}

Try / catch

cwd, err := ResolveCwd(cmd)
if err != nil {
	if strings.Contains(err.Error(), "change directory") {
		slog.Error("--cwd path invalid; verify it exists and is a directory")
	}
	return err
}

Prevention

When it happens

Trigger: `crush --cwd /some/path` where the path was deleted/renamed, is a regular file, or the user lacks permission; relative --cwd values resolved against an unexpected base directory.

Common situations: Hard-coded paths in scripts/aliases pointing at moved repositories; containers with restricted mounts; typos in the flag value; removed symlink targets.

Related errors


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