Skip to content

ls glob and glob glob behave differently. #7579

Closed

Description

@NfNitLoop

Describe the bug

The docs for ls say its pattern is:

pattern: the glob pattern to use

But its behavior is different than that of glob. ls with a glob pattern fails to find matching files, but glob with the same pattern finds them.

How to reproduce

mkdir a/b
touch a/b/foo.java
touch a/b/foo.class

mkdir a/b/c/d/e/f/g/h
touch a/b/c/d/e/f/g/h/bar.java
touch a/b/c/d/e/f/g/h/bar.class

# Finds all the files:
ls **/* | where type == file

# ls with glob does not find the files:
ls **/*.{class,java}

# But glob does:
glob **/*.{class,java}

Expected behavior

The thing documented as a glob should act as a glob.

Alternatively: if they are not equivalent, document the differences.

Screenshots

Screenshot 2022-12-22 at 5 11 43 PM

Or, in text:

/Users/codyc/test/nushell/glob-bug〉ls **/* | where type == file                                               12/22/2022 05:10:50 PM
╭───┬───────────────────────────┬──────┬──────┬───────────────╮
│ # │           name            │ type │ size │   modified    │
├───┼───────────────────────────┼──────┼──────┼───────────────┤
│ 0 │ a/b/c/d/e/f/g/h/bar.class │ file │  0 B │ 2 minutes ago │
│ 1 │ a/b/c/d/e/f/g/h/bar.java  │ file │  0 B │ 2 minutes ago │
│ 2 │ a/b/foo.class             │ file │  0 B │ 3 minutes ago │
│ 3 │ a/b/foo.java              │ file │  0 B │ 3 minutes ago │
╰───┴───────────────────────────┴──────┴──────┴───────────────╯
/Users/codyc/test/nushell/glob-bug〉ls **/*.{class,java}                                                       12/22/2022 05:10:54 PM
Error: 
  × No matches found for **/*.{class,java}
   ╭─[entry #36:1:1]
 1 │ ls **/*.{class,java}
   ·    ────────┬────────
   ·            ╰── Pattern, file or folder not found
   ╰────
  help: no matches found


/Users/codyc/test/nushell/glob-bug〉glob **/*.{class,java}                                                     12/22/2022 05:11:12 PM
╭───┬──────────────────────────────────────────────────────────────╮
│ 0 │ /Users/codyc/test/nushell/glob-bug/a/b/foo.java              │
│ 1 │ /Users/codyc/test/nushell/glob-bug/a/b/foo.class             │
│ 2 │ /Users/codyc/test/nushell/glob-bug/a/b/c/d/e/f/g/h/bar.java  │
│ 3 │ /Users/codyc/test/nushell/glob-bug/a/b/c/d/e/f/g/h/bar.class │
╰───┴──────────────────────────────────────────────────────────────╯

Configuration

key to
version 0.73.0
branch
commit_hash
build_os macos-x86_64
build_target x86_64-apple-darwin
rust_version rustc 1.65.0
cargo_version cargo 1.65.0 (4bc8f24d3 2022-10-20)
pkg_version 0.73.0
build_time 2022-12-20 08:58:48 +00:00
build_rust_channel release
features database, default, trash, which, zip
installed_plugins

Additional context

Was very curious where my files went! 😆

Activity

  1. fdncred commented on Dec 23, 2022

    @fdncred
    Contributor

    They use different globbing crates. ls uses nu-glob and glob uses wax. I wrote glob to use wax because nu-glob had to be changed to support things in ls better. I think glob uses the more tradition pattern matching.

  2. rgwood commented on Dec 23, 2022

    @rgwood
    Contributor

    This has come up in the context of watch too. We should standardize on a single glob implementation eventually, just need to find a contributor with time on their hands :)

  3. added
    help wanted
    category:inconsistent-behavior
    A:file-system
    on Dec 23, 2022
  4. fdncred commented on Dec 23, 2022

    @fdncred
    Contributor

    I'd love to standardize on wax and discontinue nu-glob but I can't really remember the exact reasons why we had to change it. I wouldn't want to remove it just to have the same problems with ls again.

  5. added
    A:glob
    on Jul 9, 2023
  6. skyvine commented on Sep 24, 2023

    @skyvine

    Hello,

    I see that this issue has the "help wanted" tag and there's an open question about the reason for nu-glob's existence.

    The first commit touching the nu-glob directory references #4818 which states that the reason is to "remove the [] syntax that is causing problems with users expecting more traditional globs". I eyeball-compared the crates/nu-glob/src/lib.rs from that PR (3ea0e66) with the upstream one (https://docs.rs/crate/glob/latest/source/src/lib.rs), and the match in impl Pattern { fn new(...) { ... } } is missing the [ case which exists on upstream. Perhaps there were other changes, but this seems to satisfy the reason stated in the pull request. I'm not sure if the upstream glob has been updated since the fork. The PR is referenced by #4811, which was created a day before the pull request, so I am assuming that this is the only issue that caused this change in the first place. If @jntrnr is still active on this project perhaps they remember additional context, although this did happen a year and a half ago.

    I am interested in working on this issue. Additionally, I believe it is a blocker for #10364 for reasons explained in my comment on that issue. With the context of the above paragraph, I think it is safe to proceed with this change: the problem that caused the creation of nu_glob was the inability to use square braces literally, and Wax supports backslash escapes (nu_glob supports escaping by surrounding special characters with square braces, but this is rather esoteric ).

    However, I am not sure if the above comment about standardizing on Wax was an offhand remark or if the project is certain that Wax is the library to standardize on. In either case, I do think that it would be useful to maintain a nu_glob crate as an interface to globbing for the rest of the project. This will be helpful if the backend needs to change in the future, and if embedding is a use-case that the project cares about then it may be useful to embedders, as their application might use a different globbing engine and they would want a unified experience for their users. This approach will require to changes in the glob command, but based on its user-visible behavior (take in a glob, return the list of matches) I do not expect this to be a significant source of problems.

    I have spent a couple of days looking at the nushell source code to assess whether or not I had the capacity to make these kinds of changes (I was mostly focused on #10364, because I originally believed that was a blocker for this issue). I am confident that I can make this change, however I would appreciate feedback from a maintainer before I put more time into nushell.

    Thanks,
    Sklyer

  7. fdncred commented on Sep 25, 2023

    @fdncred
    Contributor

    @skyvine I tried to sync nu-glob and glob up within the past few months, but may have missed some areas or they've added more/different functionality/bug fixes. I'd be fine with removing nu-glob entirely if glob works better now. I added the [] back in to see if someone could fix the problem. My reasoning was, if we continually hide the issue, it'll never be fixed and having the globbing [] syntax is important. If there's some simple change to nu-glob that would fix these problems, we'd accept the PR.

    Related to wax, I added it with the glob command to try and have better globbing (multiple inputs (although not supported yet), multiple nots, a little different syntax). At the time, it seemed to be the best globbing crate. Since then, it doesn't seem to be supported very well. I'd be supportive of unifying all our globbing on some well supported crate, assuming we don't lose a ton of functionality or performance. nu-glob/glob is pretty fast. I'd also be willing to entertain using jwalk for ls since it will stream sorted and is able to use multiple threads. I've written 4 different branches to make ls better with different crates like jwalk and never finished any of them to my liking.

  8. skyvine commented on Sep 26, 2023

    @skyvine

    Regarding curly brace behavior, one specific example of the inconsistency problem, Wax adds the curly braces as an unusual extension to what has historically been referred to as globbing. Curly brace expansion is a separate feature in traditional shells like Bash (brace expansion, filename expansion aka globbing) and Zsh (brace expansion, filename expansion aka globbing). Personally, I don't see any reason why nushell needs to conform to this specific precedent, although I'm also new to the project.

    One thing that I am thinking about is the silver searcher. It is typically a grep replacement, but with the -g flag it matches filenames (including the path, relative to the input) instead of contents. This means that the same regex syntax can be applied to both use-cases (file content searching and filename searching) which I personally find to be more ergonomic than using different pattern-matching syntaxes in different contexts. It would be possible to implement this with jwalk. Rust's built-in regex crate intentionally excludes some features in order to minimize the worst-case complexity which sounds like a tradeoff that the project would favor. An initial implementation would be straightforward in theory, as jwalk's process_read_dir lets us filter based on the path, so we can just walk everything and check the regex.

    However, the above approach would be very surprising to users coming from traditional shells because regexes are not path-aware (for example, .* would match the path separator). It might be possible to process the regexes in a path-aware way, but I'm suspicious that this would add an excessive amount of complexity both in terms of readability and runtime performance. A more conservative approach would be to add curly-brace expansion to nu_glob. It is possible that there will still be some inconsistencies compared to performing curly-brace expansion and filename expansion in separate phases, but it is a less significant departure compared to abandoning globs all together in favor of regexes.

  9. fdncred commented on Sep 27, 2023

    @fdncred
    Contributor

    I'm not super worried about brace expansion because we have str expand that does that. I'm more concerned about files or folders with [] in their names making file operations puke.

    One problem with the current implementation is that ls is slow. You can really see that when using network drives or going from WSL to Windows drives. Having a multithreaded ability is helpful in these situations. I have a closed PR here somewhere with the one I wrote but the problem was that it was non-deterministic in it's sort, which could turn off users and after I made it sort, i resorted to collecting which broke the streaming rules. However, the test made it very clear that multithreaded behavior was superior as far as speed is concerned.

  10. skyvine commented on Sep 27, 2023

    @skyvine

    Ok, then I think that the issue described in this report is working as intended. Please let me know if anything below is inaccurate:

    It is expected that the glob and ls commands return different results. glob was added a an experimental ls alternative, so it would not make sense for it to behave identically. Unfortunately, the main dependency of the glob command is no longer maintained, so it will never be used for commands which take globs as inputs.

    Technically, I suppose the glob command should be deprecated/removed due to the above.

  11. fdncred commented on Sep 27, 2023

    @fdncred
    Contributor

    to be clear, there's a glob crate, nu-glob crate, and glob command. the glob command was never intended to be a ls replacement/alternative. it was to fill a gap that ls couldn't provide, which was to do better globbing. we forked the glob crate in the pr you found to make nu-glob but then restricted the globbing capabilities. i really want to have our cake and eat it too.

    I do agree with your point that the glob command and ls globbing was never supposed to work identically. that was never the intent. quite the opposite. So, yes, the two crates are working independently as intended. However, I definitely want to have better globbing support in ls, without confusion. I'm not sure what form that will take, but that's the goal. If we can get proper globbing in ls, we may end-of-life the glob command.

  12. ErichDonGubler commented on Sep 28, 2023

    @ErichDonGubler
    Contributor

    ICYMI: The maintainer of wax has expressed intent to continue maintaining after a lull and ping in issues: olson-sean-k/wax#45 (comment)

  13. fdncred commented on Sep 28, 2023

    @fdncred
    Contributor

    I hope they do keep maintaining it. It's a nice crate.

  14. c-git commented on Feb 17, 2026

    @c-git

    Bumped into this while going through the Getting started

    Wasn't sure if I should mention it but it brought me here as I was quite surprised so sharing in case it makes it easier for the next person to find. I kinda had to look through a lot of issues to find this one. It's a bit surprising when you bump into unexpected results in the getting started.

    Image

    As text:

    ~/tmp> mkdir a
    ~/tmp> cd a
    ~/tmp/a> touch a2026-02-17_file
    ~/tmp/a> touch 2026-02-17_file
    ~/tmp/a> # Find files whose name includes the current month in the form YYYY-mm
    let current_month = (date now | format date '%Y-%m')
    let glob_pattern = ($"*($current_month)*" | into glob)
    ls $glob_pattern
    ╭───┬──────────────────┬──────┬──────┬──────────────╮
    │ # │       name       │ type │ size │   modified   │
    ├───┼──────────────────┼──────┼──────┼──────────────┤
    │ 0 │ 2026-02-17_file  │ file │  0 B │ a minute ago │
    │ 1 │ a2026-02-17_file │ file │  0 B │ a minute ago │
    ╰───┴──────────────────┴──────┴──────┴──────────────╯
    ~/tmp/a> # Find files whose name includes the current month in the form YYYY-mm
    let current_month = (date now | format date '%Y-%m')
    ls ...(glob $"*($current_month)*")
    ╭───┬──────────────────────────────────┬──────┬──────┬──────────────╮
    │ # │               name               │ type │ size │   modified   │
    ├───┼──────────────────────────────────┼──────┼──────┼──────────────┤
    │ 0 │ /home/one/tmp/a/2026-02-17_file  │ file │  0 B │ a minute ago │
    │ 1 │ /home/one/tmp/a/a2026-02-17_file │ file │  0 B │ a minute ago │
    ╰───┴──────────────────────────────────┴──────┴──────┴──────────────╯
    ~/tmp/a> # Find files whose name includes the current month in the form YYYY-mm
    let current_month = (date now | format date '%Y-%m')
    let glob_pattern: glob = ($"*($current_month)*")
    ls $glob_pattern
    Error: nu::shell::error
    
      × No matches found for DoNotExpand("*2026-02*")
       ╭─[entry #84:4:4]
     3 │ let glob_pattern: glob = ($"*($current_month)*")
     4 │ ls $glob_pattern
       ·    ──────┬──────
       ·          ╰── Pattern, file or folder not found
       ╰────
      help: no matches found
  15. fdncred commented on Feb 17, 2026

    @fdncred
    Contributor

    I'm wondering if there's a typo that I'm missing or something because it all works for me. Maybe I'm missing the whole point?

    ❯ touch a2026-02-17_file
    ❯ touch 2026-02-17_file
    ❯ let current_month = date now | format date '%Y-%m'
    ❯ $current_month
    2026-02
    ❯ let glob_pattern = $"*($current_month)*" | into glob
    ❯ $glob_pattern
    *2026-02*
    ❯ ls $glob_pattern
    ╭─#─┬───────name───────┬─type─┬─size─┬───modified───╮
    │ 0 │ 2026-02-17_file  │ file │    0 │ a minute ago │
    │ 1 │ a2026-02-17_file │ file │    0 │ a minute ago │
    ╰─#─┴───────name───────┴─type─┴─size─┴───modified───╯
    ❯ glob $glob_pattern
    ╭───┬────────────────────────────────────────────────────────────╮
    │ 0 │ C:\Users\username\source\repos\forks\test\2026-02-17_file  │
    │ 1 │ C:\Users\username\source\repos\forks\test\a2026-02-17_file │
    ╰───┴────────────────────────────────────────────────────────────╯
    ❯ ls ...(glob $glob_pattern)
    ╭─#─┬────────────────────────────name────────────────────────────┬─type─┬─size─┬───modified────╮
    │ 0 │ C:\Users\username\source\repos\forks\test\2026-02-17_file  │ file │    0 │ 2 minutes ago │
    │ 1 │ C:\Users\username\source\repos\forks\test\a2026-02-17_file │ file │    0 │ 2 minutes ago │
    ╰─#─┴────────────────────────────name────────────────────────────┴─type─┴─size─┴───modified────╯
  16. c-git commented on Feb 17, 2026

    @c-git

    I'm wondering if there's a typo that I'm missing or something because it all works for me. Maybe I'm missing the whole point?

    No I think you just missed 3. Force glob type via annotation: from the documentation

    # Find files whose name includes the current month in the form YYYY-mm
    let current_month = (date now | format date '%Y-%m')
    let glob_pattern: glob = ($"*($current_month)*")
    ls $glob_pattern

    The ones you tried also work for me. The 3rd one gives me an error

    ~/tmp/a> let current_month = (date now | format date '%Y-%m')
    ~/tmp/a> $current_month
    2026-02
    ~/tmp/a> let glob_pattern: glob = ($"*($current_month)*")
    ~/tmp/a> $glob_pattern
    *2026-02*
    ~/tmp/a> ls $glob_pattern
    Error: nu::shell::error
    
      × No matches found for DoNotExpand("*2026-02*")
       ╭─[entry #133:1:4]
     1 │ ls $glob_pattern
       ·    ──────┬──────
       ·          ╰── Pattern, file or folder not found
       ╰────
      help: no matches found
  17. fdncred commented on Feb 17, 2026

    @fdncred
    Contributor

    oh, i see. it's because quotes fall through to the DoNotExpand part. It's like doing ls * vs ls "*". Seems like a bug, but of course, the work-around is to just do | into glob.

    The key is ls is trying to know when you're literally looking for * and when it's a glob but for some reason, it doesn't see that let gp: glob = some_glob is really a glob even though describe thinks it is.

  18. c-git commented on Feb 18, 2026

    @c-git

    Maybe "hide" / "temporarily remove" the 3rd way from the docs until it works as described? I think 2 ways is plenty enough without sending the reader into the weeds.

  19. fdncred commented on Feb 18, 2026

    @fdncred
    Contributor

    hopefully this #17596 fixes the problem

  20. added this to the v0.111.0 milestone on Feb 21, 2026
  21. c-git commented on Feb 25, 2026

    @c-git

    hopefully this #17596 fixes the problem

    Should I install from the main branch to test for you? And if yes is it as simple as cargo install --git https://github.com/nushell/nushell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A:file-systemA:globcategory:inconsistent-behaviorhelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions