{"record":{"id":"e6785561b1084fe9","repo":"zed-industries/zed","slug":"cd-directory-cd-was-not-in-any-root-director","errorCode":null,"errorMessage":"`cd` directory {cd:?} was not in any root directory in the project.","messagePattern":"`cd` directory (.+?) was not in any root directory in the project\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/agent/src/tools/terminal_tool.rs","lineNumber":1373,"sourceCode":"            None => Ok(None),\n        }\n    } else {\n        let path_style = project.path_style(cx);\n        let worktree_roots = project\n            .worktrees(cx)\n            .filter_map(|worktree| {\n                let worktree = worktree.read(cx);\n                // Skip single-file worktrees: a file can't be a working directory.\n                let root_dir = worktree.root_dir()?;\n                Some((worktree.root_name_str(), root_dir.to_path_buf()))\n            })\n            .collect::<Vec<_>>();\n\n        if let Some(dir) = resolve_cd_in_worktrees(cd, path_style, &worktree_roots) {\n            return Ok(Some(dir));\n        }\n\n        anyhow::bail!(\"`cd` directory {cd:?} was not in any root directory in the project.\");\n    }\n}\n\n/// Resolves a `cd` argument to an absolute worktree directory. `cd` may be a\n/// worktree's root name or an absolute path to a worktree or a subdirectory\n/// therein.\n///\n/// Absolute paths are classified with the project's [`PathStyle`] rather than\n/// the host's, so an absolute POSIX path resolves correctly on a Windows host\n/// driving a WSL/SSH project (#60040).\n///\n/// Both `cd` and the worktree roots are lexically normalized before prefix\n/// matching. This resolves `.` and `..` components up front, so a path that\n/// escapes a worktree does not have that worktree's root as a prefix and is\n/// rejected (#60014). On Windows-style projects it also unifies `/` and `\\`\n/// separators, since models frequently write `C:/foo/bar` for a root stored\n/// as `C:\\foo\\bar`.\nfn resolve_cd_in_worktrees(","sourceCodeStart":1355,"sourceCodeEnd":1391,"githubUrl":"https://github.com/zed-industries/zed/blob/bc538def4545534201bbfcac4e95ac34ea6501b6/crates/agent/src/tools/terminal_tool.rs#L1355-L1391","documentation":"For any non-trivial cd value, the terminal tool resolves it to a directory inside a worktree: it matches worktree root names and absolute paths, both lexically normalized and classified with the project's PathStyle (not the host's, so POSIX paths work from Windows hosts driving WSL/SSH projects). If no worktree matches, the command is rejected before a shell is spawned. Single-file worktrees are skipped because a file cannot be a working directory.","triggerScenarios":"cd is neither a worktree root name nor a path under any worktree root: outside paths like /tmp or ~/.config, a typo in the root name, or any path into a single-file worktree (its root_dir() is None so it never participates).","commonSituations":"The agent suggests cd-ing to a scratch directory outside the project; the project was opened as a single file; a stale absolute path from another machine.","solutions":["Use an absolute path that lies inside one of the project's worktree roots","Add the target folder to the project (Add Folder to Project) so it becomes a worktree, then retry","Use the exact root name as displayed in the project panel"],"exampleFix":"// before — target outside every worktree\nrun_terminal_command(command=\"ls\", cd=\"/tmp/work\")\n\n// after — add /tmp/work as a project folder, or cd inside an existing root\nrun_terminal_command(command=\"ls\", cd=\"/home/me/repo/scripts\")","handlingStrategy":"validation","validationCode":"let roots: Vec<(String, PathBuf)> = project\n    .read(cx)\n    .worktrees(cx)\n    .filter_map(|worktree| {\n        let worktree = worktree.read(cx);\n        Some((worktree.root_name_str().to_string(), worktree.root_dir()?.to_path_buf()))\n    })\n    .collect();\nlet resolves = cd == \".\"\n    || roots.iter().any(|(name, root)| cd == *name || root.join(&cd).starts_with(root));\nif !resolves {\n    // cd will be rejected; pick a directory inside a worktree root\n}","typeGuard":null,"tryCatchPattern":"On the 'not in any root directory' message, list the project's worktree roots back to the caller and request a corrected cd.","preventionTips":["Keep scratch work the agent must touch inside a project worktree (or add the folder to the project)","Use root names exactly as shown in the project panel; single-file worktrees can never serve as cd targets"],"tags":["agent","terminal","cd","worktree","path-resolution"],"backgroundTag":null,"analyzedSha":"bc538def4545534201bbfcac4e95ac34ea6501b6","analyzedAt":"2026-08-16T07:30:46.435Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}