From 7336e1df1aeb6dbb15dd4b986c2358decccba3ec Mon Sep 17 00:00:00 2001 From: nibon7 Date: Thu, 20 Oct 2022 16:00:25 +0800 Subject: [PATCH] rm: fix error span when targets doesn't exists (#6815) Closes #6810 --- crates/nu-command/src/filesystem/rm.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index c3533e7997..d56bd50cd9 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -202,6 +202,7 @@ fn rm( let path = current_dir(engine_state, stack)?; + let (mut target_exists, mut empty_span) = (false, call.head); let mut all_targets: HashMap = HashMap::new(); for target in targets { if path.to_string_lossy() == target.item @@ -230,6 +231,8 @@ fn rm( ) { Ok(files) => { for file in files { + target_exists = true; + match file { Ok(ref f) => { // 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) => { return Err(ShellError::GenericError( e.to_string(), e.to_string(), - Some(call.head), + Some(target.span), None, Vec::new(), )) @@ -270,7 +278,7 @@ fn rm( return Err(ShellError::GenericError( "No valid paths".into(), "no valid paths".into(), - Some(call.head), + Some(empty_span), None, Vec::new(), ));