nushell / nushell

A new type of shell
https://www.nushell.sh/
MIT License
32.02k stars 1.65k forks source link

`rm -r .*` will remove parent folder #13494

Open steelywing opened 2 months ago

steelywing commented 2 months ago

Describe the bug

Warning: this will remove the parent folder, create an extra sub folder before test this bug

rm -r .* will remove parent folder, I lost some of my file due to this 😢

How to reproduce

Warning: this will remove the parent folder, create an extra sub folder before test this bug

echo | save file
mkdir test
cd test
rm -r .*

nu will try to delete the parent folder, I think nu try to delete . and .., but rm .* should only remove files that start . in current directory

Expected behavior

Don't delete parent folder

Screenshots

No response

Configuration

key value
version 0.96.1
major 0
minor 96
patch 1
branch
commit_hash f7d6c28a001f94f224e459e21df0da2ab5bdc923
build_os windows-x86_64
build_target x86_64-pc-windows-msvc
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09)
rust_channel 1.77.2-x86_64-pc-windows-msvc
cargo_version cargo 1.77.2 (e52e36006 2024-03-26)
build_time 2024-07-29 23:43:54 +00:00
build_rust_channel release
allocator mimalloc
features default, sqlite, system-clipboard, trash
installed_plugins

Additional context

No response

NotTheDr01ds commented 2 months ago

Exists on 0.95 as well. Looks to be OS-specific (Windows). Can't reproduce on Linux, but I can (partially) on Windows.

However, using the example above, I also can't reproduce the actual deletion, but only the attempted deletion. However, it seems that it would be guaranteed to fail if run from the current directory:

↪ rm -rf .*
Error: nu::shell::remove_not_possible

  × Remove not possible
   ╭─[entry #23:1:8]
 1 │ rm -rf .*
   ·        ─┬
   ·         ╰── Could not delete C:\temp\nu-test: The process cannot access the file because it is being used by another process. (os error 32)
   ╰────

Error: nu::shell::remove_not_possible

  × Remove not possible
   ╭─[entry #23:1:8]
 1 │ rm -rf .*
   ·        ─┬
   ·         ╰── Could not delete C:\temp\nu-test\rm-test: The process cannot access the file because it is being used by another process. (os error 32)
   ╰────

That said, if you run the rm -r <path_to>/.* from a parent directory it will delete successfully.

Worse, a rm -rf <path_to>/<subdir>/.* will recursively delete multiple-levels-upwards. This could be catastrophic.

NotTheDr01ds commented 2 months ago

Moving the final line of my previous comment out to a separate message, because it's important.

Running rm -rf <path_to>/<subdir>/.* will recursively delete multiple-levels-upwards. This could be catastrophic.