linera-io/linera-protocol · error · anyhow::Error

failed to initialize git repository at {}

Error message

failed to initialize git repository at {}

What it means

Project::initialize_git_repository shells out to `git init` for the freshly scaffolded project and fails when the command exits non-zero. The scaffold itself is complete; only the VCS initialization failed.

Source

Thrown at linera-service/src/project.rs:159

    }

    fn create_test_directory(project_root: &Path) -> Result<PathBuf> {
        let test_directory = project_root.join("tests");
        fs_err::create_dir_all(&test_directory)?;
        Ok(test_directory)
    }

    fn initialize_git_repository(project_root: &Path) -> Result<()> {
        let output = Command::new("git")
            .args([
                "init",
                project_root
                    .to_str()
                    .context("project name contains non UTF-8 characters")?,
            ])
            .output()?;

        ensure!(
            output.status.success(),
            "failed to initialize git repository at {}",
            project_root.display()
        );

        Self::write_string_to_file(&project_root.join(".gitignore"), "/target")
    }

    fn create_cargo_toml(
        project_root: &Path,
        project_name: &str,
        linera_root: Option<&Path>,
    ) -> Result<()> {
        let toml_path = project_root.join("Cargo.toml");
        let (linera_sdk_dep, linera_sdk_dev_dep) = Self::linera_sdk_dependencies(linera_root);
        let binary_root_name = project_name.replace('-', "_");
        let contract_binary_name = format!("{binary_root_name}_contract");
        let service_binary_name = format!("{binary_root_name}_service");

View on GitHub (pinned to 6c226ddcb3)

Solutions

  1. Verify git is installed and on PATH (`git --version`)
  2. Check write permissions on the project directory and re-run `git init <path>` manually to see the real error
  3. Install git in the image/environment (e.g. `apt-get install -y git`) and re-scaffold or init manually
  4. Inspect global git config/templates if git init fails reproducibly

Example fix

# before (container without git)
$ linera project new my-dapp
error: failed to initialize git repository at my-dapp

# after
$ apt-get install -y git
$ linera project new my-dapp
Defensive patterns

Strategy: validation

Validate before calling

let git_ok = std::process::Command::new("git")
    .arg("--version")
    .output()
    .map(|o| o.status.success())
    .unwrap_or(false);
if !git_ok {
    eprintln!("git not available -- skipping repository initialization");
}

Prevention

When it happens

Trigger: Calling initialize_git_repository when git is absent from PATH, the project directory was removed mid-run, or filesystem permissions deny creating .git.

Common situations: Minimal Docker or CI images without git installed; read-only output directories; unusual global git templates or configs that make `git init` fail; safe.directory restrictions in containers.

Related errors


AI-assisted analysis of linera-io/linera-protocol@6c226ddcb3 (2026-08-22). Data as JSON: /api/errors/e65b8460b5948c8e. Report an issue: GitHub.