"Unexpected end" issue when extra line gap in code

Hello everyone,

I’m using Julia through VS Code and I often face an issue regarding “end” statements (at least for Ubuntu 20.04) when using the REPL. The problem seems to be that if there are two consecutive empty lines, then the code seems to be evaluated in 2 separate parts, which throws an error.

For instance, the following works:


function test()

a = 2

b = 3

end

But this does not:


function test()

a = 2


b = 3

end

julia> function test()
           a = 2

ERROR: syntax: incomplete: "function" at REPL[7]:1 requires end
Stacktrace:
 [1] top-level scope
   @ none:1

julia> b = 3
3

julia> end
ERROR: syntax: unexpected "end"
Stacktrace:
 [1] top-level scope
   @ none:1

The above was for a function, the following is with for loop. Again, this works:


for i = 1:5

x = 2

println("$i")

end

But this does not:


for i = 1:5

x = 2


println("$i")

end

julia> for i = 1:5
           x = 2

ERROR: syntax: incomplete: "for" at REPL[2]:1 requires end
Stacktrace:
 [1] top-level scope
   @ none:1

julia> println("$i")
ERROR: UndefVarError: i not defined
Stacktrace:
 [1] top-level scope
   @ REPL[3]:1

julia> 

julia> end
ERROR: syntax: unexpected "end"
Stacktrace:
 [1] top-level scope
   @ none:1

Indenting the code does not change anything either.

The weird thing is that it does not happen every time, but does happen fairly often. So I’m not sure how seriously everyone here should take it. I’m posting this in case anyone else has had the same problem.

Note that this happens both when I’m giving the code directly to the terminal from VS Code, or activating the Julia-VS Code environment (via the Julia extension for VS Code) then sending it to the terminal. It might be a Ubuntu 20.04 issue though, so I’d be curious to know if this happens in other OS.

I can reproduce this without VSCode by entering this:

julia> function test()                                          
                                                                
       a = 2                                                    
                                                                
                                                                
ERROR: syntax: incomplete: "function" at REPL[17]:1 requires end
Stacktrace:                                                     
 [1] top-level scope                                            
   @ none:1                                                     

directly into the REPL. That is,function test() followed by newline, newline, then a = 2, followed by newline, newline, newline. The last new line throws the error. Seems like a genuine bug.

Please open an issue at github.com/julialang/julia!

1 Like

It was already discussed here. The relevant section of the manual is at The Julia REPL · The Julia Language.

2 Likes

Oops, hadn’t seen that post somehow. Thanks!