Bug #20918
openPrism error indicates line number of `-e` that does not exist
Description
$ ruby -e 'foo('
-e: -e:2: syntax error found (SyntaxError)
1 | foo(
> 2 |
| ^ unexpected end-of-input; expected a `)` to close the arguments
It says -e:2
, but there is no line 2 in -e 'foo('
.
eval("foo(")
reports line 1, so I guess this issue is only for -e
.
Updated by ydah (Yudai Takada) 4 days ago · Edited
It seems that the foo {
has the same problem.
❯ ruby -e 'foo {'
-e: -e:2: syntax errors found (SyntaxError)
> 1 | foo {
| ^ expected a block beginning with `{` to end with `}`
> 2 |
| ^ unexpected end-of-input, assuming it is closing the parent top level context
Since the error is not confirmed until EOL, it looks like you are outputting the position where the error is confirmed.
❯ ruby -e "foo(
"
-e: -e:4: syntax error found (SyntaxError)
2 |
3 |
> 4 |
| ^ unexpected end-of-input; expected a `)` to close the arguments
Updated by hsbt (Hiroshi SHIBATA) 1 day ago
- Status changed from Open to Assigned
Updated by kddnewton (Kevin Newton) about 4 hours ago
I looked into this this morning, it looks like ruby.c is automatically concatenating a \n onto the -e script here: https://github.com/ruby/ruby/blob/f43585b02c3634ab9a4e54049b08e04ab1a640fd/ruby.c#L1303. Is this desired behavior?
Updated by tenderlovemaking (Aaron Patterson) about 3 hours ago
kddnewton (Kevin Newton) wrote in #note-3:
I looked into this this morning, it looks like ruby.c is automatically concatenating a \n onto the -e script here: https://github.com/ruby/ruby/blob/f43585b02c3634ab9a4e54049b08e04ab1a640fd/ruby.c#L1303. Is this desired behavior?
I'm not sure if it matters. If you have a script with one line (that has a newline), Prism will report line 2 as the error.
[aaron@tc-lan-adapter ~/g/ruby (master)]$ cat x.rb
foo(
[aaron@tc-lan-adapter ~/g/ruby (master)]$ od -c x.rb
0000000 f o o ( \n
0000005
[aaron@tc-lan-adapter ~/g/ruby (master)]$ wc -l x.rb
1 x.rb
[aaron@tc-lan-adapter ~/g/ruby (master)]$ ./miniruby x.rb
x.rb: x.rb:2: syntax error found (SyntaxError)
1 | foo(
> 2 |
| ^ unexpected end-of-input; expected a `)` to close the arguments
[aaron@tc-lan-adapter ~/g/ruby (master)]$
It's fine for newlines to appear in method parameters as well as blocks, so Prism considers the EOF token to be the "error token". Indeed EOF "occurs" on line 2 (since it's after the newline), but since nobody writes EOF in to their files, I think we should consider the error to have occurred at the newline preceding EOF.
One more example:
[aaron@tc-lan-adapter ~/g/ruby (master)]$ cat x.rb
foo(
[aaron@tc-lan-adapter ~/g/ruby (master)]$ wc -l x.rb
5 x.rb
[aaron@tc-lan-adapter ~/g/ruby (master)]$ od -c x.rb
0000000 f o o ( \n \n \n \n \n
0000011
[aaron@tc-lan-adapter ~/g/ruby (master)]$ ./miniruby x.rb
x.rb: x.rb:6: syntax error found (SyntaxError)
4 |
5 |
> 6 |
| ^ unexpected end-of-input; expected a `)` to close the arguments
[aaron@tc-lan-adapter ~/g/ruby (master)]$
Most tools agree the above file only has 5 lines in it, but Prism reports the error on line 6. IMO errors should only occur on lines that exist.