rm: fix error span when targets doesn't exists (#6815)

Closes #6810
This commit is contained in:
nibon7 2022-10-20 16:00:25 +08:00 committed by GitHub
parent a724a8fe7d
commit 7336e1df1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -202,6 +202,7 @@ fn rm(
let path = current_dir(engine_state, stack)?; let path = current_dir(engine_state, stack)?;
let (mut target_exists, mut empty_span) = (false, call.head);
let mut all_targets: HashMap<PathBuf, Span> = HashMap::new(); let mut all_targets: HashMap<PathBuf, Span> = HashMap::new();
for target in targets { for target in targets {
if path.to_string_lossy() == target.item if path.to_string_lossy() == target.item
@ -230,6 +231,8 @@ fn rm(
) { ) {
Ok(files) => { Ok(files) => {
for file in files { for file in files {
target_exists = true;
match file { match file {
Ok(ref f) => { Ok(ref f) => {
// It is not appropriate to try and remove the // It is not appropriate to try and remove the
@ -253,12 +256,17 @@ fn rm(
} }
} }
} }
// Target doesn't exists
if !target_exists && empty_span.eq(&call.head) {
empty_span = target.span;
}
} }
Err(e) => { Err(e) => {
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
e.to_string(), e.to_string(),
e.to_string(), e.to_string(),
Some(call.head), Some(target.span),
None, None,
Vec::new(), Vec::new(),
)) ))
@ -270,7 +278,7 @@ fn rm(
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
"No valid paths".into(), "No valid paths".into(),
"no valid paths".into(), "no valid paths".into(),
Some(call.head), Some(empty_span),
None, None,
Vec::new(), Vec::new(),
)); ));