zed-industries/zed · error

failed to run `git init` in directory {directory:?}

Error message

failed to run `git init` in directory {directory:?}

What it means

This error is raised in `checkout_repo` (crates/extension/src/extension_builder.rs:357) when Zed's extension builder runs `git init` to create a fresh git repository in the grammar's source directory and the command exits with a non-zero status. The builder initializes a repository so it can add the grammar's URL as an `origin` remote and shallow-fetch the pinned revision. The error means git itself failed before any network operation, so the checkout cannot proceed.

Source

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

                .args(["remote", "get-url", "origin"])
                .env("GIT_CONFIG_GLOBAL", "/dev/null")
                .output()
                .await?;
            let has_remote = remotes_output.status.success()
                && String::from_utf8_lossy(&remotes_output.stdout).trim() == url;
            anyhow::ensure!(
                has_remote,
                "grammar directory {directory:?} already exists, but is not a git clone of '{url}'"
            );
        } else {
            fs::create_dir_all(directory)
                .with_context(|| format!("creating grammar directory {directory:?}"))?;
            let init_output = util::command::new_command("git")
                .arg("init")
                .current_dir(directory)
                .output()
                .await?;
            anyhow::ensure!(
                init_output.status.success(),
                "failed to run `git init` in directory {directory:?}"
            );

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

        let fetch_output = util::command::new_command("git")

View on GitHub (pinned to 9d272b0363)

Solutions

  1. Verify git is installed and on PATH: run `git --version`; install it if missing (e.g. `apt install git` / `brew install git`).
  2. Delete the broken grammar directory and rebuild so `git init` starts from a clean state: `rm -rf <extensions dir>/work/tree-sitter-<lang>` (or the directory named in the message).
  3. Check the directory's ownership and permissions; ensure the user running Zed can write to it (`ls -ld <directory>`, `chown`/`chmod` as needed).
  4. Ensure the filesystem containing the directory is writable and not mounted read-only.

Example fix

// before (shell): building an extension in a minimal container without git
$ zed --extensions-dir /opt/zed/extensions
// error: failed to run `git init` in directory ...

// after (shell): install git and clear the stale directory first
$ apt-get update && apt-get install -y git
$ rm -rf /opt/zed/extensions/work/tree-sitter-yaml
$ zed --extensions-dir /opt/zed/extensions
Defensive patterns

Strategy: validation

Validate before calling

// Before building, verify git exists and the grammar dir is writable-or-absent
async fn precheck(directory: &std::path::Path) -> Result<(), String> {
    if which::which("git").is_err() {
        return Err("git is not installed or not on PATH".into());
    }
    if directory.exists() && std::fs::metadata(directory).map(|m| m.permissions().readonly()).unwrap_or(true) {
        return Err(format!("grammar directory {:?} is not writable", directory));
    }
    Ok(())
}

Try / catch

// In Node/TS wrappers around the build, surface stderr and the directory
try {
  await buildExtension(path);
} catch (err) {
  if (String(err).includes("failed to run `git init`")) {
    console.error("git is missing or the grammar directory is not writable:", err.message);
  }
  throw err;
}

Prevention

When it happens

Trigger: Calling `compile_grammar` -> `checkout_repo` where the target grammar directory does not yet exist and `git init` (run with `current_dir(directory)`) exits non-zero. Specific conditions: the `git` binary is missing or not on PATH, `directory` points to something that is not a writable empty directory (e.g. permission denied, read-only filesystem), or an environment/config issue makes git refuse to initialize (e.g. GIT_CONFIG_GLOBAL pointing somewhere unusable).

Common situations: Building a Rust/WASM extension in a sandboxed CI environment without git installed; a stale or root-owned grammar cache directory left over from a previous run as a different user; running on a read-only filesystem or in a container whose home/workdir lacks write permissions; corporate security tooling blocking git execution.

Understand the failure class

Background: "git command failed": what it means when a tool shells out to git and git exits non-zero — this error's family across 21 libraries.

Related errors


AI-assisted analysis of zed-industries/zed@9d272b0363 (2026-09-12). Data as JSON: /api/errors/b19e7760fe7d63e2. Report an issue: GitHub.