{"record":{"id":"42e0822b29c04bed","repo":"microsoft/autogen","slug":"working-directory-not-properly-initialized","errorCode":null,"errorMessage":"Working directory not properly initialized","messagePattern":"Working directory not properly initialized","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py","lineNumber":394,"sourceCode":"        return CommandLineCodeResult(exit_code=last_exit_code, output=\"\".join(outputs), code_file=code_file)\n\n    @property\n    def work_dir(self) -> Path:\n        # If a user specifies a working directory, use that\n        if self._work_dir is not None:\n            # If a user specifies the current directory, warn them that this is deprecated\n            if self._work_dir == Path(\".\"):\n                warnings.warn(\n                    \"Using the current directory as work_dir is deprecated.\",\n                    DeprecationWarning,\n                    stacklevel=2,\n                )\n            return self._work_dir\n        # If a user does not specify a working directory, use the default directory (tempfile.TemporaryDirectory)\n        elif self._temp_dir is not None:\n            return Path(self._temp_dir.name)\n        else:\n            raise RuntimeError(\"Working directory not properly initialized\")\n\n    @property\n    def bind_dir(self) -> Path:\n        # If the user specified a bind directory, return it\n        if self._bind_dir is not None:\n            return self._bind_dir\n        # Otherwise bind_dir is set to the current work_dir as default\n        else:\n            return self.work_dir\n\n    async def execute_code_blocks(\n        self, code_blocks: List[CodeBlock], cancellation_token: CancellationToken\n    ) -> CommandLineCodeResult:\n        \"\"\"(Experimental) Execute the code blocks and return the result.\n\n        Args:\n            code_blocks (List[CodeBlock]): The code blocks to execute.\n","sourceCodeStart":376,"sourceCodeEnd":412,"githubUrl":"https://github.com/microsoft/autogen/blob/027ecf0a379bcc1d09956d46d12d44a3ad9cee14/python/packages/autogen-ext/src/autogen_ext/code_executors/docker/_docker_code_executor.py#L376-L412","documentation":"Raised by the DockerCommandLineCodeExecutor.work_dir property when neither an explicit work_dir was given at construction nor the temp directory exists. work_dir resolves to: user-specified dir if provided, else the TemporaryDirectory created during start(); if neither is available the executor is in an uninitialized state and this RuntimeError fires.","triggerScenarios":"Accessing executor.work_dir (directly, or via bind_dir / file-listing helpers) before start() was ever called on an executor constructed with work_dir=None. Also reachable via code paths that touch work_dir after a failed start where temp-dir creation was skipped.","commonSituations":"Inspecting executor.work_dir immediately after construction to plan file placement; components that snapshot bind_dir early during wiring before the container starts; tests constructing executors without entering the context manager.","solutions":["Access work_dir only after start(): use `async with DockerCommandLineCodeExecutor() as e:` and read e.work_dir inside the block","Pass an explicit work_dir at construction if you need the path before starting: work_dir=\"/tmp/mywork\"","For pre-start planning, compute your own path and pass it as work_dir rather than reading the property"],"exampleFix":"# before\nexecutor = DockerCommandLineCodeExecutor()\nprint(executor.work_dir)  # RuntimeError: not initialized\n\n# after\nexecutor = DockerCommandLineCodeExecutor(work_dir=\"/tmp/mywork\")\nprint(executor.work_dir)  # /tmp/mywork, valid before start","handlingStrategy":"validation","validationCode":"null","typeGuard":"def has_work_dir(executor) -> bool:\n    return executor._work_dir is not None or executor._temp_dir is not None","tryCatchPattern":"try:\n    wd = executor.work_dir\nexcept RuntimeError:\n    await executor.start()\n    wd = executor.work_dir","preventionTips":["Pass an explicit work_dir if you need the path before start()","Read work_dir only inside the `async with` block after the container has started","Pre-compute your own paths and hand them to the constructor instead of reading the property early"],"tags":["docker","lifecycle","filesystem","state"],"backgroundTag":null,"analyzedSha":"027ecf0a379bcc1d09956d46d12d44a3ad9cee14","analyzedAt":"2026-08-15T03:38:00.719Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}