{"record":{"id":"68fba864fe29806e","repo":"helix-editor/helix","slug":"could-not-change-working-directory-to-err","errorCode":null,"errorMessage":"Could not change working directory to '{}': {err}","messagePattern":"Could not change working directory to '(.+?)': (.+?)","errorType":"exception","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"helix-term/src/commands/typed.rs","lineNumber":1329,"sourceCode":"        .set_status(cx.editor.registers.clipboard_provider_name());\n    Ok(())\n}\n\n/// Helper function to parse the first argument as a directory\n#[inline]\nfn parse_first_arg_as_dir(args: &Args, last_cwd: Option<PathBuf>) -> anyhow::Result<PathBuf> {\n    match args.first().map(AsRef::as_ref) {\n        Some(\"-\") => last_cwd.ok_or_else(|| anyhow!(\"No previous working directory\")),\n        Some(path) => Ok(helix_stdx::path::expand_tilde(Path::new(path)).into_owned()),\n        None => Ok(home_dir()?),\n    }\n}\n\n/// Helper function to apply a directory change for an already-parsed Path ref\n#[inline]\nfn apply_directory_change(cx: &mut compositor::Context, dir: &Path) -> anyhow::Result<()> {\n    cx.editor.set_cwd(dir).map_err(|err| {\n        anyhow!(\n            \"Could not change working directory to '{}': {err}\",\n            dir.display()\n        )\n    })?;\n\n    cx.editor.set_status(format!(\n        \"Current working directory is now {}\",\n        helix_stdx::env::current_working_dir().display()\n    ));\n\n    Ok(())\n}\n\nfn change_current_directory(\n    cx: &mut compositor::Context,\n    args: Args,\n    event: PromptEvent,\n) -> anyhow::Result<()> {","sourceCodeStart":1311,"sourceCodeEnd":1347,"githubUrl":"https://github.com/helix-editor/helix/blob/079a789e8cb08ead67f19e1971a1b7438b37354b/helix-term/src/commands/typed.rs#L1311-L1347","documentation":"After parse_first_arg_as_dir resolves the target (argument with '~' expanded, '-' as previous dir, or home as default), apply_directory_change calls Editor::set_cwd, which canonicalizes the path and switches the process into it via helix_stdx::env::set_current_working_dir. A nonexistent directory, a path that is a file, or an OS-level permission failure is reported as 'Could not change working directory to <path>: <err>'.","triggerScenarios":"`:cd /nonexistent`; `:cd somefile.txt`; `:cd` into a directory without execute permission; `:cd -` when the previous directory was deleted in the meantime.","commonSituations":"Typos and missing quotes around paths with spaces; expecting environment variables to expand (only a leading '~' is expanded); mounts that went away; permission-restricted directories.","solutions":["Verify the path exists and is a directory (`:sh ls <dir>` or check in a terminal).","Use an absolute path or a leading '~'; note that arbitrary env vars like $HOME are not expanded inside the prompt.","If the path exists but still fails, check execute permissions and mount health."],"exampleFix":"# before\n:cd ~/My Docs\n# after — quote paths containing spaces where your shell/prompt requires, or escape:\n:cd \"~/My Docs\"","handlingStrategy":"validation","validationCode":"let p = std::path::Path::new(target);\nif !(p.is_dir() && p.canonicalize().is_ok()) {\n    // refuse early instead of calling :cd and surfacing the OS error\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Verify the target with `:sh ls <dir>` before scripting `:cd` sequences.","Remember only a leading '~' expands; env vars do not."],"tags":["cwd","filesystem","command"],"backgroundTag":null,"analyzedSha":"079a789e8cb08ead67f19e1971a1b7438b37354b","analyzedAt":"2026-08-16T09:09:59.668Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}