google / google/yamlfmt

`yamlfmt` Fails to Parse Block Scalars with Initial Newline

Open
#213 2 comments 0 reactions 0 assignees View on GitHub
yaml_v3_problem
Dominant language
Go
Stars
1.8k
Forks
75
PR merge metrics
No merged PRs in 30d

Description

`yamlfmt` encounters an error when parsing YAML files that contain block scalars (`|-`) with an initial newline after the block scalar indicator. This results in a parsing failure with the following error message:

```
❯ yamlfmt file.yml
encountered the following formatting errors:
file.yml: yaml: line 3: did not find expected key
```

The issue occurs when the YAML content is formatted as follows:

```yaml
# file.yml

- id: item-1
description: |-

A lot of

multi-line

copy here.
```

However, if the initial newline after the `|-` is removed, the content is getting parsed correctly:

```yaml
# file.yml

- id: item-1
description: |-
A lot of

multi-line

copy here.
```

#### Expected Behavior

`yamlfmt` should correctly parse and format YAML content that includes an initial newline after a block scalar indicator (`|-`). As far as I can tell this seems to be supported in YAML.

#### Steps to Reproduce

1. Create a YAML file (`file.yml`) with the following content:

```yaml
- id: item-1
description: |-

A lot of

multi-line

text here.
```

2. Run `yamlfmt` on the file:

```
yamlfmt file.yml
```

3. Observe the parsing error:
```
encountered the following formatting errors:
file.yml: yaml: line 3: did not find expected key
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.