xai-org/grok-build · error

Failed to set working directory to {:?}: {}

Error message

Failed to set working directory to {:?}: {}

What it means

std::env::set_current_dir failed when applying the --cwd option during CLI launch initialization. The library wraps the io::Error so launch aborts rather than running commands from the wrong directory.

Source

Thrown at crates/codegen/xai-grok-pager/src/app/cli.rs:854

            .unwrap_or("grok")
            .to_owned();
        Self::parse_from(std::iter::once(bin_name).chain(std::env::args().skip(1)))
    }
    /// Apply launch-directory path anchoring and `--cwd` after early commands have been dispatched without filesystem or process initialization.
    pub fn apply_cwd(self) -> anyhow::Result<Self> {
        let launch_dir = std::env::current_dir().ok();
        self.apply_cwd_from(launch_dir.as_deref())
    }
    fn apply_cwd_from(mut self, launch_dir: Option<&std::path::Path>) -> anyhow::Result<Self> {
        if let Some(socket) = self.leader_socket.take() {
            self.leader_socket = Some(anchor_to_launch_dir(socket, launch_dir));
        }
        if let Some(file) = self.debug_file.take() {
            self.debug_file = Some(anchor_to_launch_dir(file, launch_dir));
        }
        if let Some(ref cwd) = self.cwd {
            std::env::set_current_dir(cwd).map_err(|e| {
                anyhow::anyhow!("Failed to set working directory to {:?}: {}", cwd, e)
            })?;
        }
        Ok(self)
    }
    /// Optional-flag accessor; always `false` in builds without the optional feature, so call sites need no `cfg` of their own.
    pub fn chat(&self) -> bool {
        false
    }
    /// `--local-workspace[=cwd]` own-mode flag.
    #[cfg(feature = "local-workspace")]
    pub fn local_workspace(&self) -> Option<Option<&std::path::Path>> {
        self.local_workspace.as_ref().map(|inner| inner.as_deref())
    }
    /// `--local-workspace-attach=<server_id>`.
    #[cfg(feature = "local-workspace")]
    pub fn local_workspace_attach(&self) -> Option<&str> {
        self.local_workspace_attach.as_deref()
    }

View on GitHub (pinned to bc7f02eddd)

Solutions

  1. Verify the path passed to --cwd exists and is a directory
  2. Fix permissions on the target directory or run with sufficient rights
  3. Use an absolute path to avoid dependence on the current directory
  4. If cwd is optional, drop the flag to stay in the launch directory

Example fix

// before
myapp --cwd ./missing-dir chat
// after
mkdir -p ./missing-dir && myapp --cwd ./missing-dir chat
# or validate first:
[ -d "$dir" ] && myapp --cwd "$dir" chat
Defensive patterns

Strategy: validation

Validate before calling

fn cwd_ok(p: &std::path::Path) -> bool {
    p.is_dir() && std::fs::metadata(p).map(|m| !m.permissions().readonly()).unwrap_or(false)
}
// before invoking the CLI: assert cwd_ok(Path::new(&cwd))

Try / catch

match myapp::run(opts) {
    Err(e) if e.to_string().starts_with("Failed to set working directory") => {
        eprintln!("bad --cwd: {e:#}");
        std::process::exit(2);
    }
    r => r,
}

Prevention

When it happens

Trigger: Calling the CLI with a cwd option pointing to a path that does not exist, is not a directory, or the process lacks permission to chdir into it (set_current_dir returns Err).

Common situations: Typo in --cwd path, directory deleted between shell completion and launch, running in a container/sandbox without access to the path, restricted permissions after su/sudo.

Related errors


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