Skip to content

ELECTRON_RUN_AS_NODE not printing output to console on Windows #5715

Closed

Description

@jviotti

For some reason, ELECTRON_RUN_AS_NODE is not working as expected.

If I call electron from cmd.exe, the default application opens. So far so good. Now if I type set ELECTRON_RUN_AS_NODE=1 and call electron again, nothing happens. I don't see the NodeJS prompt as in other operating systems, the electron binary doesn't execute any node scripts I pass as arguments, and no errors are shown.


  • Electron version: v1.1.1, v1.1.3
  • Operating system: Windows 10 x64

Activity

  1. zcbenz commented on May 29, 2016

    @zcbenz
    Contributor

    This is probably related to #5713, after setting breakpoint, it seems that Node threw an uncaught exception on startup, but the error was not successfully printed to console.

  2. jviotti commented on May 30, 2016

    @jviotti
    ContributorAuthor

    I see. Can you actually see the output when running a debugger (I'll try to give it a go myself)? Any ideas on how we can solve this? Happy to help with anything I can.

  3. jviotti commented on May 30, 2016

    @jviotti
    ContributorAuthor

    Actually, v0.37.8 seems to work fine. I'll try to bisect to find the reason.

  4. jviotti commented on May 30, 2016

    @jviotti
    ContributorAuthor

    It was introduced in v1.1.0. I'll try to identify the first bad commit once I get VS 2015 here.

  5. zcbenz commented on May 31, 2016

    @zcbenz
    Contributor

    @jviotti Thanks for locating the version, the exception happens when creating process.stdin, it seems to be introduced by the upgrade of Node.

  6. self-assigned this
    on May 31, 2016
  7. jviotti commented on May 31, 2016

    @jviotti
    ContributorAuthor

    Awesome, thanks a lot for the quick fix!

    Sent from my iPhone

    On May 30, 2016, at 11:20 PM, Cheng Zhao [email protected] wrote:

    Closed #5715 via #5776.

    —
    You are receiving this because you were mentioned.
    Reply to this email directly, view it on GitHub, or mute the thread.

  8. jviotti commented on Jun 6, 2016

    @jviotti
    ContributorAuthor

    Hi @zcbenz ,

    I've just tested v1.2.1 and I still can reproduce the issue. Thoughts?

  9. zcbenz commented on Jun 7, 2016

    @zcbenz
    Contributor

    @jviotti You can not get Node prompt because stdin is not working, you can find explanations in #5776. But normal Node scripts should work.

  10. jviotti commented on Jun 7, 2016

    @jviotti
    ContributorAuthor

    @zcbenz I'm trying to run a normal NodeJS script with it, and still get no output:

    C:\Users\jviot\Projects>electron -v
    
    v1.2.1
    
    C:\Users\jviot\Projects>type foo.js
    console.log('foo');
    
    C:\Users\jviot\Projects>node foo.js
    foo
    
    C:\Users\jviot\Projects>set ELECTRON_RUN_AS_NODE=1
    
    C:\Users\jviot\Projects>electron foo.js
    
    C:\Users\jviot\Projects>
  11. zcbenz commented on Jun 8, 2016

    @zcbenz
    Contributor

    It seems like a Windows 10 only problem. The node script actually works, it is just the output not printed to console.

  12. reopened this on Jun 8, 2016
  13. changed the title [-]ELECTRON_RUN_AS_NODE not working on Windows 10[/-] [+]ELECTRON_RUN_AS_NODE not printing output to console on Windows 10[/+] on Jun 8, 2016
  14. bpasero commented on Jul 8, 2016

    @bpasero
    Contributor

    We also see no output on Windows 10 with Electron 1.2.6. Works fine in 0.37.6.

  15. bpasero commented on Aug 4, 2016

    @bpasero
    Contributor

    @zcbenz can we go back to a solution where we get output running on Windows 10? My understanding is that a change in Electron (> 1.x?) caused this regression. I am asking because we are currently blocked on updating Electron for VS Code until we can get output back on Win 10.

  16. zcbenz commented on Aug 5, 2016

    @zcbenz
    Contributor

    @bpasero I'll take a look.

  17. bpasero commented on Aug 30, 2016

    @bpasero
    Contributor

    @zcbenz I tried to reproduce locally on my Win 10 machine and when I build electron and run the example from #5715 (comment) out of out/D I can see the output but not if I run it from out/R, any clues?

  18. bpasero commented on Sep 1, 2016

    @bpasero
    Contributor

    I take it back, now on my other Windows machine I am not seeing output for both debug and release. This is really Windows 10 command line specific because e.g. our CI builds on Appveyor show the output of a test that was run with ELECTRON_RUN_AS_NODE.

  19. bpasero commented on Sep 3, 2016

    @bpasero
    Contributor

    @zcbenz played around a little bit and now I am facing some questions. I have a simple out.js file which does console.log("Hello World") and my test case is to run electron.exe out.js while ELECTRON_RUN_AS_NODE is set to true.

    I do NOT get the output on the command line (as outlined in this bug), but piping the output to another file shows that the output is indeed there, just not shown in the console.

    Now, when I add the following code after https://github.com/electron/electron/blob/master/atom/app/atom_main.cc#L61 I do see output:

    base::RouteStdioToConsole(false);
    std::cout << "cout works";
    fprintf(stdout, "fprintf works");

    This indicates that the call to base::RouteStdioToConsole(false) seems work (because I do not see the output when not calling those lines).

    Now I wonder why node.js output does not end up in the console. It seems that any console.log from node.js is being rewritten to call process.log (from here: https://github.com/electron/electron/blob/master/lib/browser/init.js#L27) which in the end calls std::cout << message << std::flush in https://github.com/electron/electron/blob/master/atom/common/api/atom_bindings.cc#L83

    But if that is true, why does the call to cout not end up in the console if it does when I manually add it in https://github.com/electron/electron/blob/master/atom/app/atom_main.cc#L61

    Maybe this is a flushing issue where the process terminates before the output is written to the console and thus it does not show up? However, changing my out.js script to console.log after a timeout of 1000ms also does not improve anything.

    Appreciate some more pointers what to look for 👍

    Update
    Upon further investigation it seems that if ELECTRON_RUN_AS_NODE is set to true the console.log methods are not patched but rather used as node.js provides them. In its basic form, console.log seems to translate to process.stdout.write().

    It seems to me that we need the same patching that happens for console.log calls for running inside Electron when running as node as long as we detect that a console is attached.

  20. bpasero commented on Sep 6, 2016

    @bpasero
    Contributor

    Updated with more findings.

    Btw I can reproduce this issue with Windows 7, so it seems to me a general Windows issue.

  21. changed the title [-]ELECTRON_RUN_AS_NODE not printing output to console on Windows 10[/-] [+]ELECTRON_RUN_AS_NODE not printing output to console on Windows[/+] on Sep 6, 2016
  22. bpasero commented on Sep 8, 2016

    @bpasero
    Contributor

    After spending more time on this I came to the conclusion that this issue is a problem with how node.js sets up stdout if running from Electron. When using console.log you are basically ending up callingprocess.stdout.write. stdout, stdin and stderr are being setup in stdio.js. There is code in place that checks for the kind of file descriptors of stdout, stdin and stderr:

    switch (tty_wrap.guessHandleType(fd)) {
        case 'TTY':
          const tty = require('tty');
          stream = new tty.WriteStream(fd);
          stream._type = 'tty';
          break;
    
        case 'FILE':
          const fs = require('fs');
          stream = new fs.SyncWriteStream(fd, { autoClose: false });
          stream._type = 'fs';
          break;
    
        case 'PIPE':
        case 'TCP':
          const net = require('net');
          stream = new net.Socket({
            fd: fd,
            readable: false,
            writable: true
          });
          stream._type = 'pipe';
          break;
    
        default:
          // Probably an error on in uv_guess_handle()
          throw new Error('Implement me. Unknown stream file type!');
      }

    When running from the command line you want to end up in the tty case. However, running node via electron, I end up in the default case and thus you end up with a dummy stream that does not produce anything useful.

    My theory is that because Electron is compiled as UI application, the file descriptors of stdout, stdin and stderr are invalid (or at least libuv is unable to validate them or detect their kind).

    I propose the following:

    I was hoping for a simpler fix and maybe someone else has more ideas. The fix is a bit ugly because it adds a new global method to process when running with ELECTRON_RUN_AS_NODE and it always just pipes output from stdout to cout, no matter what.

  23. zcbenz commented on Oct 17, 2016

    @zcbenz
    Contributor

    Closed with #7578.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions