{"id":"6294cb0a2f5e6912","repo":"aio-libs/aiohttp","slug":"directory-does-not-exist","errorCode":null,"errorMessage":"'{directory}' does not exist","messagePattern":"'(.+?)' does not exist","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"aiohttp/web_urldispatcher.py","lineNumber":519,"sourceCode":"    VERSION_KEY = \"v\"\n\n    def __init__(\n        self,\n        prefix: str,\n        directory: PathLike,\n        *,\n        name: str | None = None,\n        expect_handler: _ExpectHandler | None = None,\n        chunk_size: int = DEFAULT_CHUNK_SIZE,\n        show_index: bool = False,\n        break_symlink_sandbox: bool = False,\n        append_version: bool = False,\n    ) -> None:\n        super().__init__(prefix, name=name)\n        try:\n            directory = Path(directory).expanduser().resolve(strict=True)\n        except FileNotFoundError as error:\n            raise ValueError(f\"'{directory}' does not exist\") from error\n        if not directory.is_dir():\n            raise ValueError(f\"'{directory}' is not a directory\")\n        self._directory = directory\n        self._show_index = show_index\n        self._chunk_size = chunk_size\n        self._break_symlink_sandbox = break_symlink_sandbox\n        self._expect_handler = expect_handler\n        self._append_version = append_version\n\n        self._routes = {\n            \"GET\": ResourceRoute(\n                \"GET\", self._handle, self, expect_handler=expect_handler\n            ),\n            \"HEAD\": ResourceRoute(\n                \"HEAD\", self._handle, self, expect_handler=expect_handler\n            ),\n        }\n        self._allowed_methods = set(self._routes)","sourceCodeStart":501,"sourceCodeEnd":537,"githubUrl":"https://github.com/aio-libs/aiohttp/blob/c0ef574e29109210e96e652771ae4e7b88615fa4/aiohttp/web_urldispatcher.py#L501-L537","documentation":"Raised as ValueError by StaticResource.__init__ when Path(directory).resolve(strict=True) raises FileNotFoundError — i.e. the configured static directory does not exist on disk. The static resource needs a real directory to serve files from at setup time, so a missing path is rejected immediately rather than 404-ing every request.","triggerScenarios":"Calling app.router.add_static('/static', '/wrong/path') or web.static('/s', './missing') where the directory does not exist. Relative paths resolved against an unexpected working directory also trigger it.","commonSituations":"Deploying with a different working directory than dev; build step that creates the assets dir not yet run; typo in the path; containerized app where the static dir isn't copied into the image; path from config pointing to a host path not mounted.","solutions":["Verify the directory exists: check with Path(directory).resolve() before add_static.","Use an absolute path derived from __file__: Path(__file__).parent / 'static'.","Ensure build/copy steps (npm run build, COPY in Dockerfile) run before server startup.","In containers, confirm the directory is present at the expected absolute path."],"exampleFix":"# before\napp.router.add_static('/static', '/var/www/assets')  # missing\n\n# after\nfrom pathlib import Path\nstatic_dir = Path(__file__).parent / 'assets'\napp.router.add_static('/static', static_dir)","handlingStrategy":"validation","validationCode":"from pathlib import Path\n\ndef resolve_static_dir(directory):\n    p = Path(directory).expanduser()\n    if not p.exists():\n        raise FileNotFoundError(f\"static directory does not exist: {p}\")\n    return p.resolve()","typeGuard":"from pathlib import Path\n\ndef directory_exists(directory) -> bool:\n    return Path(directory).expanduser().exists()","tryCatchPattern":"try:\n    app.router.add_static('/static', directory)\nexcept ValueError as e:\n    if 'does not exist' in str(e):\n        raise SystemExit(f\"Static dir missing: {e}\") from e\n    raise","preventionTips":["Derive static paths from __file__ (Path(__file__).parent / 'static').","In Docker, COPY assets before CMD and verify the path exists.","Add a startup check that all static dirs exist before binding routes."],"tags":["aiohttp","static-files","filesystem","deployment","value-error"],"analyzedSha":"c0ef574e29109210e96e652771ae4e7b88615fa4","analyzedAt":"2026-08-04T19:51:05.467Z","schemaVersion":2}