From 25b62c2ac3a0e473f559a4f6ed01f0444f11b1da Mon Sep 17 00:00:00 2001 From: WindSoilder Date: Sun, 28 Jan 2024 23:01:19 +0800 Subject: [PATCH] fix force rm: should suppress error if directory is not found (#11656) # Description Fix a breaking change which is introduced by #11621 `rm -f /tmp/aaa` shouldn't return error if `/tmp/aaa/` doesn't exist. # User-Facing Changes NaN # Tests + Formatting Done --- crates/nu-command/src/filesystem/rm.rs | 13 +++++-------- crates/nu-command/tests/commands/rm.rs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index d44f278762..b601ac988c 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -254,7 +254,6 @@ fn rm( }); } - // let path = currentdir_path.join(target.item.as_ref()); match nu_engine::glob_from( &target, ¤tdir_path, @@ -302,13 +301,11 @@ fn rm( } } Err(e) => { - return Err(ShellError::GenericError { - error: e.to_string(), - msg: e.to_string(), - span: Some(target.span), - help: None, - inner: vec![], - }) + // glob_from may canonicalize path and return `DirectoryNotFound` + // nushell should suppress the error if `--force` is used. + if !(force && matches!(e, ShellError::DirectoryNotFound { .. })) { + return Err(e); + } } }; } diff --git a/crates/nu-command/tests/commands/rm.rs b/crates/nu-command/tests/commands/rm.rs index 9d7da4fc03..1cae6e29b1 100644 --- a/crates/nu-command/tests/commands/rm.rs +++ b/crates/nu-command/tests/commands/rm.rs @@ -480,3 +480,18 @@ fn rm_files_inside_glob_metachars_dir() { )); }); } + +#[test] +fn force_rm_suppress_error() { + Playground::setup("force_rm_suppress_error", |dirs, sandbox| { + sandbox.with_files(vec![EmptyFile("test_file.txt")]); + + // the second rm should suppress error. + let actual = nu!( + cwd: dirs.test(), + "rm test_file.txt; rm -f test_file.txt", + ); + + assert!(actual.err.is_empty()); + }); +}