Skip to content

Commit 471d804

Browse files
committed
fix(embedded): Allow empty frontmatter
1 parent 4b2b386 commit 471d804

File tree

1 file changed

+34
-22
lines changed

1 file changed

+34
-22
lines changed

src/cargo/util/toml/embedded.rs

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,18 @@ impl<'s> ScriptSource<'s> {
188188
if !info.is_empty() {
189189
source.info = Some(info);
190190
}
191-
let rest = rest
192-
.strip_prefix('\n')
193-
.expect("earlier `found` + `split_at` left us here");
194191

195192
// Ends with a line that starts with a matching number of `-` only followed by whitespace
196193
let nl_fence_pattern = format!("\n{fence_pattern}");
197194
let Some(frontmatter_nl) = rest.find(&nl_fence_pattern) else {
198195
anyhow::bail!("no closing `{fence_pattern}` found for frontmatter");
199196
};
200197
let frontmatter = &rest[..frontmatter_nl + 1];
201-
let rest = &rest[frontmatter_nl + nl_fence_pattern.len()..];
198+
let frontmatter = frontmatter
199+
.strip_prefix('\n')
200+
.expect("earlier `found` + `split_at` left us here");
202201
source.frontmatter = Some(frontmatter);
202+
let rest = &rest[frontmatter_nl + nl_fence_pattern.len()..];
203203

204204
let (after_closing_fence, rest) = rest.split_once("\n").unwrap_or((rest, ""));
205205
let after_closing_fence = after_closing_fence.trim_matches(WHITESPACE);
@@ -319,18 +319,22 @@ content: "\n// infostrings cannot have leading dots\n\nfn main() {}\n"
319319

320320
#[test]
321321
fn rustc_dot_in_infostring_non_leading() {
322-
assert_err(
323-
ScriptSource::parse(
324-
r#"---Cargo.toml
322+
assert_source(
323+
r#"---Cargo.toml
325324
---
326325
327326
// infostrings can contain dots as long as a dot isn't the first character.
328327
//@ check-pass
329328
330329
fn main() {}
331330
"#,
332-
),
333-
str!["no closing `---` found for frontmatter"],
331+
str![[r#"
332+
shebang: None
333+
info: "Cargo.toml"
334+
frontmatter: ""
335+
content: "\n// infostrings can contain dots as long as a dot isn't the first character.\n//@ check-pass\n\nfn main() {}\n"
336+
337+
"#]],
334338
);
335339
}
336340

@@ -371,7 +375,7 @@ content: "\n//@ check-pass\n\n// This test checks that longer dashes for opening
371375
fn main() {}
372376
"#,
373377
),
374-
str!["no closing `---` found for frontmatter"],
378+
str!["trailing characters found after frontmatter close"],
375379
);
376380
}
377381

@@ -470,9 +474,8 @@ fn foo(x: i32) -> i32 {
470474

471475
#[test]
472476
fn rustc_frontmatter_whitespace_3() {
473-
assert_err(
474-
ScriptSource::parse(
475-
r#"
477+
assert_source(
478+
r#"
476479
477480
478481
---cargo
@@ -488,25 +491,34 @@ fn foo(x: i32) -> i32 {
488491
489492
fn main() {}
490493
"#,
491-
),
492-
str!["no closing `---` found for frontmatter"],
494+
str![[r#"
495+
shebang: None
496+
info: "cargo"
497+
frontmatter: ""
498+
content: "\n// please note the whitespace characters after the first four lines.\n// This ensures that we accept whitespaces before the frontmatter, after\n// the frontmatter opening and the frontmatter close.\n\n//@ check-pass\n// ignore-tidy-end-whitespace\n// ignore-tidy-leading-newlines\n\nfn main() {}\n"
499+
500+
"#]],
493501
);
494502
}
495503

496504
#[test]
497505
fn rustc_frontmatter_whitespace_4() {
498-
assert_err(
499-
ScriptSource::parse(
500-
r#"--- cargo
506+
assert_source(
507+
r#"--- cargo
501508
---
502509
503510
//@ check-pass
504511
// A frontmatter infostring can have leading whitespace.
505512
506513
fn main() {}
507514
"#,
508-
),
509-
str!["no closing `---` found for frontmatter"],
515+
str![[r#"
516+
shebang: None
517+
info: "cargo"
518+
frontmatter: ""
519+
content: "\n//@ check-pass\n// A frontmatter infostring can have leading whitespace.\n\nfn main() {}\n"
520+
521+
"#]],
510522
);
511523
}
512524

@@ -609,8 +621,8 @@ fn main() {}
609621
str![[r#"
610622
shebang: None
611623
info: None
612-
frontmatter: "---\n\n"
613-
content: "//~^ ERROR: expected item, found `-`\n// FIXME(frontmatter): make this diagnostic better\n---\n\n// test that we do not parse another frontmatter block after the first one.\n\nfn main() {}\n"
624+
frontmatter: ""
625+
content: "\n---\n//~^ ERROR: expected item, found `-`\n// FIXME(frontmatter): make this diagnostic better\n---\n\n// test that we do not parse another frontmatter block after the first one.\n\nfn main() {}\n"
614626
615627
"#]],
616628
);

0 commit comments

Comments
 (0)