{"record":{"id":"957568ed29d25164","repo":"sxyazi/yazi","slug":"failed-to-get-current-working-directory","errorCode":null,"errorMessage":"failed to get current working directory","messagePattern":"failed to get current working directory","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"yazi-fs/src/cwd.rs","lineNumber":26,"sourceCode":"\npub static CWD: RoCell<Cwd> = RoCell::new();\n\npub struct Cwd(ArcSwap<UrlBuf>);\n\nimpl Deref for Cwd {\n\ttype Target = ArcSwap<UrlBuf>;\n\n\tfn deref(&self) -> &Self::Target { &self.0 }\n}\n\nimpl Default for Cwd {\n\tfn default() -> Self {\n\t\tlet u = std::env::var_os(\"PWD\")\n\t\t\t.map(PathBuf::from)\n\t\t\t.filter(|p| p.is_absolute())\n\t\t\t.map(clean_url)\n\t\t\t.or_else(|| current_dir().ok().map(UrlBuf::from))\n\t\t\t.expect(\"failed to get current working directory\");\n\n\t\tSelf(ArcSwap::new(Arc::new(u)))\n\t}\n}\n\nimpl Cwd {\n\tpub fn path(&self) -> PathBuf { self.0.load().as_url().working_path().into_owned() }\n\n\tpub fn set(&self, url: &UrlBuf, callback: fn()) -> bool {\n\t\tif !url.is_absolute() {\n\t\t\treturn false;\n\t\t} else if self.0.load().as_ref() == url {\n\t\t\treturn false;\n\t\t}\n\n\t\tself.0.store(Arc::new(url.clone()));\n\t\tSelf::sync_cwd(callback);\n","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/sxyazi/yazi/blob/94abcfa92f4ad3f0a1aef6c1ea083cfb8aa6c8c2/yazi-fs/src/cwd.rs#L8-L44","documentation":"Cwd::default() panics through expect when neither source yields a working directory: $PWD is unset or not absolute, and std::env::current_dir() (getcwd) returns an error. yazi bootstraps its process-wide CWD from an absolute $PWD first, then falls back to the OS; both failing means the OS cannot report a valid current directory.","triggerScenarios":"Initializing yazi-fs (CWD RoCell) with $PWD unset or relative while getcwd fails: the current directory was deleted (ENOENT, e.g. after git clean, docker rm of a bind mount, or a resurrected tmux session) or a parent directory lost execute permission (EACCES).","commonSituations":"Starting yazi from a shell whose cwd was removed by another process; tmux/screen session restore pointing at a gone directory; daemonized/service contexts with no cwd; wrappers that clear the environment including PWD.","solutions":["cd to a live directory (cd /tmp) and relaunch — fixes the deleted-cwd case immediately","Recreate the deleted directory (mkdir -p /old/cwd) so getcwd resolves again","Export an absolute PWD before starting (cd \"$PWD\" in the shell, or env PWD=\"$PWD\" yazi when PWD drifted)","For embedders: std::env::set_current_dir(\"/\") and set PWD to an absolute path before initializing yazi-fs","Fix execute (x) permission on parent directories if the failure is EACCES"],"exampleFix":"// before: yazi-fs initialized while the process cwd was deleted\n// -> panic \"failed to get current working directory\"\n\n// after: pin a live directory before init\nif std::env::current_dir().is_err() {\n    std::env::set_var(\"PWD\", \"/\"); // Cwd::default honors an absolute PWD first\n    std::env::set_current_dir(\"/\").ok();\n}","handlingStrategy":"validation","validationCode":"fn cwd_usable() -> bool {\n    std::env::var_os(\"PWD\").is_some_and(|p| std::path::Path::new(&p).is_absolute())\n        || std::env::current_dir().is_ok()\n}\n// before initializing yazi-fs:\nif !cwd_usable() {\n    std::env::set_current_dir(\"/\").ok();\n    std::env::set_var(\"PWD\", \"/\");\n}","typeGuard":null,"tryCatchPattern":"let cwd = std::panic::catch_unwind(Cwd::default)\n    .unwrap_or_else(|_| Cwd(ArcSwap::new(Arc::new(UrlBuf::from(\"/\"))))); // embedder-side last resort","preventionTips":["Launch yazi from a directory that exists; avoid resurrected shells whose cwd was deleted","Keep $PWD absolute in wrapper scripts (cd \"$PWD\" before exec)","When embedding, call set_current_dir to a known-good dir before touching yazi-fs"],"tags":["environment","startup","panic","filesystem"],"backgroundTag":null,"analyzedSha":"94abcfa92f4ad3f0a1aef6c1ea083cfb8aa6c8c2","analyzedAt":"2026-08-16T09:56:24.836Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}