{"record":{"id":"98c7b50c92899cb1","repo":"FoundationAgents/MetaGPT","slug":"call-with-srcs-first","errorCode":null,"errorMessage":"Call with_srcs first.","messagePattern":"Call with_srcs first\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"metagpt/utils/project_repo.py","lineNumber":130,"sourceCode":"        srcs_str = f\"Srcs({self.srcs.all_files})\"\n        return f\"{repo_str}\\n{docs_str}\\n{srcs_str}\"\n\n    @property\n    async def requirement(self):\n        return await self.docs.get(filename=REQUIREMENT_FILENAME)\n\n    @property\n    def git_repo(self) -> GitRepository:\n        return self._git_repo\n\n    @property\n    def workdir(self) -> Path:\n        return Path(self.git_repo.workdir)\n\n    @property\n    def srcs(self) -> FileRepository:\n        if not self._srcs_path:\n            raise ValueError(\"Call with_srcs first.\")\n        return self._git_repo.new_file_repository(self._srcs_path)\n\n    def code_files_exists(self) -> bool:\n        src_workdir = get_project_srcs_path(self.git_repo.workdir)\n        if not src_workdir.exists():\n            return False\n        code_files = self.with_src_path(path=src_workdir).srcs.all_files\n        if not code_files:\n            return False\n        return bool(code_files)\n\n    def with_src_path(self, path: str | Path) -> ProjectRepo:\n        path = Path(path)\n        if path.is_relative_to(self.workdir):\n            self._srcs_path = path.relative_to(self.workdir)\n        else:\n            self._srcs_path = path\n        return self","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/FoundationAgents/MetaGPT/blob/11cdf466d042aece04fc6cfd13b28e1a70341b1f/metagpt/utils/project_repo.py#L112-L148","documentation":"Raised by ProjectRepo.srcs: the property returns a FileRepository rooted at self._srcs_path, but that path is only set after with_src_path()/with_srcs() has been called on the ProjectRepo instance. Accessing repo.srcs before that is a usage error, not a data error.","triggerScenarios":"repo = ProjectRepo('/x'); repo.srcs.all_files without a prior repo.with_src_path('path/to/srcs') call. Note __init__ itself calls code_files_exists() which uses with_src_path on a temporary copy, so the original instance still has _srcs_path=None.","commonSituations":"Code written against a flow where with_srcs() ran earlier (WriteCode/RunCode actions); iterating a fresh ProjectRepo in scripts or tests expecting srcs to be pre-populated; refactors that dropped the with_src_path call.","solutions":["Call repo.with_src_path(get_project_srcs_path(repo.workdir)) (or the convenience repo.with_srcs()) before accessing repo.srcs.","Or read code files without srcs: use repo.git_repo.new_file_repository(relative_path) directly with a known path.","Guard access: `if repo._srcs_path: ... repo.srcs ...`."],"exampleFix":"# before\nrepo = ProjectRepo('/work/myproj')\nfiles = repo.srcs.all_files  # ValueError: Call with_srcs first.\n\n# after\nrepo = ProjectRepo('/work/myproj').with_srcs()\nfiles = repo.srcs.all_files","handlingStrategy":"validation","validationCode":"if not repo._srcs_path:\n    repo = repo.with_src_path(get_project_srcs_path(repo.workdir))\nfiles = repo.srcs.all_files","typeGuard":"def has_srcs_path(repo) -> bool:\n    return bool(getattr(repo, '_srcs_path', None))","tryCatchPattern":"try:\n    files = repo.srcs.all_files\nexcept ValueError as e:\n    if 'with_srcs' in str(e):\n        repo = repo.with_srcs()\n        files = repo.srcs.all_files","preventionTips":["Chain construction: ProjectRepo(p).with_srcs() when you will read srcs.","Remember with_src_path returns a new/copied repo object — use its return value.","In tests, always set a srcs path before asserting on srcs contents."],"tags":["metagpt","repository","usage-order"],"backgroundTag":null,"analyzedSha":"11cdf466d042aece04fc6cfd13b28e1a70341b1f","analyzedAt":"2026-08-14T23:20:02.994Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}