charmbracelet/crush · error

failed to get current directory: %w

Error message

failed to get current directory: %w

What it means

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.

Source

Thrown at internal/cmd/stats.go:212

		return fmt.Errorf("no data available: no sessions found in database")
	}

	currentUser, err := user.Current()
	if err != nil {
		return fmt.Errorf("failed to get current user: %w", err)
	}
	username := currentUser.Username

	var projName string
	switch {
	case crawlDir != "":
		projName = crawlDir
	case useAll:
		projName = "all projects"
	default:
		project, err := os.Getwd()
		if err != nil {
			return fmt.Errorf("failed to get current directory: %w", err)
		}
		projName = strings.Replace(project, currentUser.HomeDir, "~", 1)
	}

	outputDataDir := dataDir
	if outputDataDir == "" {
		cfg, err := config.Init("", "", false)
		if err == nil {
			outputDataDir = cfg.Config().Options.DataDirectory
		}
	}
	if outputDataDir == "" {
		outputDataDir = ".crush"
	}

	htmlPath := filepath.Join(outputDataDir, "stats/index.html")
	if err := generateHTML(mergedStats, projectStats, projName, username, htmlPath); err != nil {
		return fmt.Errorf("failed to generate HTML: %w", err)

View on GitHub (pinned to 7944b8e522)

Solutions

  1. cd out of the deleted/stale directory into a valid one and rerun `crush stats`
  2. Use `crush stats --project <name>` or the --all flag to skip the Getwd call entirely
  3. If in a container, recreate the working directory before invoking the command

Example fix

// before
cd /tmp/build && rm -rf /tmp/build && crush stats
// after
cd /valid/project/dir && crush stats
Defensive patterns

Strategy: validation

Validate before calling

// Verify the working directory is resolvable before invoking crush stats
if [ ! -d "$(pwd)" ] || [ "$(pwd)" != "$(cd "$(pwd)" 2>/dev/null && pwd)" ]; then
  echo "Current directory is invalid; cd to a real project directory first" >&2
  exit 1
fi
crush stats

Try / catch

if err := runStats(...); err != nil {
    var pwdErr *os.PathError
    if errors.As(err, &pwdErr) && errors.Is(err, os.ErrNotExist) {
        // getwd failed: recover by using an explicit project flag
        return runStatsWithProject("--all")
    }
    return err
}

Prevention

When it happens

Trigger: 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.

Common situations: 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).

Related errors


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