Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad record with unclosing quotes cause unhelpful error messages (need better error messages) #12795

Closed
wdv4758h opened this issue May 7, 2024 · 3 comments · Fixed by #12868
Closed
Labels
needs-triage An issue that hasn't had any proper look parser Issues related to parsing unhelpful-error The error message you observe is not helpful to identify the problem
Milestone

Comments

@wdv4758h
Copy link

wdv4758h commented May 7, 2024

Describe the bug

def func [] {
  {
    "A": ""B"   # <- the quote is bad
  }
}

def main [] {
}
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[/tmp/error.nu:9:1]
 8 │ }
   ╰────

How to reproduce

Run above code.

Expected behavior

Show some helpful error messages pointing that bad quote line.

For comparison, here is the error messages from Python for similar case:

  File "/tmp/error.py", line 3
    "A": ""B"
           ^
SyntaxError: unterminated string literal (detected at line 3)

It will be helpful if nushell can output error messages like this. The showcase is dumb, but I notice this kind off issue is hard to find when code gets bigger.

Screenshots

No response

Configuration

key value
version 0.93.0
major 0
minor 93
patch 0
branch
commit_hash 3b220e0
build_os linux-x86_64
build_target x86_64-unknown-linux-gnu
rust_version rustc 1.77.2 (25ef9e3d8 2024-04-09)
rust_channel 1.77.2-x86_64-unknown-linux-gnu
cargo_version cargo 1.77.2 (e52e36006 2024-03-26)
build_time 2024-04-30 23:06:38 +00:00
build_rust_channel release
allocator mimalloc
features dataframe, default, sqlite, static-link-openssl, system-clipboard, trash, which
installed_plugins query

Additional context

No response

@wdv4758h wdv4758h added the needs-triage An issue that hasn't had any proper look label May 7, 2024
@wdv4758h wdv4758h changed the title Bad record with unclosing quotes cause unhelpful error messages Bad record with unclosing quotes cause unhelpful error messages (need better error messages) May 7, 2024
@bew
Copy link

bew commented May 7, 2024

Well, Python can give this quick error because unlike nushell, Python does not support multiline strings using "..."
(only """...""" works for multiline str in python)
This is a valid nushell:

"foo
bar
baz"

so if the last " is not there it looks like the string is never finished and you get an error for that.

@wdv4758h
Copy link
Author

wdv4758h commented May 7, 2024

I don't think multiline support is a good reason to have bad error messages. Python output better error messages even with the """ multiline string. It shows the source of line that quote does not close. And actually, that is not the case in much older Python versions. It needs effort to improve error messages. Raw parsing error sometime is not helpful enough.

Not an expert of nushell parser, but I will guess nushell parser has the context that which line the quote begins and doesn't close.

@WindSoilder
Copy link
Collaborator

Well, unexpected_eof is the key to allow multi-line string. You can define a function like this:

def func [] {
   {
     "A": ""B"   # <- the quote is bad
   }
 }"
 }
 }

But I agree that the error message can be better.

@sholderbach sholderbach added parser Issues related to parsing unhelpful-error The error message you observe is not helpful to identify the problem labels May 8, 2024
IanManske added a commit that referenced this issue May 15, 2024
# Description
Fixes: #12795

The issue is caused by an empty position of `ParseError::UnexpectedEof`.
So no detailed message is displayed.
To fix the issue, I adjust the start of span to `span.end - 1`. In this
way, we can make sure that it never points to an empty position.

After lexing item, I also reorder the unclosed character checking . Now
it will be checking unclosed opening delimiters first.

# User-Facing Changes
After this pr, it outputs detailed error message for incomplete string
when running scripts.

## Before
```
❯ nu -c "'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:4]
 1 │ 'ab
   ╰────
> ./target/debug/nu -c "r#'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:6]
 1 │ r#'ab
   ╰────
```
## After
```
> nu -c "'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:3]
 1 │ 'ab
   ·   ┬
   ·   ╰── expected closing '
   ╰────
> ./target/debug/nu -c "r#'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:5]
 1 │ r#'ab
   ·     ┬
   ·     ╰── expected closing '#
   ╰────
```


# Tests + Formatting
Added some tests for incomplete string.

---------

Co-authored-by: Ian Manske <ian.manske@pm.me>
@hustcer hustcer added this to the v0.94.0 milestone May 15, 2024
FilipAndersson245 pushed a commit to FilipAndersson245/nushell that referenced this issue May 18, 2024
# Description
Fixes: nushell#12795

The issue is caused by an empty position of `ParseError::UnexpectedEof`.
So no detailed message is displayed.
To fix the issue, I adjust the start of span to `span.end - 1`. In this
way, we can make sure that it never points to an empty position.

After lexing item, I also reorder the unclosed character checking . Now
it will be checking unclosed opening delimiters first.

# User-Facing Changes
After this pr, it outputs detailed error message for incomplete string
when running scripts.

## Before
```
❯ nu -c "'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:4]
 1 │ 'ab
   ╰────
> ./target/debug/nu -c "r#'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:6]
 1 │ r#'ab
   ╰────
```
## After
```
> nu -c "'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:3]
 1 │ 'ab
   ·   ┬
   ·   ╰── expected closing '
   ╰────
> ./target/debug/nu -c "r#'ab"
Error: nu::parser::unexpected_eof

  × Unexpected end of code.
   ╭─[source:1:5]
 1 │ r#'ab
   ·     ┬
   ·     ╰── expected closing '#
   ╰────
```


# Tests + Formatting
Added some tests for incomplete string.

---------

Co-authored-by: Ian Manske <ian.manske@pm.me>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage An issue that hasn't had any proper look parser Issues related to parsing unhelpful-error The error message you observe is not helpful to identify the problem
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants