{"record":{"id":"c257966da1c8f1a4","repo":"xai-org/grok-build","slug":"failed-to-get-cwd-e","errorCode":null,"errorMessage":"Failed to get cwd: {e}","messagePattern":"Failed to get cwd: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-pager/src/app/session_startup.rs","lineNumber":809,"sourceCode":"///\n/// Agent `session/new` rejects non-UUID `_meta.sessionId`; fail fast here so\n/// CLI users get a clear error before ACP.\npub fn ensure_session_id_available(session_id: &str, cwd: &str) -> anyhow::Result<()> {\n    if uuid::Uuid::try_parse(session_id).is_err() {\n        anyhow::bail!(\"Error: --session-id must be a valid UUID (got '{session_id}').\");\n    }\n    if xai_grok_shell::session::persistence::session_exists_for_cwd(session_id, cwd) {\n        anyhow::bail!(\"Error: Session ID {session_id} is already in use.\");\n    }\n    Ok(())\n}\n/// Materialize CLI intent into a concrete startup plan (I/O + remote restore).\npub async fn materialize_startup(\n    ctx: MaterializeCtx,\n    intent: SessionStartupIntent,\n) -> anyhow::Result<MaterializedStartup> {\n    let cwd = std::env::current_dir()\n        .map_err(|e| anyhow::anyhow!(\"Failed to get cwd: {e}\"))?\n        .to_string_lossy()\n        .to_string();\n    materialize_startup_for_cwd(ctx, intent, &cwd).await\n}\n/// Same as [`materialize_startup`] but with an explicit process cwd (tests, headless).\npub async fn materialize_startup_for_cwd(\n    ctx: MaterializeCtx,\n    intent: SessionStartupIntent,\n    cwd: &str,\n) -> anyhow::Result<MaterializedStartup> {\n    if ctx.chat_mode && matches!(intent, SessionStartupIntent::ForkFrom { .. }) {\n        anyhow::bail!(\"{CHAT_MODE_FORK_CONFLICT}\");\n    }\n    match intent {\n        SessionStartupIntent::NewAuto => Ok(MaterializedStartup::NewAuto),\n        SessionStartupIntent::NewWithId { session_id } => {\n            if !ctx.has_worktree {\n                ensure_session_id_available(&session_id, cwd)?;","sourceCodeStart":791,"sourceCodeEnd":827,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-pager/src/app/session_startup.rs#L791-L827","documentation":"materialize_startup reads the process working directory with std::env::current_dir() to resolve the session-startup plan. If the OS call fails (typically ENOENT), the error is wrapped as 'Failed to get cwd' and startup aborts, because every path/session resolution needs a valid cwd.","triggerScenarios":"Calling materialize_startup after the process's working directory has been deleted or made unreachable (e.g. the directory was removed or renamed while the process ran, or on a network filesystem that dropped).","commonSituations":"Deleting or renaming the project directory in another shell while grok runs inside it; container/WSL mounts disappearing; running the binary after a tmpdir cleanup removed its cwd.","solutions":["cd back to an existing directory and relaunch grok","Recreate the deleted directory if the shell still points at it (mkdir -p)","Check the filesystem/mount is still available (especially network/WSL mounts)","Prefer launching grok from a stable, existing absolute path"],"exampleFix":"// before: shell cwd was deleted\n$ rm -rf ~/old-project  # while grok runs here\n// after: return to a live directory first\n$ cd ~/new-project && grok resume","handlingStrategy":"validation","validationCode":"// verify the cwd is still reachable before materializing startup\nfn cwd_alive() -> bool {\n    std::env::current_dir().is_ok()\n}","typeGuard":null,"tryCatchPattern":"match materialize_startup(ctx, intent).await {\n    Err(e) if e.to_string().starts_with(\"Failed to get cwd:\") => {\n        eprintln!(\"working directory vanished; cd to a valid dir and retry\");\n        std::env::set_current_dir(\"/\").ok();\n    }\n    other => other.map(|_| ()),\n}","preventionTips":["Do not delete/rename the directory a running grok process is inside","Launch from stable paths (home or project root), not ephemeral tmpdirs","Keep mounts (network/WSL) healthy before starting long sessions","If cwd may vanish, resolve and cache an absolute path early"],"tags":["filesystem","cwd","startup"],"backgroundTag":"cwd-deleted","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}