zed-industries/zed · error · anyhow::Error

failed to run `git init` in directory '{}'

Error message

failed to run `git init` in directory '{}'

What it means

checkout_repo() creates the grammar directory and runs 'git init' inside it (after GIT_CONFIG_GLOBAL is neutralized only for later commands - this call inherits the environment). A non-zero exit from git init bails with this message. git init fails rarely: permissions, read-only filesystem, broken global config, or disk issues.

Source

Thrown at crates/extension/src/extension_builder.rs:392

                && String::from_utf8_lossy(&remotes_output.stdout).trim() == url;
            if !has_remote {
                bail!(
                    "grammar directory '{}' already exists, but is not a git clone of '{}'",
                    directory.display(),
                    url
                );
            }
        } else {
            fs::create_dir_all(directory).with_context(|| {
                format!("failed to create grammar directory {}", directory.display(),)
            })?;
            let init_output = util::command::new_command("git")
                .arg("init")
                .current_dir(directory)
                .output()
                .await?;
            if !init_output.status.success() {
                bail!(
                    "failed to run `git init` in directory '{}'",
                    directory.display()
                );
            }

            let remote_add_output = util::command::new_command("git")
                .arg("--git-dir")
                .arg(&git_dir)
                .args(["remote", "add", "origin", url])
                .output()
                .await
                .context("failed to execute `git remote add`")?;
            if !remote_add_output.status.success() {
                bail!(
                    "failed to add remote {url} for git repository {}",
                    git_dir.display()
                );
            }

View on GitHub (pinned to f4178619ac)

Solutions

  1. Run 'git init' manually in the directory named by the message to see the real git error
  2. Check the directory and cache-root permissions and disk space for the extension cache filesystem
  3. Inspect ~/.gitconfig / GIT_* env for anything breaking git (test with GIT_CONFIG_GLOBAL=/dev/null git init)
  4. If the directory is in a weird half-initialized state, delete it and rebuild so the builder recreates it cleanly
Defensive patterns

Strategy: try-catch

Validate before calling

// Ensure the cache root is writable before building
let probe = cache_dir.join(".write-probe");
std::fs::write(&probe, b"").context("extension cache dir is not writable")?;
std::fs::remove_file(&probe).ok();
anyhow::ensure!(which::which("git").is_ok(), "git is required to build grammar extensions");

Try / catch

match build_result {
    Err(err) if err.to_string().contains("git init") =>
        Err(err.context("cannot initialize git repo in the grammar cache - check permissions/disk")),
    rest => rest,
}

Prevention

When it happens

Trigger: The grammar cache directory was created (create_dir_all succeeded) but 'git init' inside it returned failure - e.g. the cache filesystem is read-only or full, the user lacks write permission, a global git config/template makes init fail, or a stale .git tail exists inside the new directory.

Common situations: Cache dir on a read-only or full volume; restrictive permissions on the extensions cache after a chown/reinstall; broken ~/.gitconfig or GIT_* environment variables injected by wrappers/containers that make every git invocation fail.

Related errors


AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20). Data as JSON: /api/errors/5f9d5b8f73b0a88d. Report an issue: GitHub.