fix(nu): script file not found error should point to commandline, not rust code (#16273)

This commit is contained in:
Bahex
2025-08-21 16:23:56 +03:00
committed by GitHub
parent 215b1ec2f1
commit f7c131443f
2 changed files with 34 additions and 8 deletions

View File

@@ -26,14 +26,23 @@ pub fn evaluate_file(
) -> Result<(), ShellError> {
let cwd = engine_state.cwd_as_string(Some(stack))?;
let file_path = canonicalize_with(&path, cwd).map_err(|err| {
IoError::new_internal_with_path(
err.not_found_as(NotFound::File),
"Could not access file",
nu_protocol::location!(),
PathBuf::from(&path),
)
})?;
let file_path = {
match canonicalize_with(&path, cwd) {
Ok(t) => Ok(t),
Err(err) => {
let cmdline = format!("nu {path} {}", args.join(" "));
let mut working_set = StateWorkingSet::new(engine_state);
let file_id = working_set.add_file("<commandline>".into(), cmdline.as_bytes());
let span = working_set
.get_span_for_file(file_id)
.subspan(3, path.len() + 3)
.expect("<commandline> to contain script path");
engine_state.merge_delta(working_set.render())?;
let e = IoError::new(err.not_found_as(NotFound::File), span, PathBuf::from(&path));
Err(e)
}
}
}?;
let file_path_str = file_path
.to_str()

View File

@@ -445,3 +445,20 @@ fn main_script_subcommand_help_uses_script_name2() {
assert!(!actual.err.contains("Usage: main foo"));
})
}
#[test]
fn script_file_not_found() {
let actual = nu!(r#"nu non-existent-script.nu foo bar"#);
assert!(
!actual.err.contains(".rs"),
"internal rust source was mentioned"
);
assert!(
actual.err.contains("non-existent-script.nu"),
"error did not include script name"
);
assert!(
actual.err.contains("commandline"),
"source file for the error was not commandline"
);
}