Skip to content

Nu script files cannot accept the string '(', '[', or "'" as arguments. #9939

Closed

Description

@amachang

Describe the bug

Nu script files cannot accept the string '(', '[', or "'" as arguments.

The commands below produce the same error:

~/dev/nushell> nu test.nu '('
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[<commandline>:1:1]
 1 │ main (
   ╰────

~/dev/nushell> nu test.nu '['
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[<commandline>:1:1]
 1 │ main [
   ╰────

~/dev/nushell> nu test.nu "'"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[<commandline>:1:1]
 1 │ main '
   ╰────

How to reproduce

  1. Create a script file using the following command: echo 'def main [a] { $a }' | save --force test.nu
  2. Execute the created script file with the string argument '(' as follows: nu test.nu '('

Expected behavior

For a script file like def main [a] { $a }, it should return the same character given, no matter what character is provided.

For example:

> echo 'def main [a] { $a }' | save --force test.nu
> nu test.nu '('
(

In the above case, when the string argument '(' is provided to the script, it should return '('.

Screenshots

No response

Configuration

key value
version 0.83.2
branch main
commit_hash c8f3799
build_os macos-x86_64
build_target x86_64-apple-darwin
rust_version rustc 1.69.0 (84c898d65 2023-04-16)
rust_channel 1.69.0-x86_64-apple-darwin
cargo_version cargo 1.69.0 (6e9a83356 2023-04-12)
build_time 2023-08-07 12:14:48 +09:00
build_rust_channel release
allocator standard
features default, sqlite, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    status:needs-triage
    on Aug 7, 2023
  2. changed the title [-]Nu script files cannot accept the string '(', '[', or "'".[/-] [+]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/+] on Aug 7, 2023
  3. amachang commented on Aug 7, 2023

    @amachang
    Author
  4. amachang commented on Aug 7, 2023

    @amachang
    Author

    I've read the related issues. Here's what I think: Being able to handle all strings is a basic requirement for any script file. So, here are my suggestions:

    For the main command calling code generation process:

    1. Process quoting Optional positional parameters first.
    2. After optional params quoting, quote all the other parameters as strings.
    3. After quoting, generate type casting code like ('10' | into int) using Parameter types.
  5. changed the title [-]Nu script files cannot accept the string '(', '[', or "'" as arguments.[/-] [+]Nu script files cannot accept the string `'('`, `'['`, or `"'"` as arguments.[/+] on Aug 7, 2023
  6. amtoine commented on Aug 7, 2023

    @amtoine
    Member

    might also be related to #9766 where i observe the quotes being removed before the arguments are passed to the script 🤔

    which means "-a" becomes -a and is not a valid option in my issue and here "(" becomes ( and thus gives a syntax error 😕

  7. added
    category:bug
    A:parser
    usage:script-file
    and removed
    status:needs-triage
    on Aug 7, 2023
  8. amachang commented on Aug 7, 2023

    @amachang
    Author

    @amtoine Thanks for linking another issue. I added it to my comment above.

  9. amachang commented on Aug 7, 2023

    @amachang
    Author

    I've looked into escape_for_script_arg and feel there may also be other problems.

    Regarding arg_value and input in the function, I believe that we should modify the code to escape all values first and then perform type casting.

    As for arg_name, I'm not sure if it can be escaped as my knowledge is limited in this area. I need to investigate this. If arg_name doesn't support escaping and can't accept arbitrary strings, then I think the program should fail when there is an invalid value in arg_name.

  10. amtoine commented on Aug 7, 2023

    @amtoine
    Member

    thanks for investigating into this @amachang 🙏

  11. amachang commented on Aug 7, 2023

    @amachang
    Author

    Furthermore, I believe gather_commandline_args shouldn't escape args_to_script. Instead, escaping should happen when generating the code calling main. This would allow for type casting according to main's parameters. And also, gathering the code escaping and generation together would prevent potential issues with double escaping or forgetting to escape, thus increasing maintainability.

    For example:

    let args = format!("main {}", args.map(|a| escape_for_script_arg(a, type_hint_or_something)).collect().join(" "));
  12. fdncred commented on Aug 7, 2023

    @fdncred
    Contributor

    Seems like something good to PR. @amachang Do you have time to try and fix this?

  13. 2 remaining items

  14. amachang commented on Aug 8, 2023

    @amachang
    Author

    I've been looking into the parser code. Base on that, I am considering the following implementation:

    • In the gather_commandline_args function, I'll remove the call to escape_for_script_arg.
    • In the evaluate_file function, when generating the code for main, it will apply escape_for_script_arg for each arg.
    • in the escape_for_script_arg function, I'll modify it to accept a second argument named is_flag_possible. If set to true, it will try to parse arg as a long flag or a short flag if possible, and reconstruct them as nushell flags (I plan to use nu-parser's parse_long_flag and parse_short_flag in this parse process). If arg is not flag, they will undergo type-casting using the into xxx command, determined by the main block signature's SyntaxShape.
    • The evaluate_file function, if it encounters "--" in the args, will set is_flag_possible to false for escape_for_script_arg to ensure that any arguments after "--" are not treated as flags. I believe this is necessary to ensure the command can accept all types of strings, even those beginning with a hyphen.
  15. fdncred commented on Aug 9, 2023

    @fdncred
    Contributor

    We plan on talking a little about this in our meeting today at 3pm central time. Feel free to jump in on the open-mic channel in discord.

  16. sophiajt commented on Aug 9, 2023

    @sophiajt
    Contributor

    We're currently investigating if we can get the raw commandline args passed to nu before they're processed. If we can, we can pass those directly to the nushell parser. If we can't, we may need to switch main to taking in strings as that's all we can ensure from outside of nushell.

  17. amachang commented on Aug 10, 2023

    @amachang
    Author

    @fdncred @jntrnr I apologize, I was asleep at 3pm central time. Should I wait for your investigation before starting the implementation? Regardless, I'll join the discord.

  18. amachang commented on Aug 10, 2023

    @amachang
    Author

    @jntrnr I'm curious about one thing, "raw commandline args ". When launched from other shells like bash, I was under the impression that command-line arguments can only be received as byte sequences (in most cases, they're utf-8 string). Is this understanding correct? Are you suggesting defining a special launch protocol when only launching a nu script file from nushell, allowing it to pass arguments while retaining their types and structures?

  19. fdncred commented on Aug 10, 2023

    @fdncred
    Contributor

    @amachang yes, you should wait until @jntrnr finishes the investigation. we'd hate for you to do a bunch of work and then we have to go a different direction.

  20. amachang commented on Aug 10, 2023

    @amachang
    Author

    @fdncred Roger that 🫡

  21. sophiajt commented on Aug 11, 2023

    @sophiajt
    Contributor

    So far, I haven't been able to find how to reliably get the raw commandline args, as shells are allowed to manipulate these args before they invoke the external application.

    Because of this, I'm leaning to requiring main and family to take in string args as positional arguments and string arguments as as flag parameters.

    I don't think we can try to detect the type, because these two would look the same:

    > nu 3
    > nu "3"
    

    Since the shell will strip the quotes off the second one before it hands it to us. I think we're just stuck with strings.

  22. amachang commented on Aug 12, 2023

    @amachang
    Author

    @jntrnr Thank you for your prompt and detailed response. I apologize for the delay in my response due to the time difference.

    1. We're on the same page regarding the necessity for Rust's entry point, main, to accept command line arguments as strings.
    2. Building on that, have you considered using the declaration of parameter types in the nushell script's main command, like def main [i: int, -a: string], to convert these strings? For instance, within nu-cli's evaluate_file (eval_file.rs) there's a section like:
        if engine_state.find_decl(b"main", &[]).is_some() {
            let args = format!("main {}", args.join(" "));

    Given this, we can extract the SyntaxShape of positional args and flags from the main block's signature. This would let us know the declared parameter types for nushell's main. Using this, for a main defined as def main [i: int] { $i }, we could embed it as main ('10' | into int). What are your thoughts on this?

    1. As mentioned earlier, it should be possible to extract flag SyntaxShape and identifiers from the main block. First, making nu-parser's parse_long_flags and parse_short_flags a bit more abstract and versatile, and then, they can be used to match only those flags that are declared in the main's signature. If we pursue this, I believe we would need a mechanism to stop flag parsing to allow positional args that start with a hyphen, possibly after a -- delimiter appears.

    Please note that the above is just my opinion and you don't have to incorporate it. Once the direction of the project is decided, I'd like to follow and help out in that direction.

  23. unnamedname commented on Nov 18, 2023

    @unnamedname

    In addition, other metacharacters are also unacceptable, like bare =, strings containing | ; { } or beginning with $:

    ❯ ['=' 'a|b' 'a;b' 'a{b' 'a}b' '$a'] | each { nu test.nu $in }
    Error: nu::parser::unknown_state
    
      × Unknown state.
       ╭─[<commandline>:1:1]
     1 │ main =
       · ───┬──
       ·    ╰── Incomplete statement
       ╰────
    
    Error: nu::shell::external_command
    
      × External command failed
       ╭─[<commandline>:1:1]
     1 │ main a|b
       ·        ┬
       ·        ╰── executable was not found
       ╰────
      help: No such file or directory (os error 2)
    
    Error: nu::shell::external_command
    
      × External command failed
       ╭─[<commandline>:1:1]
     1 │ main a;b
       ·        ┬
       ·        ╰── executable was not found
       ╰────
      help: No such file or directory (os error 2)
    
    Error: nu::parser::unexpected_eof
    
      × Unexpected end of code.
       ╭─[<commandline>:1:1]
     1 │ main a{b
       ╰────
    
    Error: nu::parser::unbalanced_delimiter
    
      × Unbalanced delimiter.
       ╭─[<commandline>:1:1]
     1 │ main a}b
       ·       ┬
       ·       ╰── unbalanced { and }
       ╰────
    
    Error: nu::parser::variable_not_found
    
      × Variable not found.
       ╭─[<commandline>:1:1]
     1 │ main $a
       ·      ─┬
       ·       ╰── variable not found.
       ╰────
    
  24. jasha-hrp commented on Jul 19, 2025

    @jasha-hrp
    Contributor

    A workaround that I've found useful is to make the script use stdin instead of an argument:

    #!/usr/bin/env -S nu --stdin
    def main [] { let input_string: string = $in; ... }
    '(' | ./test.nu             # invoked as a shebang script
    '(' | nu --stdin ./test.nu  # invoked with nu
  25. samoylovfp commented on Apr 29, 2026

    @samoylovfp

    The example from the issue reproduction steps does not crash in 0.112.1, but nushell now adds quotations to some inputs, which breaks my specific use case, where I use a small nushell script as a shim for psql and some arguments now grow quotes around them, which breaks psql.

    Script from the repro:

    ~/tmp> nu test.nu "("
    "("
    ~/tmp> nu test.nu "test"
    test
    ~/tmp> nu test.nu "test;"
    "test;"
    
  26. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    The arguments from this thread reach main as strings by now.
    #18217 fixed the delimiter and metacharacter cases, and #19486 covers = and a few more, which will be in the release after 0.117.
    Arguments starting with - need a -- in front of them.

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:parsercategory:bugusage:script-file

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions