Skip to content

Filenames with square brackets cause errors #10244

Closed

Description

@takac

Describe the bug

File names with square brackets are treated as globs and cannot be literally referenced as far as I can tell.

touch '[hello].txt'
ls '[hello].txt'
Error:   × No matches found for [hello].txt
   ╭─[entry #14:1:1]
 1 │ ls '[hello].txt'
   ·    ──────┬──────
   ·          ╰── Pattern, file or folder not found
   ╰────
  help: no matches found

How to reproduce

  1. touch '[hello].txt'
  2. ls '[hello].txt'

Expected behavior

I expect to be able to ls a file with square brackets with escapes (e.g. \[ \]) or otherwise and that the completion uses the "right" string to reference the file.

Screenshots

No response

Configuration

| key                | value                                                             |
| ------------------ | ----------------------------------------------------------------- |
| version            | 0.84.0                                                            |
| branch             |                                                                   |
| commit_hash        |                                                                   |
| build_os           | macos-aarch64                                                     |
| build_target       | aarch64-apple-darwin                                              |
| rust_version       | rustc 1.71.1 (eb26296b5 2023-08-03) (built from a source tarball) |
| cargo_version      | cargo 1.71.1                                                      |
| build_time         | 1980-01-01 00:00:00 +00:00                                        |
| build_rust_channel | release                                                           |
| allocator          | standard                                                          |
| features           | default, sqlite, trash, which, zip                                |
| installed_plugins  |                                                                   |

Additional context

Other ways to get the same error:

ls $'[hello].txt'
ls '\[hello\].txt'
ls \[hello\].txt

There is a slightly different error when not in quotes:

ls [hello].txt
Error: nu::parser::parse_mismatch

  × Parse mismatch during operation.
   ╭─[entry #42:1:1]
 1 │ ls [hello].txt
   ·    ─────┬─────
   ·         ╰── expected non-[] value
   ╰────

Activity

  1. added
    status:needs-triage
    on Sep 5, 2023
  2. takac commented on Sep 5, 2023

    @takac
    Author

    Some additional oddities

    open '[doesntexist].json'
    

    Does not produce an error

    Then a error opening files with square brackets in this way

    [1,2,3] | to json | save `[square].json`
    ls *.json | take 1 | insert blob {|f| open $f.name | get 0 }
    
    Error: nu::shell::io_error
    
      × I/O error
      help: can't follow stream paths
    
  3. fdncred commented on Sep 5, 2023

    @fdncred
    Contributor

    Here's what we can do on the latest main branch. Not sure it works on the version you're using.

    ❯ touch '[hello].txt'
    ❯ ls '[[]hello[]].txt'
    ╭#┬───name────┬type┬size┬──modified──╮
    │0│[hello].txt│file│16 B│a minute ago│
    ╰─┴───────────┴────┴────┴────────────╯
    ❯ open '[[]hello[]].txt'
    now is the time
    

    [ and ] are known glob patterns, so I think they currently have to be escaped this way.

  4. takac commented on Sep 5, 2023

    @takac
    Author

    Thanks! That does work on main. The completer/menu probably needs updating to use the right escape sequence. And the escape sequence to be documented somewhere.

    There are so other oddities with open and the json which need exploring. Should I open a separate bugs for those in the comment above?

  5. fdncred commented on Sep 5, 2023

    @fdncred
    Contributor

    I'd like a more holistic solution myself. This escaping is just ugly and no one will really know to do it. Not sure what to do here exactly myself.

  6. amtoine commented on Sep 6, 2023

    @amtoine
    Member

    this looks possibly a duplicate of #9310, can you have a look @takac? 😋

  7. linked a pull request that will close this issueAllow escaping glob with `\` in `nu-glob` #10246on Sep 6, 2023
  8. takac commented on Sep 8, 2023

    @takac
    Author

    Yes they are related, or even duplicate. Seems like a more general issue than just ls or any other command as it's about globs.

  9. added
    deprecated:duplicate
    and removed
    status:needs-triage
    on Sep 8, 2023
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

    deprecated:duplicate

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions