Skip to content

rm is buggy when path contains globs #10211

@horasal

Description

@horasal

Describe the bug

Related: #9310

rm directly passes current_directory to nu_glob without escaping, so the path itself is also expanded as globs.

For example, run rm a under /tmp/* will also delete /tmp/b/a, /tmp/whatever/a:

❯ ls **/*
╭────┬──────────┬──────┬───────┬────────────────╮
│  # │   name   │ type │ size  │    modified    │
├────┼──────────┼──────┼───────┼────────────────┤
│  0 │ *        │ dir  │ 192 B │ a minute ago   │
│  1 │ */a      │ file │   0 B │ a minute ago   │
│  2 │ */b      │ file │   0 B │ a minute ago   │
│  3 │ */c      │ file │   0 B │ a minute ago   │
│  4 │ */d      │ file │   0 B │ a minute ago   │
│  5 │ [test]   │ dir  │ 192 B │ 30 seconds ago │
│  6 │ [test]/a │ file │   0 B │ 30 seconds ago │
│  7 │ [test]/b │ file │   0 B │ 30 seconds ago │
│  8 │ [test]/c │ file │   0 B │ 30 seconds ago │
│  9 │ [test]/d │ file │   0 B │ 30 seconds ago │
│ 10 │ a        │ file │   0 B │ 4 minutes ago  │
│ 11 │ b        │ file │   0 B │ 4 minutes ago  │
│ 12 │ c        │ file │   0 B │ 4 minutes ago  │
│ 13 │ d        │ file │   0 B │ 4 minutes ago  │
│ 14 │ e        │ file │   0 B │ 4 minutes ago  │
│ 15 │ f        │ file │   0 B │ 4 minutes ago  │
│ 16 │ g        │ file │   0 B │ 4 minutes ago  │
╰────┴──────────┴──────┴───────┴────────────────╯


❯ cd *; rm a; cd ..; ls **/*
╭────┬──────────┬──────┬───────┬───────────────╮
│  # │   name   │ type │ size  │   modified    │
├────┼──────────┼──────┼───────┼───────────────┤
│  0 │ *        │ dir  │ 160 B │ now           │
│  1 │ */b      │ file │   0 B │ a minute ago  │
│  2 │ */c      │ file │   0 B │ a minute ago  │
│  3 │ */d      │ file │   0 B │ a minute ago  │
│  4 │ [test]   │ dir  │ 160 B │ now           │
│  5 │ [test]/b │ file │   0 B │ a minute ago  │
│  6 │ [test]/c │ file │   0 B │ a minute ago  │
│  7 │ [test]/d │ file │   0 B │ a minute ago  │
│  8 │ a        │ file │   0 B │ 4 minutes ago │
│  9 │ b        │ file │   0 B │ 4 minutes ago │
│ 10 │ c        │ file │   0 B │ 4 minutes ago │
│ 11 │ d        │ file │   0 B │ 4 minutes ago │
│ 12 │ e        │ file │   0 B │ 4 minutes ago │
│ 13 │ f        │ file │   0 B │ 4 minutes ago │
│ 14 │ g        │ file │   0 B │ 4 minutes ago │
╰────┴──────────┴──────┴───────┴───────────────╯

How to reproduce

  1. mkdir * a b c d
  2. touch */a */b */c */d a/a b/a c/a d/a
  3. cd *; rm a

Expected behavior

Only delete */a

Screenshots

No response

Configuration

column0 column1
version 0.84.1
branch master
commit_hash e514535
build_os macos-aarch64
build_target aarch64-apple-darwin
rust_version rustc 1.70.0 (90c541806 2023-05-31)
rust_channel 1.70.0-aarch64-apple-darwin
cargo_version cargo 1.70.0 (ec8a8a0ca 2023-04-25)
build_time 2023-09-04 09:41:32 +09:00
build_rust_channel release
allocator standard
features dataframe, default, extra, sqlite, trash, wasi, which, zip
installed_plugins query, query json, query web, query xml

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:coreutils-uutilsChanges relating to coreutils/uutilsA:file-systemRelated to commands and core nushell behavior around the file systemA:globBehavior around file-system globbing with regular commands or `glob`. See also A:quoting/expansion

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions