diff --git a/src/apm_cli/compilation/agents_compiler.py b/src/apm_cli/compilation/agents_compiler.py index 8e0968f6c..b09559d6a 100644 --- a/src/apm_cli/compilation/agents_compiler.py +++ b/src/apm_cli/compilation/agents_compiler.py @@ -1276,14 +1276,12 @@ def _write_output_file_with_config( if config.agents_md_mode == "managed_section": target = Path(output_path) - if not target.is_file(): - raise ManagedSectionError( - f"{target} does not exist yet. " - "Create it with the managed-section markers first, " - "or set agents_md.mode: full in apm.yml for initial generation." - ) - existing = target.read_text(encoding="utf-8") try: + if not target.exists(): + raise ManagedSectionError( + f"[{target.name}] does not exist yet. Create it with markers first, or use mode: full for initial generation." + ) + existing = target.read_text(encoding="utf-8") content = apply_managed_section( existing, content, @@ -1291,7 +1289,7 @@ def _write_output_file_with_config( config.agents_md_end_marker, ) except ManagedSectionError as exc: - raise ManagedSectionError(f"[{target}] {exc}") from exc + raise ManagedSectionError(f"[{target.name}] {exc}") from exc elif config.agents_md_mode != "full": raise ValueError( f"Unknown agents_md.mode {config.agents_md_mode!r}. " diff --git a/tests/unit/compilation/test_managed_section.py b/tests/unit/compilation/test_managed_section.py index 7dee76894..c96f8419f 100644 --- a/tests/unit/compilation/test_managed_section.py +++ b/tests/unit/compilation/test_managed_section.py @@ -374,5 +374,11 @@ def test_write_output_file_managed_section_directory_at_path(self, tmp_path): ) compiler = AgentsCompiler(str(tmp_path)) + with pytest.raises(ManagedSectionError) as exc_info: + compiler._write_output_file_with_config(str(output_file), "New content.\n", config) + + msg = str(exc_info.value) + assert "AGENTS.md does not exist yet. Create it with markers first, or use mode: full for initial generation." in msg + assert msg.startswith("[") with pytest.raises(ManagedSectionError, match=r"(?i)does not exist|not exist|create it"): compiler._write_output_file_with_config(str(output_file), "New content.\n", config)