Skip to content

nu -c "# '" fails #5175

Closed
Closed

Description

@ajeetdsouza

Describe the bug

nu -c "# '" gives an error of expected closing '. However, the opening ' is commented out, so this error should not be occurring.

How to reproduce

In bash or nu, run nu -c "# '".

Expected behavior

Expected: no error.

Screenshots

No response

Configuration

key value
version 0.61.0
branch nushell
tag
build_os macos-aarch64
rust_version rustc 1.59.0
cargo_version cargo 1.59.0
pkg_version 0.61.0
build_time 2022-04-12 20:38:15 +00:00
build_rust_channel release
features dataframe, default, trash, which, zip
installed_plugins

Additional context

No response

Activity

  1. added
    category:bug
    on Apr 13, 2022
  2. fdncred commented on Apr 13, 2022

    @fdncred
    Contributor

    I noticed that the error provides a hint, everything is single quotes in the error.

    ./target/debug/nu -c "# '"
    Error: nu::parser::unexpected_eof (link)
    
      × Unexpected end of code.
       ╭─[source:1:1]
     1 │ nu -c '# ''
       ·            ▲
       ·            ╰── expected closing '
       ╰────
    
  3. added this to the v0.62.0 milestone on Apr 21, 2022
  4. uasi commented on Apr 29, 2022

    @uasi
    Contributor

    Now nu -c '# `' fails.

  5. ajeetdsouza commented on Apr 29, 2022

    @ajeetdsouza
    Author

    @jntrnr can this be reopened?

  6. sophiajt commented on Apr 30, 2022

    @sophiajt
    Contributor

    Can we change the issue to the example that's currently broken?

    Also, can you walk me through when this happens in practice? It reads like we're just trying corner cases but would be good to know that this fixes a real world issue.

  7. uasi commented on Apr 30, 2022

    @uasi
    Contributor

    This is certainly a real world issue. Currently (1fc7abc) any one-liner containing backticks will fail or misbehave.

    bash-3.2$ target/debug/nu -c 'mkdir "./my_dir_with_`_in_name"'
    Error: nu::parser::unexpected_eof (https://docs.rs/nu-parser/0.61.1/nu_parser/enum.ParseError.html#variant.UnexpectedEof)
    
      × Unexpected end of code.
       ╭─[source:1:1]
     1 │ nu -c `mkdir "./my_dir_with_`_in_name"`
       ·                                        ▲
       ·                                        ╰── expected closing "
       ╰────
    
    bash-3.2$ target/debug/nu -c 'echo `hello world`'
    `echo `hello
    
  8. uasi commented on Apr 30, 2022

    @uasi
    Contributor

    Also, nu -c "#'" still fails (note the lack of the whitespace between # and ').

    bash-3.2$ target/debug/nu -c "#'"
    Error: nu::parser::missing_flag_param (https://docs.rs/nu-parser/0.61.1/nu_parser/enum.ParseError.html#variant.MissingFlagParam)
    
      × Missing flag argument.
       ╭─[source:1:1]
     1 │ nu -c #'
       ·    ─┬
       ·     ╰── flag missing string argument
       ╰────
    

    This also has real world implications as passing any argument that contains one single quote and no whitespace to a nu script will fail:

    bash-3.2$ cat script.nu
    def main [x: any] {
      echo $x
    }
    
    bash-3.2$ target/debug/nu script.nu "McDonald's"
    Error: nu::parser::unexpected_eof (https://docs.rs/nu-parser/0.61.1/nu_parser/enum.ParseError.html#variant.UnexpectedEof)
    
      × Unexpected end of code.
       ╭─[<commandline>:1:1]
     1 │ main McDonald's
       ·                ▲
       ·                ╰── expected closing '
       ╰────
    
  9. uasi commented on Apr 30, 2022

    @uasi
    Contributor

    The above issues are due to the following lines not escaping backticks in arg and not escaping arg containing no whitespace:

    nushell/src/main.rs

    Lines 85 to 89 in 1fc7abc

    args_to_script.push(if arg.contains(' ') {
    format!("`{}`", arg)
    } else {
    arg
    });

  10. sophiajt commented on Apr 30, 2022

    @sophiajt
    Contributor

    @uasi - agreed, that seems like the cause.

    If you have fixes, we'd happily take a PR for them.

  11. uasi commented on Apr 30, 2022

    @uasi
    Contributor

    @jntrnr unfortunately this issue doesn't seem to be easily fixed.

    format!("`{}`", arg) and format!("'{}'", arg) are insufficient. format!("\"{}\"", arg.replace('\\', r"\\").replace('"', r#"\""#) should work, but nu -c misparses escaped double quotes as described in #5383.

  12. sophiajt commented on May 1, 2022

    @sophiajt
    Contributor

    With @uasi's help, I think this is now fixed in the latest main.

  13. ajeetdsouza commented on May 1, 2022

    @ajeetdsouza
    Author

    @jntrnr the real world use case is that I'm using nu -c in CI for zoxide's nushell plugin. The use case is to test for syntax errors in the generated init script.

    I discovered this because I was using an apostrophe in a comment, which ended up breaking CI.

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