Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate keys #591

Closed
arcanis opened this issue Nov 27, 2020 · 2 comments
Closed

Duplicate keys #591

arcanis opened this issue Nov 27, 2020 · 2 comments

Comments

@arcanis
Copy link

arcanis commented Nov 27, 2020

Ref: #345 (comment)

We're using js-yaml on Yarn (our lockfiles are now regular Yaml), and to make our users' life easier we try to gracefully recover from lockfile merge conflicts (basically we parse both current and incoming versions of the lockfile, then merge them together). It turns out that in some circumstances Git will generate a bogus lockfile that duplicates some keys.

Since js-yaml doesn't support duplicated keys and aborts as soon as it sees one, we can't apply this auto-merge process when this situation happens (in our case, a valid strategy would be to simply ignore the duplicates, as they are assumed to have the same content).

While thinking about this problem I think one possible alternative would be for our project to hack our way around by using a regex to add the line number in front of each top-level key, parsing the files, then removing the number prefixes 🤔 It's sounds quite the ugly workaround though 😅

@rlidwka
Copy link
Member

rlidwka commented Dec 3, 2020

I've just found this by accident going through documentation:

json (default: false) - compatibility with JSON.parse behaviour. If true, then duplicate keys in a mapping will override values rather than throwing an error.

> require('js-yaml').load('foo: 3\nfoo: 4')
Thrown:
{ YAMLException: duplicated mapping key at line 2, column 1 }

> require('js-yaml').load('foo: 3\nfoo: 4', { json: true })
{ foo: 4 }

@puzrin
Copy link
Member

puzrin commented Dec 3, 2020

@arcanis seems we can close that?

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

No branches or pull requests

3 participants