Espruino NPM CLI Issue in Sending Code to Board

Posted on
  • Hello,

    I have just installed the espruino NPM package, and is "partially" working.

    My vital signs are:

    Host Machine
      - Windows 10
      - Node:   8.11.3
      - NPM:    5.6.0
      - Espruino NPM package: 0.1.13
    MCU
      - Espruino WiFi
      - Version:  1v99
    

    I can successfully connect to my Espruino WiFi board, and execute the MCU remotely via the espruino NPM CLI.

    However, I am experiencing a number of very weird run-time errors, when sending code to the board using the espruino NPM package. Things like:

    • Cannot place an "else" clause (of an if/then/else) on a second line. The following run-time error is encountered ONLY when the code is in-line (i.e. the error is not experienced when the code is within a function).

      Uncaught SyntaxError: Got else expected EOF
       at line 2 col 1
      else {
      ^
    
    • Cannot "require" more than one module. When the second module is included, I receive the following run-time error:

      Uncaught ReferenceError: "dModules" is not defined
      at line 1 col 10
      dModules.addCached("TestMod3","print('ex­panding TestMod3.js...
    

    All this code runs correctly, when it is sent via the Espruino IDE. I only have these problems when sending the code using the Espruino NPM package.

    Obviously, something very rudimentary is out-of-whack.

    By enabling verbose output, I have determined that the issue is something related to escape sequences sent to the board, however I do not know how to resolve.

    Sending the following code snippet:

    var x = 1;
    if (x===1) {
      x++;
    }
    else { // CRAZY: can't be on a seperate line (when code is in-line)
      x--;
    }
    

    Sends the following stream:

    Sending...
    ---> "\u0010reset();\n\u0010var x = 1;\n\u0010\u001b[4dif (x===1) {\r\u001b\n  x++;\r\u001b\n}\r\u001b\nelse { // CRAZY: can't be on a seperate line (when code is in-line)\r\u001b\n  x--;\r\u001b\n}\n\u0010\u001b[60dsave()\­n\n"
    

    Generating the following run-time error:

    :else { // CRAZY: can't be on a seperate line (when code is in-line)
    :
    :  x--;
    :
    :}
    Uncaught SyntaxError: Got else expected EOF
     at line 2 col 1
    else { // CRAZY: can't be on a seperate line (when code is i...
    ^
    

    Again, the issue has something to do with the escape sequences, but I do not know how to resolve.

    Your help is greatly appreciated in resolving this issue.

    Kevin

  • Can you try updating the NPM package? It seems it was a little bit behind the IDE (I've just published a new version) and a bug had got introduced to do with the line numbering a while back.

    I just tried the code you posted here with the new version and it seems to work ok.

    It's worth noting that if you paste the IF statement you've shown on the left-hand side of the IDE (or in interactive mode using the CLI) then it'll still fail with the same error - because when you hit 'enter' before the else Espruino has no way of knowing that is hadn't seen a complete statement that it should execute.

    When working correctly the CLI tries to detect code like that and rewrites the Carriage Return as a different set of characters which tells Espruino to add a newline but without executing.

  • @Gordon: Thanks for you quick reply. I was out-of-the-office for a bit, hence this delayed response.

    Per you request, I updated the espruino package to ver 0.1.14, and experienced mixed results.

    • GREAT: I can now require more than one module. Previously a rogue "d" was prefixed to the "Modules" reference generating the following run-time error. However this has been fixed with the 0.1.14 release ... I no longer receive this error:

      Uncaught ReferenceError: "dModules" is not defined
      at line 1 col 10
      dModules.addCached("TestMod3","print('ex­panding TestMod3.js...
    
    • PROBLEM: I still am not able to place an else clause on a second line when the code is expended in-line. Here is the run-time error:

      Uncaught SyntaxError: Got else expected EOF
       at line 2 col 1
      else {
      ^
    

    My full code can be found here:

    Here are some points of interest and/or questions:

    1. Why is it that I do not experience this problem with the IDE? I was under the impression that this was the same code.

    2. It appears to me that the "else on a subsequent line" is only a problem for code expanded in-line. I do not experience this issue when the code is contained in a function definition.

    Does this make sense?

    I can work around this issue by placing my in-line code in an IIFE:

       (function () {
         ... my in-line code here
       })();
    

    Evidently espruino does not have to do anything special in regard to cr/lf for code contained with function definitions.

    Would it make sense for espruino to wrap any code it is sending to the board in an IIFE, rather that attempting to identify/react to special cases like this?

    Your help is greatly appreciated.

    Kevin>


    1 Attachment

  • Why is it that I do not experience this problem with the IDE? I was under the impression that this was the same code.

    Yes, it should be the same.

    In fact I just tried here with your job file and it works great for me.

    I think the problem is with Windows line endings in your text files? Could you check?

    It appears to me that the "else on a subsequent line" is only a problem for code expanded in-line. I do not experience this issue when the code is contained in a function definition.

    Yes, that's correct. Espruino Bracket counts to see if a command is finished or not. This might be some help: http://www.espruino.com/Troubleshooting#­i-ve-pasted-code-into-the-left-hand-side­-of-the-web-ide-and-it-doesn-t-work

    I can work around this issue by placing my in-line code in an IIFE:

    Yes, that works fine. Or you could format your code differently, or just save it in a Unix file format for now :)

    Would it make sense for espruino to wrap any code it is sending to the board in an IIFE, rather that attempting to identify/react to special cases like this?

    No... The issue is that if Espruino did that, it'd have to have your entire source file in RAM at once. People routinely upload code files that are themselves bigger than Espruino's available RAM, and the only way it can handle that is by executing each individual command/declaration as it comes in.

    Assuming it really is windows line endings in your file, it should be trivial to make the CLI work around it.

  • Thanks for the info @Gordon.

    I had suspected windows cr/lf was the culprit, but was unfamiliar with all the special processing done by espruino.

    The difference in my new process is it is being checked into a git repository, and I think git is attempting some "neutral" representation of cr/lf. I can research some git settings.

    With that said, however I believe all my production code will be contained inside function definitions, so I should be OK.

    Thanks for your help and insight.

    Kevin

  • No problem. I'll file an issue about this and see what I can do in the CLI itself.

  • If you update from NPM now it should work :)

  • @Gordon,

    I upgraded to espruino@0.1.15 and it appears to now support windows-based cr/lf.

    Thanks!

    BTW: Do you maintain any revision history for the espruino package releases?

  • No, I don't maintain any history for the CLI tools/Web IDE I'm afraid - just for Espruino itself, which is at https://github.com/espruino/Espruino/blo­b/master/ChangeLog

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Espruino NPM CLI Issue in Sending Code to Board

Posted by Avatar for KevinAst @KevinAst

Actions