Skip to content

watch's glob seems to work differently than ls | glob #7022

Closed as duplicate

Description

@colemickens

Describe the bug

ls | glob '<[!.]*/>[!.]*'

will recursively list files but ignore dot-files/dirs

but

▶ watch . -v --glob=<[!.]*/>[!.]* { |op, path, new_path| $"($op) ($path) ($new_path)"}
Absolute glob path: "/home/cole/code/nixcfg/<[!.]*/>[!.]*"
Now watching files at "/home/cole/code/nixcfg". Press ctrl+c to abort.
NoticeWrite("/home/cole/code/nixcfg/foo")
Write("/home/cole/code/nixcfg/foo")
Matches glob: false
Create("/home/cole/code/nixcfg/.git/index")
Matches glob: false

just results in no matches

How to reproduce

Try to use watch without triggering on .git changes.

Expected behavior

There to be some way to achieve this with the built in glob feature

Screenshots

No response

Configuration

| key                | value                                           |
| ------------------ | ----------------------------------------------- |
| version            | 0.70.0                                          |
| branch             |                                                 |
| commit_hash        |                                                 |
| build_os           | linux-x86_64                                    |
| build_target       | x86_64-unknown-linux-gnu                        |
| rust_version       | rustc 1.64.0                                    |
| cargo_version      | cargo 1.64.0                                    |
| pkg_version        | 0.70.0                                          |
| build_time         | 1980-01-01 00:00:00 +00:00                      |
| build_rust_channel | release                                         |
| features           | database, dataframe, default, trash, which, zip |
| installed_plugins  |        |

Additional context

No response

