Skip to content

Commit 9dcf8a4

Browse files
authored
Allow comment-heavy YAML in check-yaml (#2554)
Closes #2501
1 parent 053a576 commit 9dcf8a4

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,13 @@ serde_json = { version = "1.0.132", features = [
8686
"unbounded_depth"
8787
] }
8888
serde_stacker = { version = "0.1.12" }
89-
serde-saphyr = { version = "1.0.0", default-features = false, features = [
89+
serde-saphyr = { version = "1.1.0", default-features = false, features = [
9090
"deserialize",
9191
"serialize"
9292
] }
9393
shlex = { version = "2.0.0" }
9494
globset = { version = "0.4.18" }
95-
granit-parser = { version = "1.0.1" }
95+
granit-parser = { version = "1.1.0" }
9696
similar = { version = "3.0.0" }
9797
ssl-certs = { version = "0.1.1" }
9898
strum = { version = "0.28.0", features = ["derive"] }

crates/prek/src/hooks/pre_commit_hooks/check_yaml.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ fn check_loaded(filename: &Path, content: &[u8], allow_multi_docs: bool) -> Hook
7171
// high-reuse anchors that are common in compose-style files. See #1838.
7272
enforce_alias_anchor_ratio: false,
7373
},
74+
// Comments do not affect syntax or structure validation. See #2501.
75+
emit_comments: false,
7476
// Do not require `!!binary` scalars to decode as UTF-8. See #1102.
7577
ignore_binary_tag_for_string: true,
7678
// The scalar values are discarded, so only validate whether they are
@@ -102,9 +104,15 @@ fn check_syntax(filename: &Path, content: &[u8]) -> HookOutput {
102104
}
103105
};
104106

107+
let options = granit_parser::options! {
108+
// Comments do not affect syntax validation. See #2501.
109+
emit_comments: false,
110+
};
111+
105112
// TODO: `granit-parser` resolves aliases while producing parser events, so this is stricter
106113
// than upstream's syntax-only mode for aliases that reference an undefined anchor.
107-
for event in granit_parser::Parser::new_from_str(content) {
114+
for event in granit_parser::Parser::with_options(granit_parser::StrInput::new(content), options)
115+
{
108116
if let Err(error) = event {
109117
let error_message = format!("{}: Failed to yaml parse ({error})\n", filename.display());
110118
return HookOutput::unchanged(1, error_message.into_bytes());
@@ -346,4 +354,27 @@ response:
346354
assert!(result.output.is_empty());
347355
Ok(())
348356
}
357+
358+
#[tokio::test]
359+
async fn test_yaml_with_many_consecutive_comments() -> Result<()> {
360+
let dir = tempdir()?;
361+
let mut content = "items:\n".to_string();
362+
for index in 0..100 {
363+
let _ = writeln!(content, " # comment {index}");
364+
}
365+
content.push_str(" - value\n");
366+
367+
let file_path = create_test_file(&dir, "many-comments.yaml", content.as_bytes()).await?;
368+
for mode in [LOAD_SINGLE_DOCUMENT, CheckMode::SyntaxOnly] {
369+
let result = check_file(Path::new(""), &file_path, mode).await?;
370+
assert_eq!(
371+
result.exit_status,
372+
0,
373+
"{}",
374+
String::from_utf8_lossy(&result.output)
375+
);
376+
assert!(result.output.is_empty());
377+
}
378+
Ok(())
379+
}
349380
}

crates/prek/tests/yaml_to_toml.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,19 @@ fn yaml_to_toml_rejects_invalid_config() -> anyhow::Result<()> {
210210
context
211211
.command()
212212
.args(["util", "yaml-to-toml", "config.yaml"]),
213-
@"
213+
@r#"
214214
success: false
215215
exit_code: 2
216216
----- stdout -----
217217
218218
----- stderr -----
219219
error: Failed to parse `config.yaml`
220-
caused by: error: line 1 column 8: unexpected event: expected sequence start
220+
caused by: error: line 1 column 8: expected sequence start
221221
--> <input>:1:8
222222
|
223223
1 | repos: 123
224-
| ^ unexpected event: expected sequence start
225-
"
224+
| ^ expected sequence start
225+
"#
226226
);
227227

228228
Ok(())

0 commit comments

Comments
 (0)