Skip to content

Commit 5263d58

Browse files
committed
fix(embedded): Allow empty frontmatter
1 parent af31b0c commit 5263d58

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
@@ -120,18 +120,18 @@ impl<'s> ScriptSource<'s> {
120120
if !info.is_empty() {
121121
source.info = Some(info);
122122
}
123-
let rest = rest
124-
.strip_prefix('\n')
125-
.expect("earlier `found` + `split_at` left us here");
126123

127124
// Ends with a line that starts with a matching number of `-` only followed by whitespace
128125
let nl_fence_pattern = format!("\n{fence_pattern}");
129126
let Some(frontmatter_nl) = rest.find(&nl_fence_pattern) else {
130127
anyhow::bail!("no closing `{fence_pattern}` found for frontmatter");
131128
};
132129
let frontmatter = &rest[..frontmatter_nl + 1];
133-
let rest = &rest[frontmatter_nl + nl_fence_pattern.len()..];
130+
let frontmatter = frontmatter
131+
.strip_prefix('\n')
132+
.expect("earlier `found` + `split_at` left us here");
134133
source.frontmatter = Some(frontmatter);
134+
let rest = &rest[frontmatter_nl + nl_fence_pattern.len()..];
135135

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

252252
#[test]
253253
fn rustc_dot_in_infostring_non_leading() {
254-
assert_err(
255-
ScriptSource::parse(
256-
r#"---Cargo.toml
254+
assert_source(
255+
r#"---Cargo.toml
257256
---
258257
259258
// infostrings can contain dots as long as a dot isn't the first character.
260259
//@ check-pass
261260
262261
fn main() {}
263262
"#,
264-
),
265-
str!["no closing `---` found for frontmatter"],
263+
str![[r#"
264+
shebang: None
265+
info: "Cargo.toml"
266+
frontmatter: ""
267+
content: "\n// infostrings can contain dots as long as a dot isn't the first character.\n//@ check-pass\n\nfn main() {}\n"
268+
269+
"#]],
266270
);
267271
}
268272

@@ -303,7 +307,7 @@ content: "\n//@ check-pass\n\n// This test checks that longer dashes for opening
303307
fn main() {}
304308
"#,
305309
),
306-
str!["no closing `---` found for frontmatter"],
310+
str!["trailing characters found after frontmatter close"],
307311
);
308312
}
309313

@@ -402,9 +406,8 @@ fn foo(x: i32) -> i32 {
402406

403407
#[test]
404408
fn rustc_frontmatter_whitespace_3() {
405-
assert_err(
406-
ScriptSource::parse(
407-
r#"
409+
assert_source(
410+
r#"
408411
409412
410413
---cargo
@@ -420,25 +423,34 @@ fn foo(x: i32) -> i32 {
420423
421424
fn main() {}
422425
"#,
423-
),
424-
str!["no closing `---` found for frontmatter"],
426+
str![[r#"
427+
shebang: None
428+
info: "cargo"
429+
frontmatter: ""
430+
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"
431+
432+
"#]],
425433
);
426434
}
427435

428436
#[test]
429437
fn rustc_frontmatter_whitespace_4() {
430-
assert_err(
431-
ScriptSource::parse(
432-
r#"--- cargo
438+
assert_source(
439+
r#"--- cargo
433440
---
434441
435442
//@ check-pass
436443
// A frontmatter infostring can have leading whitespace.
437444
438445
fn main() {}
439446
"#,
440-
),
441-
str!["no closing `---` found for frontmatter"],
447+
str![[r#"
448+
shebang: None
449+
info: "cargo"
450+
frontmatter: ""
451+
content: "\n//@ check-pass\n// A frontmatter infostring can have leading whitespace.\n\nfn main() {}\n"
452+
453+
"#]],
442454
);
443455
}
444456

@@ -541,8 +553,8 @@ fn main() {}
541553
str![[r#"
542554
shebang: None
543555
info: None
544-
frontmatter: "---\n\n"
545-
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"
556+
frontmatter: ""
557+
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"
546558
547559
"#]],
548560
);

0 commit comments

Comments
 (0)