12

This is the code to execute



    cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) {
        if (e) {
            var errorstr = "Compilation failed with the following error
"+ e.message.toString() client.send(errorstr) console.log(e, stdout, stderr) ee.prototype.removeAllListeners() } else if (stderr.length > 0) { client.send("Compilion finished with warnings\n"+ stderr + '\n') client.send('compiled') ee.prototype.emit('compiled') } else { client.send("Compilation successful") ee.prototype.emit('compiled') } })

'client' is the argument of socket.io's callback argument. 'ee' is an instance of EventEmitter

Coming to the problem. On running the code, the callback says that the command was unsuccessful. console.log(e, stdout, stderr) is

{ [Error: Command failed: ] killed: false, code: false, signal: undefined } '' ''

/tmp/test.c is a valid C code and on checking the directory /tmp , I find that test.c is proper and the binary 'test' is being generated and on running in a shell, is properly executed. So I dont understand why it is flagging unsuccessful execution. The error object's information is unhelpful too. Would appreciate some help/explanation

Shrikrishna Holla
  • 256
  • 1
  • 2
  • 11

1 Answers1

9

I'm a bit worried about the output in the console.

{ [Error: Command failed: ] killed: false, code: false, signal: undefined }

doesn't look like a proper JSON/JavaScript object, especially the [Error: Command failed: ] part; there is at least a comma missing.

Suggestions:

  1. Run the command from the command line and check the exit code (use echo $?). If the exit code is != 0, then this means the command "failed" (whatever that might mean).

  2. When the command fails, nodejs says it will put the exit code into e.code (which I'm missing in your output...). Find out what happened to this value.

  3. Try if(e !== null) instead of if(e). Shouldn't make a difference, though.

  4. Instead of calling the compiler directly, call a shell script which redirects stderr/stdout to a file (or save a copy using cc ... |& tee /tmp/cc.log) to make sure no part of the complex setup swallows important information.

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • (1) I did as you suggested and the exit code is 0 (2) Well, the entire error object is output here. so e.code will be false (yes, _this_ stumped me the most) (3) Nope. No difference (4) The thing is, I don't _need_ the stdout of this operation as mostly, stdout and stderr will be null as this is valid C code – Shrikrishna Holla Jan 14 '13 at 14:14
  • @ShrikrishnaHolla what's the version of your node? – xiaoyi Jan 14 '13 at 14:16
  • @xiaoyi: v0.9.6-pre (I recently cloned it from github) – Shrikrishna Holla Jan 14 '13 at 14:17
  • (2) sounds like you should file a bug report with a test case. Also, since you have the source code, try to find the place which implements `exec()` and check what it really does. Maybe the documentation is simply outdated and `if(e.code)` would be correct, now. – Aaron Digulla Jan 14 '13 at 14:20
  • @ShrikrishnaHolla Have you tried the same code with a stable version? say 0.8.17? – xiaoyi Jan 14 '13 at 14:24
  • @ShrikrishnaHolla: Your code looks correct and should work according to the documentation and all examples I've seen on the 'net so far. – Aaron Digulla Jan 14 '13 at 14:28
  • Thank you, everyone. As @AaronDigulla suggested, I will go through the source code and file a bug report, if necessary. – Shrikrishna Holla Jan 14 '13 at 14:29
  • Wierd. I checked out the branch v0.8.17-release because @xiaoyi said it was the most recent stable version. The bug persists! :-| – Shrikrishna Holla Jan 14 '13 at 14:45
  • I have submitted an issue. The id is [4590](https://github.com/joyent/node/issues/4590) – Shrikrishna Holla Jan 14 '13 at 15:09