From 3966c0a9fded505e173cbf6cfe119a68fc8211db Mon Sep 17 00:00:00 2001 From: Ian Manske Date: Thu, 16 Nov 2023 23:30:15 +0000 Subject: [PATCH] Fix `rm` path handling (#11064) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Fixes issue #11061 where `rm` fails to find a file after a `cd`. It looks like the new glob functions do not return absolute file paths which we forgot to account for. # Tests Added a test (fails on current main, but passes with this PR). --------- Co-authored-by: Jakub Žádník --- crates/nu-command/src/filesystem/rm.rs | 6 ++++-- crates/nu-command/tests/commands/rm.rs | 15 +++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/filesystem/rm.rs b/crates/nu-command/src/filesystem/rm.rs index 7be1b9f00c..7b7fede149 100644 --- a/crates/nu-command/src/filesystem/rm.rs +++ b/crates/nu-command/src/filesystem/rm.rs @@ -250,7 +250,7 @@ fn rm( Ok(files) => { for file in files { match file { - Ok(ref f) => { + Ok(f) => { if !target_exists { target_exists = true; } @@ -263,7 +263,9 @@ fn rm( continue; } - all_targets.entry(f.clone()).or_insert_with(|| target.span); + all_targets + .entry(nu_path::expand_path_with(f, ¤tdir_path)) + .or_insert_with(|| target.span); } Err(e) => { return Err(ShellError::GenericError( diff --git a/crates/nu-command/tests/commands/rm.rs b/crates/nu-command/tests/commands/rm.rs index 6a0ce20128..90e697c313 100644 --- a/crates/nu-command/tests/commands/rm.rs +++ b/crates/nu-command/tests/commands/rm.rs @@ -375,6 +375,21 @@ fn removes_symlink() { }); } +#[test] +fn removes_file_after_cd() { + Playground::setup("rm_after_cd", |dirs, sandbox| { + sandbox.with_files(vec![EmptyFile("delete.txt")]); + + nu!( + cwd: dirs.root(), + "let file = 'delete.txt'; cd rm_after_cd; rm $file", + ); + + let path = dirs.test().join("delete.txt"); + assert!(!path.exists()); + }) +} + struct Cleanup<'a> { dir_to_clean: &'a Path, }