{"record":{"id":"40ce7b45da663db6","repo":"oraios/serena","slug":"specified-a-relative-working-directory-cwd-bu","errorCode":null,"errorMessage":"Specified a relative working directory ({cwd}), but the resulting path is not a directory: {_cwd}","messagePattern":"Specified a relative working directory \\((.+?)\\), but the resulting path is not a directory: (.+?)","errorType":"validation","errorClass":"FileNotFoundError","httpStatus":null,"severity":"error","filePath":"src/serena/tools/cmd_tools.py","lineNumber":46,"sourceCode":"          * processes that require user interaction.\n\n        :param command: the shell command to execute\n        :param cwd: the working directory to execute the command in. If None, the project root will be used.\n        :param capture_stderr: whether to capture and return stderr output\n        :param max_answer_chars: if the output is longer than this number of characters,\n            no content will be returned. -1 means using the default value, don't adjust unless there is no other way to get the content\n            required for the task.\n        :return: a JSON object containing the command's stdout and optionally stderr output\n        \"\"\"\n        if cwd is None:\n            _cwd = self.get_project_root()\n        else:\n            if os.path.isabs(cwd):\n                _cwd = cwd\n            else:\n                _cwd = os.path.join(self.get_project_root(), cwd)\n                if not os.path.isdir(_cwd):\n                    raise FileNotFoundError(\n                        f\"Specified a relative working directory ({cwd}), but the resulting path is not a directory: {_cwd}\"\n                    )\n\n        result = execute_shell_command(command, cwd=_cwd, capture_stderr=capture_stderr)\n        result = result.model_dump_json()\n        return self._limit_length(result, max_answer_chars)\n","sourceCodeStart":28,"sourceCodeEnd":53,"githubUrl":"https://github.com/oraios/serena/blob/7fcbca7e62555ec2287ddb2f083caee805848ea6/src/serena/tools/cmd_tools.py#L28-L53","documentation":"Serena's ExecuteShellCommandTool resolves a relative cwd against the project root. If the joined path is not an existing directory, apply() raises FileNotFoundError with this message. The command is never executed, so no shell state was changed.","triggerScenarios":"Calling execute_shell_command (the apply method) with cwd=\"subdir\" when subdir does not exist under the project root, or with a file path instead of a directory, or with a typo'd/renamed relative directory.","commonSituations":"Running build/test commands from agents after a refactor moved the directory; using an absolute-style path string without leading slash (treated as relative); project checked out without submodules or generated folders that the command expects as cwd.","solutions":["Create the missing directory (mkdir -p) or correct the typo in the relative cwd path","Pass an absolute path for cwd so it is used verbatim (os.path.isabs branch)","Verify with os.path.isdir(os.path.join(project_root, cwd)) before invoking the tool","If cwd is optional, omit it to run in the project root"],"exampleFix":"// before\nresult = agent.execute_shell_command(\"npm test\", cwd=\"packages/ap\")\n// after\ncwd = \"packages/app\"\nassert os.path.isdir(os.path.join(project_root, cwd)), cwd\nresult = agent.execute_shell_command(\"npm test\", cwd=cwd)","handlingStrategy":"validation","validationCode":"import os\ncwd_abs = cwd if os.path.isabs(cwd) else os.path.join(project_root, cwd)\nif not os.path.isdir(cwd_abs):\n    raise NotADirectoryError(cwd_abs)\nresult = agent.execute_shell_command(command, cwd=cwd)","typeGuard":"def valid_cwd(cwd: str, root: str) -> bool:\n    p = cwd if os.path.isabs(cwd) else os.path.join(root, cwd)\n    return os.path.isdir(p)","tryCatchPattern":"try:\n    result = agent.execute_shell_command(cmd, cwd=cwd)\nexcept FileNotFoundError as e:\n    if \"not a directory\" in str(e):\n        os.makedirs(os.path.join(project_root, cwd), exist_ok=True)\n        result = agent.execute_shell_command(cmd, cwd=cwd)\n    else:\n        raise","preventionTips":["Verify relative cwds exist after refactors or checkouts","Use absolute paths when the directory location is known","Prefer omitting cwd to run in the project root"],"tags":["filesystem","path","validation"],"backgroundTag":"directory-not-found","analyzedSha":"7fcbca7e62555ec2287ddb2f083caee805848ea6","analyzedAt":"2026-08-29T00:04:09.619Z","schemaVersion":2},"datasetVersion":"2026-08-29T02:17:18.158Z"}