Skip to content

Nu adds uneceseray quotes to ints, giving type errors #5472

Closed

Description

@FilipAndersson245

Describe the bug

Nu seems to add quotes to script file inputs when not necessary, making incorrect typings.

the following script does not execute.

# foo.nu
def main [a: int] {}

How to reproduce

  1. Have a scriptfile with a type limited parameter. example can be seen above.
  2. Try to execute the script nu foo.nu 5

Expected behavior

It should not give error upon running.

Screenshots

image
image

Configuration

key value
version 0.62.0
branch master
tag
short_commit 1cb449b
commit_hash 1cb449b
commit_date 2022-05-07 12:33:33 +00:00
build_os windows-x86_64
rust_version rustc 1.60.0 (7737e0b5c 2022-04-04)
rust_channel stable-x86_64-pc-windows-msvc
cargo_version cargo 1.60.0 (d1fd9fe2c 2022-03-01)
pkg_version 0.62.0
build_time 2022-05-07 16:06:29 +02:00
build_rust_channel release
features database, dataframe, default, trash, which, zip
installed_plugins gstat

Additional context

No response

Activity

  1. merelymyself commented on May 9, 2022

    @merelymyself
    Contributor

    This was probably happening because of

    pub fn escape_quote_string(input: &str) -> String {
    let mut output = String::with_capacity(input.len() + 2);
    output.push('"');
    for c in input.chars() {
    if c == '"' || c == '\\' {
    output.push('\\');
    }
    output.push(c);
    }
    output.push('"');
    output
    }

    Not quite sure how to deal with this, while still resolving #5370 and #5371. @uasi , any ideas?

  2. uasi commented on May 10, 2022

    @uasi
    Contributor

    @merelymyself We have to quote values or not depending on the parameter type. For example, -10 must not be quoted if and only if the parameter is int:

    〉def main_int [x: int] { echo $x }
    
    〉main_int -10
    -10
    
    〉main_int "-10"
    Error: nu::parser::parse_mismatch (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.Expected)
    
      × Parse mismatch during operation.
       ╭─[entry #3:1:1]
     1 │ main_int "-10"
       ·          ──┬──
       ·            ╰── expected int
       ╰────
    
    〉def main_string [x: string] { echo $x }
    
    〉main_string -10
    Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
    
      × The `main_string` command doesn't have flag `-1`.
       ╭─[entry #5:1:1]
     1 │ main_string -10
       ·              ┬
       ·              ╰── unknown flag
       ╰────
      help: use main_string --help for a list of flags
    
    〉main_string "-10"
    -10
    
  3. merelymyself commented on May 10, 2022

    @merelymyself
    Contributor

    @uasi I tried using .parse() to check for this. Should work because unquoted ints translate nicely to strings but not the other way round.

    /home/gabriel/CodingProjects/nushell〉cat foo.nu                              
    def main [a: string] { echo $a }
    /home/gabriel/CodingProjects/nushell〉target/debug/nu foo.nu 5                
    5
    
  4. uasi commented on May 10, 2022

    @uasi
    Contributor

    @merelymyself unquoted negative ints don't, though.

    〉cat foo.nu
    def main [a: string] { echo $a }
    
    〉target/debug/nu foo.nu -10
    Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
    
      × The `main` command doesn't have flag `-1`.
       ╭─[<commandline>:1:1]
     1 │ main -10
       ·       ┬
       ·       ╰── unknown flag
       ╰────
      help: use main --help for a list of flags
    
    〉target/debug/nu foo.nu "-10"
    Error: nu::parser::unknown_flag (https://docs.rs/nu-parser/0.62.1/nu_parser/enum.ParseError.html#variant.UnknownFlag)
    
      × The `main` command doesn't have flag `-1`.
       ╭─[<commandline>:1:1]
     1 │ main -10
       ·       ┬
       ·       ╰── unknown flag
       ╰────
      help: use main --help for a list of flags
    
  5. merelymyself commented on May 10, 2022

    @merelymyself
    Contributor

    Good catch, @uasi . Will have to look more into this.

  6. uasi commented on May 10, 2022

    @uasi
    Contributor

    Let's put things in perspective. Command line arguments are passed to nushell as an array of strings. To convert strings to values of the appropriate type, we quote (or not quote) arguments by escape_quote_string_*(), concatenate the results, and evaluate the whole string as a command invocation.

    The problem is, it is sometimes impossible to determine what the appropriate type is from a given string alone. Does the string -10 mean a negative int? If the signature of the main function is [x: int], we can probably say yes. So what if the signature is [x: any]? Does the string [a b] mean a string or an array of two bare strings? Does --flag mean a flag? We can't be sure, because if the signature is [--flag: bool, x?: string], it can either be a flag or a string value to x.

  7. added this to the v0.64.0 milestone on Jun 11, 2022
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

    category:bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions