Skip to content

fix(yaml): comments can be key-values - #1289

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
KevSlashNull:comment-colon
Jun 17, 2026
Merged

alecthomas merged 1 commit into
alecthomas:masterfrom
KevSlashNull:comment-colon

Conversation

@KevSlashNull

Copy link
Copy Markdown
Contributor

This fixes a bug where comments that come after a name tag and contain a colon character are parsed as key-value pair instead of as comment.

For example, the following YAML is parsed as having the key version: 2 # Required:

version: 2 # Required: Configuration format version

Therefore, this makes the NameTag regex stricter and doesn't allow the hash character in name tags, avoiding the bad parsing.

Example

A current example of how this breaks in practice is our GitLab Caproni docs site, which uses imfing/hextra, which uses Chroma. The YAML comments are treated partially as keys instead of comments.

image

This fixes a bug where comments that come after a name tag and contain a
colon character are parsed as key-value pair instead of as comment.

For example, the following YAML is parsed as having the key
`version: 2 # Required`:

    version: 2 # Required: Configuration format version

Therefore, this makes the `NameTag` regex stricter and doesn't allow the
hash character in name tags, avoiding the bad parsing.

A current example of how this breaks in practice is our GitLab Caproni
docs site [1], which uses imfing/hextra, which uses Chroma. The YAML
comments are treated partially as keys instead of comments.

[1]: https://gitlab-org.gitlab.io/caproni/config/
@alecthomas
alecthomas enabled auto-merge (squash) June 17, 2026 00:08
@alecthomas
alecthomas merged commit 6da8f88 into alecthomas:master Jun 17, 2026
2 checks passed
@KevSlashNull
KevSlashNull deleted the comment-colon branch June 17, 2026 07:23
alecthomas pushed a commit that referenced this pull request Oct 16, 2026
Addresses #1333.

YAML uses `#` for comments ([YAML 1.2.2
§6.6](https://yaml.org/spec/1.2.2/#66-comments)). The `//` rules added
in #1278 instead consume valid plain scalars and prevent
slash-containing keys from matching:

```yaml
path: //server/share
proto: //cdn.example.com/lib.js
text: prefix // suffix
//key: value
```

Remove both slash-comment rules and their exclusions in the key
patterns. Keep the `#` exclusions from #1289 and leave the numeric
changes in #1372 separate.

The fixture replaces the invalid trailing-`//` comment examples with
valid quoted strings, plain scalars, keys, sequence items, and a real
`#` comment containing a colon. The new expectations fail with the
original lexer and pass with this change.

### Validation

Using the repository's Hermit toolchain (Go 1.26.6):

- `go test -count=1 ./...`
- `go test -race -count=1 ./...`
- `just check-generated` — no generated changes
- `golangci-lint run`
- `git diff --check`

Also checked actual CLI output for 25 parser-valid examples: 14
regressions are corrected and 11 controls are unchanged. HTML output
keeps real hash comments while rendering slash values and keys with
their scalar/key classes.

An additional plain `go vet ./...` check reports the same existing
unkeyed-composite diagnostics on master and this branch; the
repository-configured lint passes. No Go source or dependency files are
changed.
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

⌂ 换网址