From 1865ef683424431191faac28c7032ad17264c900 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 11 Sep 2025 15:59:11 -0500 Subject: [PATCH] fix(frontmatter): Try alternative len code fences Of the non-ideal error cases mentioned in #15939, this is likely the one people will hit the most and so important for us to improve. --- src/cargo/util/frontmatter.rs | 13 +++++++++++++ .../script/rustc_fixtures/mismatch-2.stderr | 8 ++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/cargo/util/frontmatter.rs b/src/cargo/util/frontmatter.rs index 41a77d57638..009fd3726b4 100644 --- a/src/cargo/util/frontmatter.rs +++ b/src/cargo/util/frontmatter.rs @@ -95,6 +95,19 @@ impl<'s> ScriptSource<'s> { // Ends with a line that starts with a matching number of `-` only followed by whitespace let nl_fence_pattern = format!("\n{fence_pattern}"); let Some(frontmatter_nl) = input.find_slice(nl_fence_pattern.as_str()) else { + for len in (2..(nl_fence_pattern.len() - 1)).rev() { + let Some(frontmatter_nl) = input.find_slice(&nl_fence_pattern[0..len]) else { + continue; + }; + let _ = input.next_slice(frontmatter_nl.start + 1); + let close_start = input.current_token_start(); + let _ = input.next_slice(len); + let close_end = input.current_token_start(); + return Err(FrontmatterError::new( + format!("closing code fence has too few `-`"), + close_start..close_end, + )); + } return Err(FrontmatterError::new( format!("no closing `{fence_pattern}` found for frontmatter"), open_start..open_end, diff --git a/tests/testsuite/script/rustc_fixtures/mismatch-2.stderr b/tests/testsuite/script/rustc_fixtures/mismatch-2.stderr index 31743531ccf..48667df6a70 100644 --- a/tests/testsuite/script/rustc_fixtures/mismatch-2.stderr +++ b/tests/testsuite/script/rustc_fixtures/mismatch-2.stderr @@ -1,5 +1,5 @@ -[ERROR] no closing `----` found for frontmatter - --> script:1:1 +[ERROR] closing code fence has too few `-` + --> script:3:1 | -1 | ----cargo - | ^^^^ +3 | ---cargo + | ^^^