xai-org/grok-build · error

jj workspace add failed: {e}

Error message

jj workspace add failed: {e}

What it means

Wraps a failure of `jj workspace add` — the external Jujutsu VCS CLI invoked to create a new workspace at `dest` with the given name, tracking revision `@`. The command is run via jj_cli_mut; any non-zero exit, missing binary, or invalid argument is converted into this anyhow error. It indicates the jj workspace creation step of worktree creation failed before any commit id could be resolved.

Source

Thrown at crates/codegen/xai-grok-workspace/src/worktree/mod.rs:2437

    // Ensure parent directory exists: jj workspace add doesn't create it.
    if let Some(parent) = Path::new(&dest).parent() {
        tokio::fs::create_dir_all(parent).await?;
    }

    tracing::info!(
        source = %req.source_worktree_path,
        dest = %dest,
        name = %name,
        "creating jj workspace"
    );

    jj_cli_mut(
        source_path,
        &["workspace", "add", &dest, "--name", &name, "-r", "@"],
    )
    .await
    .map_err(|e| anyhow::anyhow!("jj workspace add failed: {e}"))?;

    let commit = jj_commit_id(Path::new(&dest)).await;
    tracing::info!(dest = %dest, commit = ?commit, "jj workspace created");

    Ok(CreateWorktreeFromWorktreeResponse {
        status: "created".to_string(),
        new_session_id: req.new_session_id.clone(),
        worktree_path: dest,
        commit,
        copied_changes: None,
        source_git_root,
    })
}

/// Remove a jj workspace: forget + delete directory.
pub async fn remove_jj_workspace(workspace_path: &str) -> Result<()> {
    let path = Path::new(workspace_path);
    let name = path

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Run the same `jj workspace add <dest> --name <name> -r @` command manually in source_path to see jj's stderr detail.
  2. Ensure jj is installed and on PATH (`jj --version`) in the environment running the code.
  3. Delete the stale destination directory or choose a fresh dest path/name.
  4. Verify source_path is inside a valid jj repository and `@` resolves (`jj log`).

Example fix

// before
jj_cli_mut(source_path, &["workspace", "add", &dest, "--name", &name, "-r", "@"])
    .await
    .map_err(|e| anyhow::anyhow!("jj workspace add failed: {e}"))?;
// after
if Path::new(&dest).exists() {
    anyhow::bail!("workspace destination {dest} already exists; remove it first");
}
jj_cli_mut(source_path, &["workspace", "add", &dest, "--name", &name, "-r", "@"])
    .await
    .map_err(|e| anyhow::anyhow!("jj workspace add failed: {e}; ensure jj is installed and '{name}' is unused"))?;
Defensive patterns

Strategy: try-catch

Validate before calling

// before calling
if !Path::new(&dest).exists() && which::which("jj").is_ok() {
    // ensure name is not already used
    let listed = jj_cli_mut(source_path, &["workspace", "list"]).await?;
    if listed.contains(name) { anyhow::bail!("workspace name {name} already exists"); }
}

Try / catch

match create_worktree_from_worktree(src, &dest, name).await {
    Ok(resp) => resp,
    Err(e) if e.to_string().contains("jj workspace add failed") => {
        eprintln!("jj unavailable or workspace conflict: {e:#}");
        // clean stale dest, then retry once
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Calling create-worktree-from-worktree when the jj CLI exits non-zero: workspace name already exists, destination path already exists or is not a valid location, `-r @` cannot resolve (repo has no working-copy commit), or jj binary is absent/broken.

Common situations: Jujutsu not installed or not on PATH in CI images; leftover workspace directory from a previous failed run; trying to reuse a `--name` that already exists in the repo; running outside a jj-backed repository; corrupt or locked jj repo.

Related errors


AI-assisted analysis of xai-org/grok-build@bc7f02eddd (2026-08-31). Data as JSON: /api/errors/71de507652945a09. Report an issue: GitHub.