diff --git a/skillscope/structure.py b/skillscope/structure.py index 171eaf5..9bba866 100644 --- a/skillscope/structure.py +++ b/skillscope/structure.py @@ -143,9 +143,13 @@ def _frontmatter(text: str) -> tuple[dict | None, str, str]: import yaml # noqa: PLC0415 -- keeps the run path stdlib-only + # Any exception, not only YAMLError: PyYAML rejects some input with + # ValueError, AttributeError or KeyError (a tag whose value it cannot + # construct, such as `!!timestamp x`), and deep nesting raises + # RecursionError. `safe_load` of a string does nothing else that can fail. try: declared = yaml.safe_load(match.group("frontmatter")) - except yaml.YAMLError as exc: + except Exception as exc: # noqa: BLE001 -- whatever safe_load raises is the frontmatter's fault return None, "", f"frontmatter is not valid YAML: {exc}" if not isinstance(declared, dict): return None, "", ( diff --git a/tests/test_skillscope.py b/tests/test_skillscope.py index 5d4cf7b..e950be4 100644 --- a/tests/test_skillscope.py +++ b/tests/test_skillscope.py @@ -1610,6 +1610,23 @@ def test_frontmatter_that_is_not_yaml_is_reported_as_such(self) -> None: self.declares("name: [demo-skill\ndescription: broken") self.assertTrue(any("not valid YAML" in e for e in structure.errors())) + def test_yaml_the_parser_rejects_without_a_yaml_error_is_reported_too(self) -> None: + # Each of these is rejected with something other than a YAMLError: + # ValueError, AttributeError, KeyError, and RecursionError for nesting + # deeper than the interpreter allows. Each used to end the structural + # check with a traceback. + for name, frontmatter in ( + ("date", "name: demo-skill\ndescription: d\nupdated: 2001-13-45"), + ("timestamp tag", "name: demo-skill\ndescription: d\nupdated: !!timestamp x"), + ("bool tag", "name: demo-skill\ndescription: d\ndraft: !!bool x"), + ("nesting", "name: demo-skill\ndescription: " + "[" * 2_000), + ): + with self.subTest(name): + self.declares(frontmatter) + errors = structure.errors() + self.assertEqual(len(errors), 1, errors) + self.assertIn("not valid YAML", errors[0]) + def test_frontmatter_that_is_not_a_mapping_is_reported(self) -> None: self.declares("- demo-skill\n- a list, not a mapping") self.assertTrue(any("must be a mapping" in e for e in structure.errors()))