From 82836b0da5e00aa69e11a1ebf64d7224b0bcd225 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 7 Nov 2024 14:34:26 -0600 Subject: [PATCH] fix(toml): Strip blank linkes before frontmatter From the [RFC](https://rust-lang.github.io/rfcs/3503-frontmatter.html) > When parsing Rust source, after stripping the shebang (#!), rustc will strip the frontmatter: > > - May include 0+ blank lines (whitespace + newline) The question is what is a non-newline whitespace and what is a newline. Looking at the [Reference](https://doc.rust-lang.org/reference/whitespace.html), the answer is "unsure", particularly because the Rust language doesn't generally try to distinguish them. I kept things basic for now and we can revisit later. --- src/cargo/util/toml/embedded.rs | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/cargo/util/toml/embedded.rs b/src/cargo/util/toml/embedded.rs index d25dcbfe52a..6f569b94cf0 100644 --- a/src/cargo/util/toml/embedded.rs +++ b/src/cargo/util/toml/embedded.rs @@ -228,8 +228,17 @@ fn split_source(input: &str) -> CargoResult> { const FENCE_CHAR: char = '-'; - let fence_end = source - .content + let mut trimmed_content = source.content; + while !trimmed_content.is_empty() { + let c = trimmed_content; + let c = c.trim_start_matches([' ', '\t']); + let c = c.trim_start_matches(['\r', '\n']); + if c == trimmed_content { + break; + } + trimmed_content = c; + } + let fence_end = trimmed_content .char_indices() .find_map(|(i, c)| (c != FENCE_CHAR).then_some(i)) .unwrap_or(source.content.len()); @@ -242,7 +251,7 @@ fn split_source(input: &str) -> CargoResult> { "found {fence_end} `{FENCE_CHAR}` in rust frontmatter, expected at least 3" ) } - _ => source.content.split_at(fence_end), + _ => trimmed_content.split_at(fence_end), }; let (info, content) = rest.split_once("\n").unwrap_or((rest, "")); let info = info.trim(); @@ -438,8 +447,8 @@ fn main() {} str![[r##" shebang: "#!/usr/bin/env cargo\n" info: None -frontmatter: None -content: " \n\n\n---\n[dependencies]\ntime=\"0.1.25\"\n---\n\n\nfn main() {}\n" +frontmatter: "[dependencies]\ntime=\"0.1.25\"\n" +content: "\n\nfn main() {}\n" "##]], );