From fb7f6fc08b7c34dbfabaaadf19f7d199db40dc11 Mon Sep 17 00:00:00 2001 From: Andrej Kolchin Date: Thu, 8 Feb 2024 00:47:44 +0000 Subject: [PATCH] Fix a panic when parsing empty file (#11314) The previous implementation presumed that if files were given, they had contents. The change makes the fallback to permanent files uniform. Fix #11256 --- .../nu-protocol/src/engine/state_working_set.rs | 5 ++--- tests/shell/mod.rs | 15 +++++++++++++-- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/crates/nu-protocol/src/engine/state_working_set.rs b/crates/nu-protocol/src/engine/state_working_set.rs index f74293096e..4194585de7 100644 --- a/crates/nu-protocol/src/engine/state_working_set.rs +++ b/crates/nu-protocol/src/engine/state_working_set.rs @@ -355,11 +355,10 @@ impl<'a> StateWorkingSet<'a> { return &contents[begin..end]; } } - } else { - return self.permanent_state.get_span_contents(span); } - panic!("internal error: missing span contents in file cache") + // if no files with span were found, fall back on permanent ones + return self.permanent_state.get_span_contents(span); } pub fn enter_scope(&mut self) { diff --git a/tests/shell/mod.rs b/tests/shell/mod.rs index 596314d14c..5c0f367e46 100644 --- a/tests/shell/mod.rs +++ b/tests/shell/mod.rs @@ -297,7 +297,7 @@ fn main_script_can_have_subcommands1() { r#"def "main foo" [x: int] { print ($x + 100) } - + def "main" [] { print "usage: script.nu " }"#, @@ -318,7 +318,7 @@ fn main_script_can_have_subcommands2() { r#"def "main foo" [x: int] { print ($x + 100) } - + def "main" [] { print "usage: script.nu " }"#, @@ -329,3 +329,14 @@ fn main_script_can_have_subcommands2() { assert!(actual.out.contains("usage: script.nu")); }) } + +#[test] +fn source_empty_file() { + Playground::setup("source_empty_file", |dirs, sandbox| { + sandbox.mkdir("source_empty_file"); + sandbox.with_files(vec![FileWithContent("empty.nu", "")]); + + let actual = nu!(cwd: dirs.test(), pipeline("nu empty.nu")); + assert!(actual.out.is_empty()); + }) +}