Activity

  1. colemickens commented on Nov 6, 2022

    @colemickens
    Author

    Oh I guess glob is its own command

  2. rgwood commented on Nov 6, 2022

    @rgwood
    Contributor

    Hmm. I wrote watch so this is probably my fault. That’s a tricky glob pattern!

    I’ll take a look tomorrow.

  3. sholderbach commented on Nov 6, 2022

    @sholderbach
    Member

    I think this points to a larger issue we have: we have our own glob implementation in nu-glob used in the filesystem commands (ls etc. and in this case watch) and also have the glob command for which we import the globbing behavior from the wax crate.
    I think in the longer term we should only have one source of truth for the globbing.

  4. added
    category:inconsistent-behavior
    A:file-system
    on Nov 6, 2022
  5. colemickens commented on Nov 6, 2022

    @colemickens
    Author

    globbing doesn't really respect the ordering the same way bash does either.

    "foo/{z,y,x}" will match "foo/x" "foo/y" "foo/z"

    which in my case, break things and means I have to use a different construct

  6. fdncred commented on Nov 6, 2022

    @fdncred
    Contributor

    I think in the longer term we should only have one source of truth for the globbing.

    Agreed @sholderbach. The reason I added wax and the glob command for globbing was because we had to strip some functionality from nu-glob to get it to work right with ls.

  7. rgwood commented on Dec 10, 2022

    @rgwood
    Contributor

    So I guess the conclusion here is that watch should switch to wax for globbing?

  8. fdncred commented on Dec 10, 2022

    @fdncred
    Contributor

    @rgwood another option may be to allow watch to take an input from the glob command. e.g.

    glob 'some convoluted glob syntax' | watch . { |op, path, new_path| $"($op) ($path) ($new_path)"}
    

    Not sure if that is a better or worse design. Just throwing things out for consideration. You usually have a good feel about that stuff.

  9. rgwood commented on Dec 10, 2022

    @rgwood
    Contributor

    Hmm, that's a clever idea but I don't think it would work for watch.

    glob ... | watch ... would gather a list of files and then tell watch to watch only those files. But watch needs to be able to report on files that are created after watch is launched.

  10. melMass commented on Dec 10, 2022

    @melMass
    Contributor

    What about making watch support multiple arguments? for instance a subcommand watch files

    watch files **/*.rs {|i| print $i}
    watch files (<command that returns a list>) {|i| print $i}
  11. rgwood commented on Dec 10, 2022

    @rgwood
    Contributor

    @melMass

    watch files **/*.rs {|i| print $i}

    I think this is the same as what you can do right now with watch . --glob=**/*.rs { |i| print $i }. Am I missing something?

    watch files (<command that returns a list>) {|i| print $i}

    Is <command that returns a list> returning a list of glob patterns? That could work but it feels a bit too complicated to me.

  12. melMass commented on Dec 10, 2022

    @melMass
    Contributor

    I think this is the same as what you can do right now with watch . --glob=**/*.rs { |i| print $i }. Am I missing something?

    Is returning a list of glob patterns? T

    The idea would be that the input is X args plus a closure, in the first example glob would get expanded prior which I think is how nu works, and the command in the second sample could return a string or a list<string> (of relative or absolute paths to files)

    So it could be also as simple as

    watch files myfile.log {|i| open $i | last}
  13. rgwood commented on Dec 10, 2022

    @rgwood
    Contributor

    ^ we discussed this on Discord: https://discord.com/channels/601130461678272522/615329862395101194/1051209969439101010

    I think we came to the conclusion that the current watch <path> --glob=<glob> <closure> design is OK (but watch should handle more sophisticated glob patterns).

  14. added
    help wanted
    on Dec 23, 2022
  15. added
    A:glob
    on Jul 9, 2023
  16. instantepiphany commented on Nov 26, 2023

    @instantepiphany

    ^ we discussed this on Discord: https://discord.com/channels/601130461678272522/615329862395101194/1051209969439101010

    I think we came to the conclusion that the current watch <path> --glob=<glob> <closure> design is OK (but watch should handle more sophisticated glob patterns).

    I've just encountered this. I want to run cargo run --bin server to build a specific binary in a rust workspace whenever the files in server or lib (local dependency) change.
    I want this to happen for .rs and .toml files.

    There is another workspace folder, client which should not be watched as it is not a dependency of server.

    So with the current glob limitations in watch, I:

    1. Can't specify that only files ending in .rs or .toml should be watched (ie with **/*.{rs,toml})
    2. Can't specify that only files in server or lib should be watched (ie with **/{server,lib}/**)
    3. Can't ignore specific folders (ie like glob can via --exclude).

    If watch was updated to use wax, or if watch supported multiple globs, this would be possible (via wax "alternatives" as in my examples, or by supplying multiple globs like **/server/*.rs, **/server/Cargo.toml, **/lib/*.rs, **/lib/Cargo.toml).

    I suppose I could do another layer of filtering inside the closure, but that feels inefficient and awkward.

  17. fdncred commented on Nov 26, 2023

    @fdncred
    Contributor

    The glob command also doesn't accept multiple separate globs on the command line. Although, I'd like it to. Seems like we could do it with the any functionality of wax but I could never get it integrated properly. The glob command does offer a completely different syntax though. It is sometimes helpful to be able to do things like glob **/*.{rs,toml} --depth 2.

  18. instantepiphany commented on Nov 26, 2023

    @instantepiphany

    The glob command also doesn't accept multiple separate globs on the command line. Although, I'd like it to. Seems like we could do it with the any functionality of wax but I could never get it integrated properly. The glob command does offer a completely different syntax though. It is sometimes helpful to be able to do things like glob **/*.{rs,toml} --depth 2.

    Thats the alternative (alternatives, ha) that I mentioned in my comment.

    If wax was used as the library for nu-glob then I could have used watch . --glob **/{server,lib}/**/*.{rs,toml} {|| cargo run --bin server} and that would have worked fine.

    Also unfortunate is that this glob matches 0 files, but doesn't error. I was scratching my head for quite a while before I realised that glob behaves differently to nu-glob used by watch - my first response was to put that glob pattern into glob <pattern> so was confused when it produced exactly the files I wanted it to 😄

  19. fdncred commented on Nov 27, 2023

    @fdncred
    Contributor

    ... I was scratching my head for quite a while before I realised that glob behaves differently to nu-glob ...

    nu-glob was meant to be a more simplistic globbing crate, whereas glob using wax was meant to be more "advanced". The two aren't necessarily compatible with each other.

    I'm not sure if anyone is against switching glob crates in the watch command. What may be better is removing the globbing from watch and passing it in so one could do glob some_complicated_globbing | watch --param1 --param2. Then it would be composable and more nushell-y. If you wanted nu-glob functionality you could do ls some_nu_glob | get name | watch --param1 --parm2.

  20. instantepiphany commented on Dec 10, 2023

    @instantepiphany

    ... I was scratching my head for quite a while before I realised that glob behaves differently to nu-glob ...

    nu-glob was meant to be a more simplistic globbing crate, whereas glob using wax was meant to be more "advanced". The two aren't necessarily compatible with each other.

    This makes sense - but for someone new to nu, it is a fair assumption that "globbing" in nu is consistent. Silently using different globbing logic will absolutely confuse users.

    I'm not sure if anyone is against switching glob crates in the watch command. What may be better is removing the globbing from watch and passing it in so one could do glob some_complicated_globbing | watch --param1 --param2. Then it would be composable and more nushell-y. If you wanted nu-glob functionality you could do ls some_nu_glob | get name | watch --param1 --parm2.

    I thought about this when I encountered the problem.

    I think the trick with passing the glob paths in to watch as piped input is that if the command that watch runs generates files that would and should match the filter, they won't get picked up unless the user restarts watch.

    Whereas if watch is passed an argument, it can reexecute the glob after each time the requested command is invoked by watch, and get a new, current list of paths.

  21. fdncred commented on Dec 10, 2023

    @fdncred
    Contributor

    I thought about this when I encountered the problem.

    makes sense

  22. kronberger-droid commented on Nov 13, 2026

    @kronberger-droid
    Contributor

    The difference comes from glob and watch using two different glob implementations.
    The dc-glob backend replaces both, and with nu --experimental-options [dc-glob] watch --glob '**/*.{rs,toml}' and glob already agree.
    That work is tracked in #18101, so closing this in favour of it.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions