`basic` formatter converts an implicit null to an empty string in flow mappings
- Dominant language
- Go
- Stars
- 1.8k
- Forks
- 75
- PR merge metrics
- No merged PRs in 30d
Description
### yamlfmt version
0.21.0, also on main at a74383c
### Issue
An implicit null inside a flow mapping is rewritten as an empty string, so the
formatted document holds a different value:
```console
$ printf 'a: { b: }\n' > repro.yaml
$ yamlfmt repro.yaml
$ cat repro.yaml
a: {b: ''}
```
`{a: null}`, `{a: ~}` and block style all survive. The rewrite is idempotent, so
the file looks settled after the first run.
This can break a GitHub Actions workflow.
`on: { push: { branches: [main] }, workflow_dispatch: }` formats to
`workflow_dispatch: ''`, which Actions rejects:
```
example.yml:2:51: "workflow_dispatch" section is scalar node but mapping node is expected [syntax-check]
```
I hit this on a repository of mine and the workflow stopped running. Workaround:
spell the null as `{a: null}`, which survives formatting.
The cause is in `pkg/yaml/emitterc.go`. An empty scalar gets
`flow_plain_allowed = false` while `block_plain_allowed` stays true, so in flow
style the emitter falls back to single quoting and the null becomes `''`.
`gopkg.in/yaml.v3` behaves the same way, and only the `Node` API reaches this
path.
Will send a PR.
Contributor guide
Research direction
Start in pkg/yaml/emitterc.go and reproduce the issue with a flow mapping containing an implicit null, such as `a: { b: }`. Trace the Node API emission path and verify that formatting preserves the null rather than producing `''`; done means the example and the GitHub Actions `workflow_dispatch` mapping retain their meaning after formatting.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, yaml
- Domain
- cli, tooling
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Active
- Clarity
- Clearly specified
- Newbie friendliness
- 84/100