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

YAML: Node Anchor followed by a blank line: "Nested mappings are not allowed in compact mappings" #10510

Closed
jdufresne opened this issue Mar 12, 2021 · 13 comments · Fixed by #10516
Labels
lang:yaml Issues affecting YAML locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:bug Issues identifying ugly output, or a defect in the program

Comments

@jdufresne
Copy link

jdufresne commented Mar 12, 2021

Prettier 2.2.1
Playground link

Input:

key1: &default

  subkey1: value1

key2:
  <<: *default

Output:

test.yml[error] test.yml: SyntaxError: Nested mappings are not allowed in compact mappings (1:7)
[error] > 1 | key1: &default
[error]     |       ^^^^^^^^
[error] > 2 | 
[error]     | ^
[error] > 3 |   subkey1: value1
[error]     | ^
[error] > 4 | 
[error]     | ^
[error]   5 | key2:
[error]   6 |   <<: *default
[error]   7 | 

Expected behavior:

No error. The file should be formatted.

Both Python and Ruby YAML libraries load this file with the expected results.

In my production code, the blank line exists there to separate a comment.

key1: &default

  # This key ...
  subkey1: value1

key2:
  <<: *default

Which actually results in a different error:

test.yml[error] test.yml: SyntaxError: All collection items must start at the same column (1:1)
[error] > 1 | key1: &default
[error]     | ^^^^^^^^^^^^^^
[error] > 2 | 
[error]     | ^
[error] > 3 |   # This key ...
[error]     | ^
[error] > 4 |   subkey1: value1
[error]     | ^
[error] > 5 | 
[error]     | ^
[error] > 6 | key2:
[error]     | ^
[error] > 7 |   <<: *default
[error]     | ^
[error] > 8 | 
[error]     | ^
@thorn0
Copy link
Member

thorn0 commented Mar 12, 2021

To confirm that this is valid YAML and the parsing errors are incorrect, please use the spec, not 3rd party implementations. I'm really sure about the robustness of Prettier's underlying parser: yaml-unist-parser, which in turn uses eemeli/yaml, but if you can prove this is indeed an error on their part, we'll open an issue in the corresponding repo.

@thorn0 thorn0 added lang:yaml Issues affecting YAML status:awaiting response Issues that require answers to questions from maintainers before action can be taken labels Mar 12, 2021
@jdufresne
Copy link
Author

jdufresne commented Mar 13, 2021

Yes, this is valid YAML. There is nothing in the spec suggesting blank lines between keys are invalid, even when a node anchor is involved.

I only presented other tools as examples of compliant tools, not as an authority. Sorry for the confusion.

@no-response no-response bot removed the status:awaiting response Issues that require answers to questions from maintainers before action can be taken label Mar 13, 2021
@thorn0 thorn0 added the status:needs investigation Issues that need additional investigation, e.g. to understand whether the reported behavior is a bug label Mar 13, 2021
@thorn0
Copy link
Member

thorn0 commented Mar 13, 2021

Okay, thanks for the report. I'll try to find time to look into it. Meanwhile, to make things happen faster, you can open an issue in https://github.com/eemeli/yaml yourself.

@jdufresne
Copy link
Author

👍 I'll go ahead with that. Thanks for the response.

@thorn0
Copy link
Member

thorn0 commented Mar 13, 2021

Great. Thanks. Let's keep the issue open though.

@thorn0 thorn0 reopened this Mar 13, 2021
@jdufresne
Copy link
Author

jdufresne commented Mar 13, 2021

Upstream issue eemeli/yaml#242

@thorn0 thorn0 added scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency and removed status:needs investigation Issues that need additional investigation, e.g. to understand whether the reported behavior is a bug labels Mar 13, 2021
@eemeli
Copy link
Contributor

eemeli commented Mar 13, 2021

Yeah, this is a bug in yaml. There it's already fixed in its master branch, via the new lexer + parser that's a major reason for the library's ongoing v2 overhaul.

So this poses a bit of a problem, as I've got my hands full in that work, and I'd been hoping to avoid needing to do more v1 patch releases. For most users, v2 should work perfectly fine without any changes, but underneath, the CST changes completely. And as I recall, that's what @ikatyang's yaml-unist-parser uses.

Posting about this here to make sure that you have visibility into this; not sure yet about how to resolve this.

@eemeli
Copy link
Contributor

eemeli commented Mar 13, 2021

I resolved the issue by releasing a patch update yaml@1.10.1.

