You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add --no-glob option for each command accepts glob patterns by default #10364
A:globBehavior around file-system globbing with regular commands or `glob`. See also A:quoting/expansioncategory:enhancementNew feature or requeststatus:needs-designthis feature requires design
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.
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:
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.
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.
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.
... 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.
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?
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
I'm sorry, but i think this would be a significant breaking change for
Windows users, where paths with spaces are the norm. eg, user *does* want
to glob this command:
```
cp "My Documents/*" backup/
```
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.
[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
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.
A:globBehavior around file-system globbing with regular commands or `glob`. See also A:quoting/expansioncategory:enhancementNew feature or requeststatus:needs-designthis feature requires design
Activity
amtoine commentedon Sep 14, 2023
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 😉
fdncred commentedon Sep 14, 2023
Seems like this is more of a feature request vs a bug report. If this feature gets implemented, it could close #9310 though.
Add `--glob` named argument to ls.
Add `--glob` named argument to ls.
skyvine commentedon Sep 24, 2023
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.rsas 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 wherelscalls 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 thematch pattern_argfrom and add some complicated logic around the globbing, but it seems simpler to usePattern::escapeon 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_foldertest. 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: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)As
lsis 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 ofls, 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
--globinstead of a--no-globflag. 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.fdncred commentedon Sep 25, 2023
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].txtversus a glob of[file].txtis 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 oflsusing one of the crates I mention above. I lean towardjwalkfor it's streaming ability which we cannot lose.I'd be willing to try
--globbut 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.skyvine commentedon Sep 27, 2023
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
lswith their version. I am not sure that it would be helpful make any changes tolsat this time.fdncred commentedon Sep 27, 2023
I disagree. It remains to be seen whether we'll use coreutils
ls. It will take a major refactor on their part. So,lsis in theruncategory of commands in thecrawl,walk,runspreadsheet of coreutils commands. It's labeledrunbecause it will be exceedingly difficult (we're guessing). What makes it difficult islsneeds to stream data and the coreutilslscommand 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
crawlcommands working. Lots of internal changes on the coreutils side just to do that.So, with that said, I'd rather have an excellent multithreaded
lswith excellent globbing capabilities, rather than waiting to see if coreutils uu_ls crate ever happens.skyvine commentedon Sep 29, 2023
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
--globflag 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.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_globcrate?15 remaining items
Allow filesystem commands to access files with glob metachars in name (…
Allow filesystem commands to access files with glob metachars in name (…
Allow filesystem commands to access files with glob metachars in name (…
WindSoilder commentedon Jan 25, 2024
Currently I'm trying to use quotes to disable glob.
Once the #11621 is ok, you can quote argument to disable glob. Examples:
bobhy commentedon Jan 25, 2024
fdncred commentedon Jan 25, 2024
I thought the only way to deal with paths with spaces in them was with backtick quotes. This works on Windows
I'm still a fan of something like quotes meaning take this string literal. Perhaps
r"string literal"? Like my PR from last year #9956fdncred commentedon Jan 25, 2024
That's fine but nushell doesn't work this way unfortunately. only backtick quotes help with spaces in paths and filenames it seems.
WindSoilder commentedon Jan 26, 2024
Hi, @bobhy I'm sorry, I've update comment about the case to use backtick quote.
I think I'm in fans of
bashshell 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.

bobhy commentedon Jan 26, 2024
[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 metacharin
[], basically making it into a character class that matches only onecharacter.
I think it's working correctly:
Do we really need an alternative scheme?
WindSoilder commentedon Jan 26, 2024
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.
Unify glob behavior on `open`, `rm`, `cp-old`, `mv`, `umv`, `cp` and …
Unify glob behavior on `open`, `rm`, `cp-old`, `mv`, `umv`, `cp` and …