Skip to content

Add --no-glob option for each command accepts glob patterns by default #10364

Closed

Description

@EmilyGraceSeville7cf

Related problem

Commands like mv interpret passed strings as glob patterns out of the box, and there is no way to explicitly disable this behavior AFAIK. Now it's problematic to perform some mv operation on a file containing characters can be interpreted as glob like square brackets.

Describe the solution you'd like

Introduce --no-glob option for all commands accept glob patterns by default. Short option name should be resolved case-by-case, but -n can be preferred by default.

Describe alternatives you've considered

No response

Additional context and details

Issue is created out of this topic.

Activity

  1. added
    category:enhancement
    status:needs-triage
    on Sep 14, 2023
  2. amtoine commented on Sep 14, 2023

    @amtoine
    Member

    this looks like a duplicate of #9310, right?

    will close in favor of #9310 for now 😌
    if it's more than #9310, feel free to reopen 😉

  3. fdncred commented on Sep 14, 2023

    @fdncred
    Contributor

    Seems like this is more of a feature request vs a bug report. If this feature gets implemented, it could close #9310 though.

  4. removed
    status:needs-triage
    on Sep 14, 2023
  5. skyvine commented on Sep 24, 2023

    @skyvine

    Hello,

    I am new to the nushell codebase but interested in contributing. I started looking at this issue because I originally assumed that it was a blocker for #7579, which has the "help wanted" tag, but after spending some time with it I think that the inverse is actually true: this issue is blocked by #7579 (in particular, standardizing on a single regex library, which is presumably Wax).

    I have looked at the code in ls.rs as a litmus test to check if this change is something that I have the capacity to manage. In principle I expected it to be a simple change - there is a place where ls calls a function to interpret the glob, so we should be able to just put that into a conditional that checks the relevant flag. However, the change is complicated by the logic used to list the contents of a directory. The code currently pushes * to the end of the path so that the glob function returns the contents instead of the directory itself. If we skip the glob function completely, then it will look for a file literally named *. It would be possible to resolve this by changing the values returned by the match pattern_arg from and add some complicated logic around the globbing, but it seems simpler to use Pattern::escape on the user's input when they request no globbing.

    I have looked at implementing this change in multiple ways. The current version can be found in my fork. It currently fails the lists_regular_files_in_special_folder test. This test exists in the code base, but I added some new cases to it to confirm that my change was correct, and it is one of these cases which failed. The relevant test code is:

    fn lists_regular_files_in_special_folder() {
        Playground::setup("ls_test_3", |dirs, sandbox| {
            sandbox
                .mkdir("[abcd]")
                .mkdir("[bbcd]")
                .mkdir("abcd]")
                .mkdir("abcd")
                .mkdir("abcd/*")
                .mkdir("abcd/?")
                .with_files(vec![EmptyFile("[abcd]/test.txt")])
                .with_files(vec![EmptyFile("abcd]/test.txt")])
                .with_files(vec![EmptyFile("abcd/*/test.txt")])
                .with_files(vec![EmptyFile("abcd/?/test.txt")])
                .with_files(vec![EmptyFile("abcd/?/test2.txt")]);
    ...
            let actual = nu!(
                cwd: dirs.test(), format!(r#"ls --glob true [bbcd] | length"#));
            assert!(actual.err.contains("no matches found"));
    

    It fails because nu successfully lists the (non-existent) contents of the [bbcd] directory. This is the behavior of nu on main, and I believe it is a bug as demonstrated by the following commands. (One of the commands need to be run from a separate shell)

    (nu) $ mkdir /tmp/sandbox
    (nu) $ cd /tmp/sandbox
    (nu) $ mkdir a b c d
    (nu) $ ls [abcd]
    ╭───┬──────┬──────┬─────────┬──────────╮
    │ # │ name │ type │  size   │ modified │
    ├───┼──────┼──────┼─────────┼──────────┤
    │ 0 │ a    │ dir  │ 4.0 KiB │ now      │
    │ 1 │ b    │ dir  │ 4.0 KiB │ now      │
    │ 2 │ c    │ dir  │ 4.0 KiB │ now      │
    │ 3 │ d    │ dir  │ 4.0 KiB │ now      │
    ╰───┴──────┴──────┴─────────┴──────────╯
    (bash) $ mkdir \[abcd] # in nu, `mkdir [abcd]` and `mkdir [[]abcd[]]` prints the error "expected non-[] value"
    (nu) $ ls [abcd]
    ╭────────────╮
    │ empty list │
    ╰────────────╯
    

    As ls is supposed to unconditionally interpret [abcd] as a glob pattern, the output should not change between the two invocations. The difference occurs because there is some special logic (for getting the contents & checking permissions) which operates on the literal input, regardless of whether or not the user supplied a glob pattern. Conceptually, it is correct to list the directory itself (not the contents) if a glob happens to match exactly 1 file which happens to be a directory. Therefore, this special logic should not run at all if the user input is a glob pattern. Which leads me to the reason why #7579 is a blocker: this change is dependent on the globbing library in use. In the case of ls, we need to execute different logic depending on whether or not the input is a glob pattern, regardless of what that pattern expands to. Hopefully all glob libraries have a public helper function to check if some string is a glob pattern making this change simple, but other commands might be dependent in more complex way. Performing this change before standardizing on a single regex engine is likely to result in wasted effort.

    On a somewhat unrelated note, the implementation in my branch defines a named parameter --glob instead of a --no-glob flag. I implemented it this way because I think it would be useful to make the default behavior configurable. At least one user has noted that there are environments where it is common for filenames to contain glob characters (in particular, #9310 (comment) notes that next.js uses square brackets in file names). Regardless of the default, the named argument can be used to force a particular interpretation for a particular invocation.

  6. fdncred commented on Sep 25, 2023

    @fdncred
    Contributor

    However, the change is complicated by the logic used to list the contents of a directory. The code currently pushes * to the end of the path so that the glob function ...

    This is one of the reasons why I've been looking at jwalk, walkdir, ignore, etc. Another problem is that glob syntax can be used in file naming. So, differentiating a literal [file].txt versus a glob of [file].txt is kind of hard to do automatically. I'd support having an opt-in solution where the user can escape globbing characters. I'd also support a complete rewrite of ls using one of the crates I mention above. I lean toward jwalk for it's streaming ability which we cannot lose.

    I'd be willing to try --glob but I tend to think users would object because the shell is supposed to "just know" what you mean somehow. The globbing issue isn't just related to ls, of course, it's literally every command that interfaces with the file system cp, ucp, mv, rm, touch, etc. It would be nice to have a holistic approach to fixing all this but I'm not sure what that is.

  7. added
    A:glob
    on Sep 25, 2023
  8. skyvine commented on Sep 27, 2023

    @skyvine

    I was looking at the glob-expansion tag and noticed the conversation in #10498. I see that the plan for the future is to use uutils, which presumably includes replacing the current version of ls with their version. I am not sure that it would be helpful make any changes to ls at this time.

  9. fdncred commented on Sep 27, 2023

    @fdncred
    Contributor

    ... see that the plan for the future is to use uutils, which presumably includes replacing the current version of ls with their version. I am not sure that it would be helpful make any changes to ls at this time.

    I disagree. It remains to be seen whether we'll use coreutils ls. It will take a major refactor on their part. So, ls is in the run category of commands in the crawl, walk, run spreadsheet of coreutils commands. It's labeled run because it will be exceedingly difficult (we're guessing). What makes it difficult is ls needs to stream data and the coreutils ls command does not do that. It needs to somehow iterate through files and folders, yielding output on the way, while still continuing.

    I'd love to use their code, I just don't think it will happen any time soon. It's a struggle enough right now to get the crawl commands working. Lots of internal changes on the coreutils side just to do that.

    So, with that said, I'd rather have an excellent multithreaded ls with excellent globbing capabilities, rather than waiting to see if coreutils uu_ls crate ever happens.

  10. skyvine commented on Sep 29, 2023

    @skyvine

    I'd be willing to try --glob but I tend to think users would object because the shell is supposed to "just know" what you mean somehow.

    I agree with the point that the shell should "just know" as much as possible. I still think that it is useful to have the --glob flag as an escape hatch, so that if a user runs into an unconsidered or ambiguous edge case they can force the behavior that they need.

    So, differentiating a literal [file].txt versus a glob of [file].txt is kind of hard to do automatically.

    What would you think about a PR that adds the traditional strategy of "if it doesn't match anything when interpreted as a glob pattern, check if it exists literally" to the nu_glob crate?

  11. 15 remaining items

  12. WindSoilder commented on Jan 25, 2024

    @WindSoilder
    Contributor

    Currently I'm trying to use quotes to disable glob.

    Once the #11621 is ok, you can quote argument to disable glob. Examples:

    ls a[bc]d   # apply glob
    
    mkdir ii
    cp "a[bc]d" e[fg]h ii  # don't apply glob for `a[bc]d`, apply glob for `e[fg]h`
    cp `My Documents/*`   # if a path contains whitespace, use backtick quote to apply glob
  13. bobhy commented on Jan 25, 2024

    @bobhy
    Contributor
  14. fdncred commented on Jan 25, 2024

    @fdncred
    Contributor

    I thought the only way to deal with paths with spaces in them was with backtick quotes. This works on Windows

    ls `c:/Program Files/*`
    

    I'm still a fan of something like quotes meaning take this string literal. Perhaps r"string literal"? Like my PR from last year #9956

  15. fdncred commented on Jan 25, 2024

    @fdncred
    Contributor

    The "..." spelliing to quote spaces is normative in Windows CMD and Powershell, fwiw.

    That's fine but nushell doesn't work this way unfortunately. only backtick quotes help with spaces in paths and filenames it seems.

  16. WindSoilder commented on Jan 26, 2024

    @WindSoilder
    Contributor

    Hi, @bobhy I'm sorry, I've update comment about the case to use backtick quote.
    I think I'm in fans of bash shell way, we need to have a way to glob or not glob inputs.


    And would you please to make code part of comment more readable? I think something is not right here.
    image

  17. bobhy commented on Jan 26, 2024

    @bobhy
    Contributor

    [reposted above comment: note to self: do not reply to github with code sections!]

    The "..." spelliing to quote spaces is normative in Windows CMD and
    Powershell, fwiw.

    There's already a quoting__an escaping__ scheme for globs, too. Just enclose a metachar
    in [], basically making it into a character class that matches only one
    character.

    I think it's working correctly:

    > touch a*b a?b a^b  # touch doesn't glob its args
    > ls
    ╭───┬──────┬──────┬──────┬──────────╮
    │ # │ name │ type │ size │ modified │
    ├───┼──────┼──────┼──────┼──────────┤
    │ 0 │ a*b  │ file │  0 B │ now      │
    │ 1 │ a?b  │ file │  0 B │ now      │
    │ 2 │ a^b  │ file │  0 B │ now      │
    ╰───┴──────┴──────┴──────┴──────────╯
    > cp a[*]b star  # single char class escapes the metachar
    > cp a[?]b ques
    > cp a[^]b upca
    > ls
    ╭───┬──────┬──────┬──────┬────────────────╮
    │ # │ name │ type │ size │    modified    │
    ├───┼──────┼──────┼──────┼────────────────┤
    │ 0 │ a*b  │ file │  0 B │ a minute ago   │
    │ 1 │ a?b  │ file │  0 B │ a minute ago   │
    │ 2 │ a^b  │ file │  0 B │ a minute ago   │
    │ 3 │ ques │ file │  0 B │ 18 seconds ago │
    │ 4 │ star │ file │  0 B │ 37 seconds ago │
    │ 5 │ upca │ file │  0 B │ now            │
    ╰───┴──────┴──────┴──────┴────────────────╯
    > rm a[?]b  # matched only a single file
    > ls
    ╭───┬──────┬──────┬──────┬────────────────╮
    │ # │ name │ type │ size │    modified    │
    ├───┼──────┼──────┼──────┼────────────────┤
    │ 0 │ a*b  │ file │  0 B │ a minute ago   │
    │ 1 │ a^b  │ file │  0 B │ a minute ago   │
    │ 2 │ ques │ file │  0 B │ 36 seconds ago │
    │ 3 │ star │ file │  0 B │ a minute ago   │
    │ 4 │ upca │ file │  0 B │ 20 seconds ago │
    ╰───┴──────┴──────┴──────┴────────────────╯
    > ls a[*]*  # ls also obeys this escaping rule
    ╭───┬──────┬──────┬──────┬───────────────╮
    │ # │ name │ type │ size │   modified    │
    ├───┼──────┼──────┼──────┼───────────────┤
    │ 0 │ a*b  │ file │  0 B │ 2 minutes ago │
    ╰───┴──────┴──────┴──────┴───────────────╯
    > ls a[*]
    Error:   × No matches found for a[*]
       ╭─[entry #89:1:1]
     1 │ ls a[*]
       ·    ──┬─
       ·      ╰── Pattern, file or folder not found
       ╰────
      help: no matches found
    

    Do we really need an alternative scheme?

  18. WindSoilder commented on Jan 26, 2024

    @WindSoilder
    Contributor

    There's already a quoting__an escaping__ scheme for globs, too. Just enclose a metacha in [], basically making it into a character class that matches only one character.

    Just personal thought. I have to say it feels strange to me...To me, follow same rules to other shells make it easier to use in this case.

  19. added this to the v0.90.0 milestone on Jan 26, 2024
  20. added a commit that references this issue on Jan 26, 2024
    d646903
  21. added a commit that references this issue on Feb 20, 2024
    549cee9
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:globcategory:enhancementstatus:needs-design

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions