{"id":"bcc9ebbad8f03987","repo":"python-poetry/poetry","slug":"specified-path-path-is-not-a-valid-directory","errorCode":null,"errorMessage":"Specified path '{path}' is not a valid {'directory' if is_directory else 'file'}.","messagePattern":"Specified path '(.+?)' is not a valid (.+?)\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"src/poetry/utils/helpers.py","lineNumber":140,"sourceCode":"        path = Path(os.path.relpath(package.source_url, root)).as_posix()\n        return f\"{package.version} {path}\"\n\n    pretty_version: str = package.full_pretty_version\n    return pretty_version\n\n\ndef paths_csv(paths: list[Path]) -> str:\n    return \", \".join(f'\"{c!s}\"' for c in paths)\n\n\ndef ensure_path(path: str | Path, is_directory: bool = False) -> Path:\n    if isinstance(path, str):\n        path = Path(path)\n\n    if path.exists() and path.is_dir() == is_directory:\n        return path\n\n    raise ValueError(\n        f\"Specified path '{path}' is not a valid {'directory' if is_directory else 'file'}.\"\n    )\n\n\ndef is_dir_writable(path: Path, create: bool = False) -> bool:\n    try:\n        if not path.exists():\n            if not create:\n                return False\n            path.mkdir(parents=True, exist_ok=True)\n\n        with tempfile.TemporaryFile(dir=str(path)):\n            pass\n    except OSError:\n        return False\n    else:\n        return True\n","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/python-poetry/poetry/blob/92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54/src/poetry/utils/helpers.py#L122-L158","documentation":"Raised as ValueError in helpers.ensure_path when the given path either does not exist at all, or exists but its type does not match the is_directory flag (a file passed where a directory was required, or vice versa). It is a path precondition check used at internal call sites.","triggerScenarios":"Calling ensure_path(path, is_directory=...) where not path.exists(), or path.is_dir() != is_directory. Exact branch: helpers.py:137-142. Used wherever Poetry needs to assert an externally-supplied path is a usable file or directory before proceeding.","commonSituations":"Passing a path string that hasn't been created yet; pointing at a file when the API needs a directory (e.g. a site-packages dir); pointing at a directory when a single file is required; relative path resolved against the wrong cwd so it appears missing.","solutions":["Verify the path exists: 'Path(p).exists()'; create it if it should be there.","Match the is_directory flag to what the path actually is (or what the caller requires).","Resolve the path to absolute before checking: 'Path(p).resolve()'.","Create the parent directory or the file itself before calling ensure_path."],"exampleFix":"// before\nensure_path(\"/tmp/missing-dir\", is_directory=True)  # -> ValueError\n\n// after\nPath(\"/tmp/missing-dir\").mkdir(parents=True, exist_ok=True)\nensure_path(\"/tmp/missing-dir\", is_directory=True)","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef path_is_valid(path, is_directory: bool) -> bool:\n    p = Path(path)\n    return p.exists() and p.is_dir() == is_directory\n\n# before ensure_path:\nassert path_is_valid(target, is_directory=is_directory), f\"{target} missing or wrong type\"","typeGuard":"def is_ensure_path_value_error(e: Exception) -> bool:\n    return isinstance(e, ValueError) and \"is not a valid\" in str(e) and (\"directory\" in str(e) or \"file\" in str(e))","tryCatchPattern":"from poetry.utils.helpers import ensure_path\n\ntry:\n    p = ensure_path(target, is_directory=is_directory)\nexcept ValueError as e:\n    if \"is not a valid\" in str(e):\n        # create the missing file/dir, or fix the is_directory flag\n        if is_directory:\n            Path(target).mkdir(parents=True, exist_ok=True)\n        else:\n            Path(target).touch()\n        p = ensure_path(target, is_directory=is_directory)\n    else:\n        raise","preventionTips":["Create the target file or directory before calling ensure_path.","Match the is_directory flag to the path's actual (or intended) type.","Resolve to absolute and confirm existence with Path.exists() first.","Validate parent directories exist before asserting a file path."],"tags":["path-validation","filesystem","precondition","helpers"],"analyzedSha":"92b74dcfe348d0e01e14d40d6c1fa47a4ee04a54","analyzedAt":"2026-08-04T20:33:34.072Z","schemaVersion":2}