charmbracelet/crush · error

failed to create .gitignore file: %q %w

Error message

failed to create .gitignore file: %q %w

What it means

After creating the data directory, createDotCrushDir writes a .gitignore containing "*\n" (mode 0644) so workspace data is never committed. This error wraps os.WriteFile failure for that file, meaning the directory exists but the ignore file could not be created.

Source

Thrown at internal/backend/util.go:17

package backend

import (
	"fmt"
	"os"
	"path/filepath"
)

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")
	if _, err := os.Stat(gitIgnorePath); os.IsNotExist(err) {
		if err := os.WriteFile(gitIgnorePath, []byte("*\n"), 0o644); err != nil {
			return fmt.Errorf("failed to create .gitignore file: %q %w", gitIgnorePath, err)
		}
	}

	return nil
}

View on GitHub (pinned to 7944b8e522)

Solutions

  1. Check whether the quoted .gitignore path collides with an existing directory and remove/rename it.
  2. Verify write permission on the data directory itself (chmod u+w).
  3. Check disk quota/remaining space.
  4. Re-run CreateWorkspace; the .gitignore creation is idempotent (skipped when it already exists).
Defensive patterns

Strategy: validation

Validate before calling

p := filepath.Join(dir, ".gitignore")
if fi, err := os.Stat(p); err == nil && fi.IsDir() {
    return fmt.Errorf("%s is a directory, cannot write .gitignore", p)
}

Type guard

func IsGitignoreWriteErr(err error) (string, bool) {
    var pe *fs.PathError
    if errors.As(err, &pe) {
        return pe.Path, true
    }
    return "", false
}

Try / catch

if err := createDotCrushDir(dir); err != nil {
    var pe *fs.PathError
    if errors.As(err, &pe) && errors.Is(pe.Err, fs.ErrPermission) {
        os.Chmod(dir, 0o700) // restore write access, then retry once
        return createDotCrushDir(dir)
    }
    return err
}

Prevention

When it happens

Trigger: CreateWorkspace -> createDotCrushDir where the directory was created (or already existed) but os.WriteFile(gitIgnorePath, ...) fails — permission denied on the directory, .gitignore exists as a directory, disk full, or the directory was made read-only between creation and write.

Common situations: Antivirus or security software locking new files; a directory literally named .gitignore inside the data dir; quota exhaustion on network filesystems (NFS homes).

Related errors


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