mirror of
https://github.com/nushell/nushell.git
synced 2025-08-26 01:35:23 +02:00
fix(nu): script file not found error should point to commandline, not rust code (#16273)
This commit is contained in:
@@ -26,14 +26,23 @@ pub fn evaluate_file(
|
|||||||
) -> Result<(), ShellError> {
|
) -> Result<(), ShellError> {
|
||||||
let cwd = engine_state.cwd_as_string(Some(stack))?;
|
let cwd = engine_state.cwd_as_string(Some(stack))?;
|
||||||
|
|
||||||
let file_path = canonicalize_with(&path, cwd).map_err(|err| {
|
let file_path = {
|
||||||
IoError::new_internal_with_path(
|
match canonicalize_with(&path, cwd) {
|
||||||
err.not_found_as(NotFound::File),
|
Ok(t) => Ok(t),
|
||||||
"Could not access file",
|
Err(err) => {
|
||||||
nu_protocol::location!(),
|
let cmdline = format!("nu {path} {}", args.join(" "));
|
||||||
PathBuf::from(&path),
|
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
|
let file_path_str = file_path
|
||||||
.to_str()
|
.to_str()
|
||||||
|
@@ -445,3 +445,20 @@ fn main_script_subcommand_help_uses_script_name2() {
|
|||||||
assert!(!actual.err.contains("Usage: main foo"));
|
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"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user