n-riesco / jp-babel

jp-babel is a babel kernel for the Jupyter notebook
Other
86 stars 16 forks source link

jupyter console: syntax errors are impossible to recover from #13

Closed shreevatsa closed 7 years ago

shreevatsa commented 7 years ago

Consider a typo like the following:

% jupyter console --kernel=babel
KERNEL: SHELL_SOCKET: Unhandled message type: history_request
Jupyter console 5.1.0

jp-babel v0.2.0
https://github.com/n-riesco/jp-babel

In [1]: if (3 == 4) ( console.log('wow'); }
SyntaxError: unknown: Unexpected token, expected , (1:32)
> 1 | if (3 == 4) ( console.log('wow'); }
    |                                 ^

and then you can hit Ctrl-C all you like, but nothing works and you basically have to kill the process.

This issue is not present in IJavascript:

% jupyter console --kernel=javascript
KERNEL: SHELL_SOCKET: Unhandled message type: history_request
Jupyter console 5.1.0

IJavascript v5.0.17
https://github.com/n-riesco/ijavascript

In [1]: if (3 == 4) ( console.log('wow'); }
SyntaxError: Unexpected token ;
    at Object.exports.runInThisContext (vm.js:53:16)
    at run ([eval]:613:19)
    at onRunRequest ([eval]:384:22)
    at onMessage ([eval]:352:17)
    at emitTwo (events.js:87:13)
    at process.emit (events.js:172:7)
    at handleMessage (internal/child_process.js:695:10)
    at Pipe.channel.onread (internal/child_process.js:440:11)

In [2]:                                                                                                                                                                                                                                       

where you get back the shell prompt and can fix your typo and continue to work.

n-riesco commented 7 years ago

Closed via https://github.com/n-riesco/jp-babel/commit/bc95edff49329d379d133ac917fc74cba6ea5545 .

shreevatsa commented 7 years ago

Thanks for the commit and the new release! The is_complete_request from https://github.com/n-riesco/ijavascript/issues/97 seems to be working perfectly.

About the issue from this report, it seems that the change helps a great deal, in the sense that for various inputs I tried that contain a syntax error, jp-babel doesn't complete the line, which is reasonably clear indication that I need to go back and edit the line to get rid of the errors. So in a sense one could consider this issue solved. However, if I force the input and hit Alt-Enter on a line, then the issue is still present:

~/jupyter/bin/jupyter console --kernel=babel
Jupyter console 5.1.0

jp-babel v1.0.0
https://github.com/n-riesco/jp-babel

In [1]: if (3 == 4) ( console.log('wow'); }
      : 
      : 
      : 
      : 
      : 
      : 
      : 
      : 
SyntaxError: unknown: Unexpected token, expected , (1:32)
> 1 | if (3 == 4) ( console.log('wow'); }
    |                                 ^
  2 | 
  3 | 
  4 | 

(where I hit Alt-Enter to end the input) and after this, hitting Ctrl-C just shows ^C in the terminal.

In practice it is not a problem as the user can just avoid hitting Alt-Enter :-)

n-riesco commented 7 years ago

@shreevatsa I've opened a new issue (see #14), but this issue is unlikely to be fixed, because Babel's parser doesn't distinguish invalid code from incomplete code (it throws an exception in both cases).

shreevatsa commented 7 years ago

@n-riesco Hmm ok: my report here was more about the fact that after the exception is thrown, control doesn't return to the terminal. I'm not sure it's necessary to distinguish between invalid and incomplete code for that. (E.g. if I hit { and then Alt-Enter, I guess it counts as incomplete code? but the issue is the same.)

This issue isn't present in IJavascript:

% ~/jupyter/bin/jupyter console --kernel=javascript
Jupyter console 5.1.0

IJavascript v5.0.18
https://github.com/n-riesco/ijavascript

In [1]: {
SyntaxError: Unexpected end of input
    at Object.exports.runInThisContext (vm.js:53:16)
    at run ([eval]:613:19)
    at onRunRequest ([eval]:384:22)
    at onMessage ([eval]:352:17)
    at emitTwo (events.js:87:13)
    at process.emit (events.js:172:7)
    at internal/child_process.js:729:12
    at nextTickCallbackWith0Args (node.js:489:9)
    at process._tickCallback (node.js:418:13)

In [2]: var x = "I can type here";
Out[2]: undefined

(see how I was able to get to [2] and type something). While in jp-babel:

% ~/jupyter/bin/jupyter console --kernel=babel
Jupyter console 5.1.0

jp-babel v1.0.0
https://github.com/n-riesco/jp-babel

In [1]: {
SyntaxError: unknown: Unexpected token (1:1)
> 1 | {
    |  ^
    at Parser.pp$5.raise (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:4373:13)
    at Parser.pp.unexpected (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:1716:8)
    at Parser.pp$3.parseExprAtom (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3683:12)
    at Parser.pp$3.parseExprSubscripts (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3427:19)
    at Parser.pp$3.parseMaybeUnary (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3407:19)
    at Parser.pp$3.parseExprOps (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3337:19)
    at Parser.pp$3.parseMaybeConditional (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3314:19)
    at Parser.pp$3.parseMaybeAssign (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3277:19)
    at Parser.pp$3.parseExpression (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:3239:19)
    at Parser.pp$1.parseStatement (/Users/srajagopalan/jupyter/node_modules/jp-babel/node_modules/babel-core/node_modules/babylon/lib/index.js:1861:19)
^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C^C

and the only way out is to kill the process from another shell.