{"record":{"id":"0f8f7e6aa3ca9546","repo":"xai-org/grok-build","slug":"failed-to-set-working-directory-to","errorCode":null,"errorMessage":"Failed to set working directory to {:?}: {}","messagePattern":"Failed to set working directory to (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-grok-pager/src/app/cli.rs","lineNumber":854,"sourceCode":"            .unwrap_or(\"grok\")\n            .to_owned();\n        Self::parse_from(std::iter::once(bin_name).chain(std::env::args().skip(1)))\n    }\n    /// Apply launch-directory path anchoring and `--cwd` after early commands have been dispatched without filesystem or process initialization.\n    pub fn apply_cwd(self) -> anyhow::Result<Self> {\n        let launch_dir = std::env::current_dir().ok();\n        self.apply_cwd_from(launch_dir.as_deref())\n    }\n    fn apply_cwd_from(mut self, launch_dir: Option<&std::path::Path>) -> anyhow::Result<Self> {\n        if let Some(socket) = self.leader_socket.take() {\n            self.leader_socket = Some(anchor_to_launch_dir(socket, launch_dir));\n        }\n        if let Some(file) = self.debug_file.take() {\n            self.debug_file = Some(anchor_to_launch_dir(file, launch_dir));\n        }\n        if let Some(ref cwd) = self.cwd {\n            std::env::set_current_dir(cwd).map_err(|e| {\n                anyhow::anyhow!(\"Failed to set working directory to {:?}: {}\", cwd, e)\n            })?;\n        }\n        Ok(self)\n    }\n    /// Optional-flag accessor; always `false` in builds without the optional feature, so call sites need no `cfg` of their own.\n    pub fn chat(&self) -> bool {\n        false\n    }\n    /// `--local-workspace[=cwd]` own-mode flag.\n    #[cfg(feature = \"local-workspace\")]\n    pub fn local_workspace(&self) -> Option<Option<&std::path::Path>> {\n        self.local_workspace.as_ref().map(|inner| inner.as_deref())\n    }\n    /// `--local-workspace-attach=<server_id>`.\n    #[cfg(feature = \"local-workspace\")]\n    pub fn local_workspace_attach(&self) -> Option<&str> {\n        self.local_workspace_attach.as_deref()\n    }","sourceCodeStart":836,"sourceCodeEnd":872,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-grok-pager/src/app/cli.rs#L836-L872","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Verify the path passed to --cwd exists and is a directory","Fix permissions on the target directory or run with sufficient rights","Use an absolute path to avoid dependence on the current directory","If cwd is optional, drop the flag to stay in the launch directory"],"exampleFix":"// before\nmyapp --cwd ./missing-dir chat\n// after\nmkdir -p ./missing-dir && myapp --cwd ./missing-dir chat\n# or validate first:\n[ -d \"$dir\" ] && myapp --cwd \"$dir\" chat","handlingStrategy":"validation","validationCode":"fn cwd_ok(p: &std::path::Path) -> bool {\n    p.is_dir() && std::fs::metadata(p).map(|m| !m.permissions().readonly()).unwrap_or(false)\n}\n// before invoking the CLI: assert cwd_ok(Path::new(&cwd))","typeGuard":null,"tryCatchPattern":"match myapp::run(opts) {\n    Err(e) if e.to_string().starts_with(\"Failed to set working directory\") => {\n        eprintln!(\"bad --cwd: {e:#}\");\n        std::process::exit(2);\n    }\n    r => r,\n}","preventionTips":["Validate --cwd exists and is a directory before launch ([ -d \"$dir\" ])","Use absolute paths for --cwd","Re-check that the directory still exists after any script that may delete it","Ensure the launching user has execute permission on the target directory"],"tags":["filesystem","cli","cwd","io"],"backgroundTag":"chdir-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}