Skip to content

Yaml: only treat % at the start of a line as a directive - #8561

Merged
timtebeek merged 1 commit into
mainfrom
tim/yaml-percent-in-leading-comment
Aug 19, 2026
Merged

Yaml: only treat % at the start of a line as a directive#8561
timtebeek merged 1 commit into
mainfrom
tim/yaml-percent-in-leading-comment

Conversation

@timtebeek

@timtebeek timtebeek commented Aug 19, 2026

Copy link
Copy Markdown
Member

Problem

A comment before the first (implicit) document containing a % was parsed as a directive:

# 100%

key: value

printed back as

# 100%
key: value
  • Parser.requirePrintEqualsInput then rejects the file and it becomes a ParseError — so any YAML whose header comment block contains a percent-encoded URL or prose like # 100% coverage drops out of the LST entirely. Introduced by Support for directives in Yaml #6529 (v8.72.0).

Fix

YamlParser.parseDirectivesFromPrefix scanned for % anywhere in the document prefix. It now applies the same "must be at the start of a line" guard that its sibling preScanDirectives already had.

While testing this I found a second, related bug: preScanDirectives treated a leading comment line as document content and skipped past everything up to the next ---, so a directive following a comment was never pre-scanned. It then fell back to reconstructing the directive from SnakeYAML's DocumentStartEvent, whose DumperOptions.Version enum has no 1.2 — so

# comment
%YAML 1.2
---
key: value

round-tripped as %YAML 1.1. preScanDirectives now accumulates a comment line into the pending directive prefix instead, keeping the directive's exact source text.

Tests

Four cases added to DirectiveTest, covering the variants from the issue (comment before a mapping, before a sequence, a comment starting with %) plus the directive-after-comment case above. All four fail on main.

A comment before the first document that contains a `%` (for example a URL
with a percent-encoded character, or prose like `# 100% coverage`) was parsed
as a directive, which relocated the comment and any trailing blank line on
print and turned the source into a `ParseError`.

`parseDirectivesFromPrefix` now applies the same start-of-line guard that
`preScanDirectives` already had. `preScanDirectives` in turn no longer treats
a leading comment line as document content, so a directive that follows a
comment is still pre-scanned and keeps its exact text.

Fixes #8560
@timtebeek

Copy link
Copy Markdown
Member Author

Given the small size of the change, the readability, and test coverage, I'm fairly confident we can see this through and rely on asynchronous review if needed.

@timtebeek
timtebeek merged commit 146a598 into main Aug 19, 2026
1 check passed
@timtebeek
timtebeek deleted the tim/yaml-percent-in-leading-comment branch August 19, 2026 18:06
@github-project-automation github-project-automation Bot moved this from In Progress to Done in OpenRewrite Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working parser yaml

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

YamlParser not print idempotent when a comment before the first document contains %

1 participant