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
- Run 'git init' manually in the directory named by the message to see the real git error
- Check the directory and cache-root permissions and disk space for the extension cache filesystem
- Inspect ~/.gitconfig / GIT_* env for anything breaking git (test with GIT_CONFIG_GLOBAL=/dev/null git init)
- 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
- Keep the extensions cache on a writable local filesystem with free space
- Validate global git config occasionally ('git config --list --global') so silent breakage surfaces early
- Install git and keep it on PATH in build environments
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
- grammar directory '{}' already exists, but is not a git clon
- failed to compile {} parser with clang: {}
- A worktree already exists at {}
- grammar name '{grammar_name}' must be written in snake_case:
- extension dir {} is not an absolute path
AI-assisted analysis of zed-industries/zed@f4178619ac (2026-08-20).
Data as JSON: /api/errors/5f9d5b8f73b0a88d.
Report an issue: GitHub.