Skip to content

Conversation

@codeafridi
Copy link

Summary

I fixed an issue with YAML frontmatter where duplicate keys caused a vague error that didn’t point to the real problem. I added a simple check that detects repeated keys and shows a clear message with the key name and line number which points to the file

Test plan

-ensured normal frontmatter files still parse correctly.
-ran the formatter manually to confirm it now identifies the duplicated key.
-verified that the error message displays the correct key and the line where it occurs.

Checklist

Please add a x inside each checkbox:

@codeafridi codeafridi requested a review from a team as a code owner December 8, 2025 13:40
Copy link
Member

@martinjagodic martinjagodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This correctly prints to the console which key is duplicated on which line. It's frustrating that it doesn't tell me which file is affected in the collection view, so I have to guess that. It also breaks loading of affected entries - I get an empty card in the collection view if it's affected. I'm testing this on a large collection and my console is bobmarded with these messages

  • Don't throw an error - this will enable the title to be displayed on the collection view even if an entry is affected. Show a warning instead, don't include a stack trace.
  • Add info about the affected file along with the key and line number
  • When an entry is open in Editor mode, only show errors for that file, not every file in the collection.

To discuss:
Should we display a warning to the user? If they are not a developer, they will have no control over this, so it could be annoying. However, this would be an effective way to fund such issues.

@codeafridi
Copy link
Author

Thanks for the feedback. ill update it now.

  • i will remove the thrown error and replace it with a simple console.log warn without a stack trace.
  • i will include the file path, key name, and line number in the warning message so it’s clear which entry is affected.
    -then limit the detection to the currently opened entry, so warnings don’t appear for every file in the collection.
  • yes cms users shouldn’t see this so i will keep it in the console only for developers.

I’ll push the changes soon.

Copy link
Member

@martinjagodic martinjagodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is better now. For me, all files return as unknown while testing locally with decap proxy.

Warnings in the console are ok now. I noticed another problem: there are false positives when the same key appears on several levels. For example

title: root title
object:
  title: object title

will give an error that title is a duplicate on line 3, even though it should be fine.

Also, make sure that all tests are passing.

@codeafridi
Copy link
Author

Alright now I get it. I'll now

-parse yaml structure properly
-track full key paths
-only warn when the same key appears in the same parent scope
-ennsuring all existing tests pass before pushing the update

@codeafridi
Copy link
Author

do tell if any changes are needed.

Copy link
Member

@martinjagodic martinjagodic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To make duplicate-key detection more robust and precise, please leverage the yaml parser’s built-in diagnostics instead of manual checks.

Concretely:

  • Use yaml.parseDocument(src, { uniqueKeys: true, prettyErrors: true }).
  • Inspect doc.errors/doc.warnings

This way, we can also pretty print any other formatting problems, not just duplicate keys.

Note: we had js-yaml and yaml both used in this project, but since #7684 we only have yaml.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants