You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ELECTRON_RUN_AS_NODE not printing output to console on Windows #5715
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
Reacted by Artur Parkhisenko, Glen, BijoC, Amin Ya and Glenn 'devalias' Grant
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.
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.
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
@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.
Reacted by Benjamin Pasero, Abhinav Gauniyal, Wayne Yang, 唐焱鑫, Robert Konrad, Brunno Pleffken Hosti, Daniel Derevjanik, Will Prater, bootstraponline, Kalin Krustev and 1 more
@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?
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.
@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.
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.
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
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':
consttty=require('tty');stream=newtty.WriteStream(fd);stream._type='tty';break;case'FILE':
constfs=require('fs');stream=newfs.SyncWriteStream(fd,{autoClose: false});stream._type='fs';break;case'PIPE':
case'TCP':
constnet=require('net');stream=newnet.Socket({fd: fd,readable: false,writable: true});stream._type='pipe';break;default:
// Probably an error on in uv_guess_handle()thrownewError('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 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.
Activity
zcbenz commentedon May 29, 2016
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.
jviotti commentedon May 30, 2016
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.
jviotti commentedon May 30, 2016
Actually, v0.37.8 seems to work fine. I'll try to bisect to find the reason.
jviotti commentedon May 30, 2016
It was introduced in v1.1.0. I'll try to identify the first bad commit once I get VS 2015 here.
zcbenz commentedon May 31, 2016
@jviotti Thanks for locating the version, the exception happens when creating process.stdin, it seems to be introduced by the upgrade of Node.
jviotti commentedon May 31, 2016
Awesome, thanks a lot for the quick fix!
Sent from my iPhone
jviotti commentedon Jun 6, 2016
Hi @zcbenz ,
I've just tested v1.2.1 and I still can reproduce the issue. Thoughts?
zcbenz commentedon Jun 7, 2016
@jviotti You can not get Node prompt because stdin is not working, you can find explanations in #5776. But normal Node scripts should work.
jviotti commentedon Jun 7, 2016
@zcbenz I'm trying to run a normal NodeJS script with it, and still get no output:
zcbenz commentedon Jun 8, 2016
It seems like a Windows 10 only problem. The node script actually works, it is just the output not printed to console.
[-]ELECTRON_RUN_AS_NODE not working on Windows 10[/-][+]ELECTRON_RUN_AS_NODE not printing output to console on Windows 10[/+]bpasero commentedon Jul 8, 2016
We also see no output on Windows 10 with Electron 1.2.6. Works fine in 0.37.6.
bpasero commentedon Aug 4, 2016
@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.
zcbenz commentedon Aug 5, 2016
@bpasero I'll take a look.
bpasero commentedon Aug 30, 2016
@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/DI can see the output but not if I run it fromout/R, any clues?bpasero commentedon Sep 1, 2016
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.
bpasero commentedon Sep 3, 2016
@zcbenz played around a little bit and now I am facing some questions. I have a simple
out.jsfile which doesconsole.log("Hello World")and my test case is to runelectron.exe out.jswhileELECTRON_RUN_AS_NODEis set totrue.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:
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.logfrom node.js is being rewritten to callprocess.log(from here: https://github.com/electron/electron/blob/master/lib/browser/init.js#L27) which in the end callsstd::cout << message << std::flushin https://github.com/electron/electron/blob/master/atom/common/api/atom_bindings.cc#L83But 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.jsscript toconsole.logafter 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_NODEis set totruetheconsole.logmethods are not patched but rather used as node.js provides them. In its basic form,console.logseems to translate toprocess.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.
bpasero commentedon Sep 6, 2016
Updated with more findings.
Btw I can reproduce this issue with Windows 7, so it seems to me a general Windows issue.
[-]ELECTRON_RUN_AS_NODE not printing output to console on Windows 10[/-][+]ELECTRON_RUN_AS_NODE not printing output to console on Windows[/+]bpasero commentedon Sep 8, 2016
After spending more time on this I came to the conclusion that this issue is a problem with how node.js sets up
stdoutif running from Electron. When usingconsole.logyou are basically ending up callingprocess.stdout.write.stdout,stdinandstderrare being setup in stdio.js. There is code in place that checks for the kind of file descriptors ofstdout,stdinandstderr:When running from the command line you want to end up in the
ttycase. However, running node via electron, I end up in thedefaultcase 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,stdinandstderrare invalid (or at least libuv is unable to validate them or detect their kind).I propose the following:
console.logends up inprocess.log(which is a simplecout << message << flush).process.logwithinnode_main.ccsimilar to how its done in (https://github.com/electron/electron/blob/master/atom/common/api/atom_bindings.cc#L104)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_NODEand it always just pipes output from stdout tocout, no matter what.zcbenz commentedon Oct 17, 2016
Closed with #7578.