charmbracelet/crush · error

failed to check if directory is empty: %w

Error message

failed to check if directory is empty: %w

What it means

Returned by ProjectNeedsInitialization when dirHasNoVisibleFiles fails while determining whether the working directory contains no non-ignored files. Used to skip initialization for empty directories; the wrap exposes the underlying filesystem error.

Source

Thrown at internal/config/init.go:57

		return false, nil
	}

	if !os.IsNotExist(err) {
		return false, fmt.Errorf("failed to check init flag file: %w", err)
	}

	someContextFileExists, err := contextPathsExist(store.WorkingDir())
	if err != nil {
		return false, fmt.Errorf("failed to check for context files: %w", err)
	}
	if someContextFileExists {
		return false, nil
	}

	// If the working directory has no non-ignored files, skip initialization step
	empty, err := dirHasNoVisibleFiles(store.WorkingDir())
	if err != nil {
		return false, fmt.Errorf("failed to check if directory is empty: %w", err)
	}
	if empty {
		return false, nil
	}

	return true, nil
}

func contextPathsExist(dir string) (bool, error) {
	entries, err := os.ReadDir(dir)
	if err != nil {
		return false, err
	}

	// Create a slice of lowercase filenames for lookup with slices.Contains
	var files []string
	for _, entry := range entries {
		if !entry.IsDir() {

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Grant the process read access to the working directory.
  2. Check filesystem health or remount if the directory is on a network/virtual mount.
  3. Verify no security sandbox is blocking directory listing.
  4. Ensure the working directory passed to the config store is correct.

Example fix

// before
needsInit, err := config.ProjectNeedsInitialization(store) // fails in sandbox
// after: run with readable workdir
if err := os.Chmod(workDir, 0o755); err != nil {
	return err
}
needsInit, err = config.ProjectNeedsInitialization(store)
Defensive patterns

Strategy: validation

Validate before calling

d, err := os.Open(workDir)
if err != nil {
	return fmt.Errorf("cannot list working dir: %w", err)
}
d.Close()

Type guard

func isReaddirError(err error) bool {
	return errors.Is(err, fs.ErrPermission) || errors.Is(err, syscall.EIO)
}

Try / catch

if err != nil && strings.Contains(err.Error(), "failed to check if directory is empty") {
	// grant read access or fix mount, then retry init
}

Prevention

When it happens

Trigger: Calling ProjectNeedsInitialization when the flag file is absent and no context file exists, and then the directory-emptiness scan fails due to unreadable directory entries, permission errors, or I/O failures on the working directory.

Common situations: Restricted read permissions on a fresh clone; directory on a failing disk or network mount; sandboxed environments denying readdir.

Related errors


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