nushell icon indicating copy to clipboard operation
nushell copied to clipboard

Bare word interpolation is DoNotExpand

Open 132ikl opened this issue 7 months ago • 1 comments

Describe the bug

With #15735, bare word interpolation works correctly, however it seems like you can't use it to create globs:

ls ($nu.home-path)/test.txt
# => ╭───┬─────────────────────┬──────┬───────┬───────────────╮
# => │ # │        name         │ type │ size  │   modified    │
# => ├───┼─────────────────────┼──────┼───────┼───────────────┤
# => │ 0 │ /home/rose/test.txt │ file │ 154 B │ 10 months ago │
# => ╰───┴─────────────────────┴──────┴───────┴───────────────╯

ls ($nu.home-path)/*.txt
# => Error:   × No matches found for DoNotExpand("/home/rose/*.txt")
# =>   ╭─[entry #8:1:4]
# => 1 │ ls ($nu.home-path)/*.txt
# =>   ·    ──────────┬──────────
# =>   ·              ╰── Pattern, file or folder not found
# =>   ╰────
# =>  help: no matches found

(cc @Bahex)

How to reproduce

ls ($nu.home-path)/*

Expected behavior

I expected the argument to ls ($nu.home-path)/*.txt to be parsed a glob which would expand, in the same way that ls *.txt would.

Configuration

key value
version 0.104.2
major 0
minor 104
patch 2
branch makepkg
commit_hash 9a968c4bdddcf22bb83332d9b074baad88604418
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.85.1 (4eb161250 2025-03-15)
rust_channel 1.85.1-x86_64-unknown-linux-gnu
cargo_version cargo 1.85.1 (d73d2caf9 2024-12-31)
build_time 2025-05-27 08:00:55 +00:00
build_rust_channel release
allocator standard
features default, sqlite, trash
installed_plugins custom_values 0.1.0, example 0.104.2, explore_ir 0.6.0, formats 0.104.2, gstat 0.104.2, inc 0.104.2, polars 0.101.1, query 0.104.2, stress_internals 0.104.2

132ikl avatar May 27 '25 14:05 132ikl

We do have an Expr::GlobInterpolation variant, and it actually works... in the specific circumstances where the

  • command is an external
  • the bare word interpolation in question doesn't start with a parenthesized sub-expression
  • sub-expressions don't have quotes in them because that will cause a parsing problem... (which does not happen when the command is internal...)

So the contrived example of it working!!

let o = "o"
^echo D($o)*
# => Documents Downloads

But Expr::GlobInterpolation is only constructed for external command arguments: https://github.com/nushell/nushell/blob/ae51f6d722fa098e17d32c056c8669c3cce65720/crates/nu-parser/src/parser.rs#L246-L246 https://github.com/nushell/nushell/blob/ae51f6d722fa098e17d32c056c8669c3cce65720/crates/nu-parser/src/parser.rs#L415-L431

And parsing globs (when the command is internal and the argument's syntax shape is glob) uses a completely separate path than string parsing: https://github.com/nushell/nushell/blob/ae51f6d722fa098e17d32c056c8669c3cce65720/crates/nu-parser/src/parser.rs#L2957-L2977

Hopefully it won't be too hard to consolidate these a little more

Bahex avatar May 27 '25 21:05 Bahex

If this is being worked on anyway, I would like to emphasise, that the following should work, as well. It's an important feature missing, as you cannot dynamically set a path through value and then search it recursively, in native Nu, without changing directories.

ls ($nu.home-path)/**/*

theAkito avatar Jul 20 '25 22:07 theAkito