@thorn0 thorn0 added the type:bug Issues identifying ugly output, or a defect in the program label Mar 13, 2021
@thorn0 thorn0 mentioned this issue Mar 13, 2021
3 tasks
@thorn0
Copy link
Member

thorn0 commented Mar 13, 2021

Thanks @eemeli. This way we can fix it without touching yaml-unist-parser.

However, I noticed something. Is this expected?

Prettier pr-10516
Playground link

Input:

key1: &default
# This key
  subkey1: value1

key2:
  <<: *default

Output:

SyntaxError: All collection items must start at the same column (1:1)
> 1 | key1: &default
    | ^^^^^^^^^^^^^^
> 2 | # This key
    | ^^^^^^^^^^
> 3 |   subkey1: value1
    | ^^^^^^^^^^
> 4 |
    | ^^^^^^^^^^
> 5 | key2:
    | ^^^^^^^^^^
> 6 |   <<: *default
    | ^^^^^^^^^^^^^^^

@eemeli
Copy link
Contributor

eemeli commented Mar 13, 2021

No, that's not expected. Bother. But I'm pretty sure I can fix this pretty easily. You may expect a yaml@1.10.2 shortly.

@eemeli
Copy link
Contributor

eemeli commented Mar 13, 2021

Yah, it was what I thought. yaml@1.10.2 is out with this fixed.

@jdufresne
Copy link
Author

Thank you @eemeli and @thorn0 for the very prompt responses and fixes! 🎉 🚀

@thorn0
Copy link
Member

thorn0 commented Mar 13, 2021

@jdufresne You aren't going to like the resulting formatting though. As can be seen in the snapshots in the PR, the comment after the anchor doesn't stay on its own line. That'll be a separate issue.

mergify bot pushed a commit to gnosis/gp-ui that referenced this issue Apr 6, 2021
Bumps [yaml](https://github.com/eemeli/yaml) from 1.10.0 to 1.10.2.
<details>
<summary>Release notes</summary>
<p><em>Sourced from <a href="https://github.com/eemeli/yaml/releases">yaml's releases</a>.</em></p>
<blockquote>
<h2>v1.10.2</h2>
<ul>
<li>Allow for unindented comment after node props (prettier/prettier#10510)</li>
</ul>
<h2>v1.10.1</h2>
<p>This release backports the following non-breaking fixes made during the work on <code>yaml@2</code> on top of <code>yaml@1.10.0</code>:</p>
<ul>
<li>Support for<code> __proto__</code> as mapping key &amp; anchor identifier (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/192">#192</a>)</li>
<li>Fix broken TS type for BigInt toggle</li>
<li>Dump long keys properly (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/195">#195</a>)</li>
<li>When folding highly indented lines, require at least <code>minContentWidth</code> chars on the first line (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/196">#196</a>)</li>
<li>Fix <code>YAML.stringify()</code> for certain null values (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/197">#197</a>)</li>
<li>Do not break escaped chars with escaped newlines (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/237">#237</a>, <a href="https://github-redirect.dependabot.com/awslabs/cdk8s/issues/8">awslabs/cdk8s8</a>)</li>
<li>Set <code>type: &quot;module&quot;</code> within browser/dist/ (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/208">#208</a>)</li>
<li>Use CommonJS for the browser endpoints <code>yaml/types</code> &amp; <code>yaml/util</code> (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/208">#208</a>)</li>
<li>Always stringify non-Node object keys using explicit notation (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/218">#218</a>)</li>
<li>Specify node type of <code>Document.Parsed.contents</code> (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/221">#221</a>)</li>
<li>Add missing type for CST <code>Node.rangeAsLinePos</code> (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/222">#222</a>)</li>
<li>Prefer literal over folded block scalar when <code>lineWidth=0</code> is set (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/232">#232</a>)</li>
<li>Allow for empty lines after node props (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/242">#242</a>)</li>
<li>Update dev dependencies</li>
</ul>
</blockquote>
</details>
<details>
<summary>Commits</summary>
<ul>
<li><a href="https://github.com/eemeli/yaml/commit/4cdcde632ece71155f3108ec0120c1a0329a6914"><code>4cdcde6</code></a> 1.10.2</li>
<li><a href="https://github.com/eemeli/yaml/commit/7c0e08316d82f167ac0a054428627f6e1f20ac6e"><code>7c0e083</code></a> Allow for unindented comment after node props (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/242">#242</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/8ef015788b219a4b249736f4bb8968dafe68dcc4"><code>8ef0157</code></a> 1.10.1</li>
<li><a href="https://github.com/eemeli/yaml/commit/6296dae2e5f61a4aa4605b4a374bd94ec5713c3a"><code>6296dae</code></a> Update links in docs</li>
<li><a href="https://github.com/eemeli/yaml/commit/b1d2b287e80caeb262c4dc81459f52b982a5e741"><code>b1d2b28</code></a> Allow for empty lines after node props (Fixes <a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/242">#242</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/3e5a64098791ea7a7c01a5465a0794049b511367"><code>3e5a640</code></a> Satisfy Prettier</li>
<li><a href="https://github.com/eemeli/yaml/commit/bd031cb67f4411826bd61cc2b3bbe21b1b398755"><code>bd031cb</code></a> Update dev dependencies + switch to lockfileVersion 2</li>
<li><a href="https://github.com/eemeli/yaml/commit/9c6e7d0ed367b2af439cfc52936e65b2fc3e5ecc"><code>9c6e7d0</code></a> Use CommonJS for browser endpoints yaml/types &amp; yaml/util (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/208">#208</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/7ddb18b4e18d4d4625b94af8f64738a4725cbbb5"><code>7ddb18b</code></a> Prefer literal over folded block scalar when lineWidth=0 is set (<a href="https://github-redirect.dependabot.com/eemeli/yaml/issues/232">#232</a>)</li>
<li><a href="https://github.com/eemeli/yaml/commit/fd817be1774145aec9354a30c4b48cc08fe98e41"><code>fd817be</code></a> Update dev dependencies</li>
<li>Additional commits viewable in <a href="https://github.com/eemeli/yaml/compare/v1.10.0...v1.10.2">compare view</a></li>
</ul>
</details>
<br />


[![Dependabot compatibility score](https://api.dependabot.com/badges/compatibility_score?dependency-name=yaml&package-manager=npm_and_yarn&previous-version=1.10.0&new-version=1.10.2)](https://dependabot.com/compatibility-score/?dependency-name=yaml&package-manager=npm_and_yarn&previous-version=1.10.0&new-version=1.10.2)

You can trigger a rebase of this PR by commenting `@dependabot rebase`.

[//]: # (dependabot-automerge-start)
Dependabot will merge this PR once CI passes on it, as requested by @alfetopito.

[//]: # (dependabot-automerge-end)

---

<details>
<summary>Dependabot commands and options</summary>
<br />

You can trigger Dependabot actions by commenting on this PR:
- `@dependabot rebase` will rebase this PR
- `@dependabot recreate` will recreate this PR, overwriting any edits that have been made to it
- `@dependabot merge` will merge this PR after your CI passes on it
- `@dependabot squash and merge` will squash and merge this PR after your CI passes on it
- `@dependabot cancel merge` will cancel a previously requested merge and block automerging
- `@dependabot reopen` will reopen this PR if it is closed
- `@dependabot close` will close this PR and stop Dependabot recreating it. You can achieve the same result by closing it manually
- `@dependabot ignore this major version` will close this PR and stop Dependabot creating any more for this major version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this minor version` will close this PR and stop Dependabot creating any more for this minor version (unless you reopen the PR or upgrade to it yourself)
- `@dependabot ignore this dependency` will close this PR and stop Dependabot creating any more for this dependency (unless you reopen the PR or upgrade to it yourself)
- `@dependabot use these labels` will set the current labels as the default for future PRs for this repo and language
- `@dependabot use these reviewers` will set the current reviewers as the default for future PRs for this repo and language
- `@dependabot use these assignees` will set the current assignees as the default for future PRs for this repo and language
- `@dependabot use this milestone` will set the current milestone as the default for future PRs for this repo and language
- `@dependabot badge me` will comment on this PR with code to add a "Dependabot enabled" badge to your readme

Additionally, you can set the following in your Dependabot [dashboard](https://app.dependabot.com):
- Update frequency (including time of day and day of week)
- Pull request limits (per update run and/or open at any time)
- Out-of-range updates (receive only lockfile updates, if desired)
- Security updates (receive only security updates, if desired)



</details>
@github-actions github-actions bot added the locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. label Jun 13, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
lang:yaml Issues affecting YAML locked-due-to-inactivity Please open a new issue and fill out the template instead of commenting. scope:dependency Issues that cannot be solved inside Prettier itself, and must be fixed in a dependency type:bug Issues identifying ugly output, or a defect in the program
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants