FoundationAgents/MetaGPT · error · ValueError

Call with_srcs first.

Error message

Call with_srcs first.

What it means

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.

Source

Thrown at metagpt/utils/project_repo.py:130

        srcs_str = f"Srcs({self.srcs.all_files})"
        return f"{repo_str}\n{docs_str}\n{srcs_str}"

    @property
    async def requirement(self):
        return await self.docs.get(filename=REQUIREMENT_FILENAME)

    @property
    def git_repo(self) -> GitRepository:
        return self._git_repo

    @property
    def workdir(self) -> Path:
        return Path(self.git_repo.workdir)

    @property
    def srcs(self) -> FileRepository:
        if not self._srcs_path:
            raise ValueError("Call with_srcs first.")
        return self._git_repo.new_file_repository(self._srcs_path)

    def code_files_exists(self) -> bool:
        src_workdir = get_project_srcs_path(self.git_repo.workdir)
        if not src_workdir.exists():
            return False
        code_files = self.with_src_path(path=src_workdir).srcs.all_files
        if not code_files:
            return False
        return bool(code_files)

    def with_src_path(self, path: str | Path) -> ProjectRepo:
        path = Path(path)
        if path.is_relative_to(self.workdir):
            self._srcs_path = path.relative_to(self.workdir)
        else:
            self._srcs_path = path
        return self

View on GitHub (pinned to 11cdf466d0)

Solutions

  1. Call repo.with_src_path(get_project_srcs_path(repo.workdir)) (or the convenience repo.with_srcs()) before accessing repo.srcs.
  2. Or read code files without srcs: use repo.git_repo.new_file_repository(relative_path) directly with a known path.
  3. Guard access: `if repo._srcs_path: ... repo.srcs ...`.

Example fix

# before
repo = ProjectRepo('/work/myproj')
files = repo.srcs.all_files  # ValueError: Call with_srcs first.

# after
repo = ProjectRepo('/work/myproj').with_srcs()
files = repo.srcs.all_files
Defensive patterns

Strategy: validation

Validate before calling

if not repo._srcs_path:
    repo = repo.with_src_path(get_project_srcs_path(repo.workdir))
files = repo.srcs.all_files

Type guard

def has_srcs_path(repo) -> bool:
    return bool(getattr(repo, '_srcs_path', None))

Try / catch

try:
    files = repo.srcs.all_files
except ValueError as e:
    if 'with_srcs' in str(e):
        repo = repo.with_srcs()
        files = repo.srcs.all_files

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14). Data as JSON: /api/errors/98c7b50c92899cb1. Report an issue: GitHub.