Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

nodejs exec command failing with no useful error message #4590

Closed
shrikrishnaholla opened this issue Jan 14, 2013 · 6 comments
Closed

nodejs exec command failing with no useful error message #4590

shrikrishnaholla opened this issue Jan 14, 2013 · 6 comments

Comments

@shrikrishnaholla
Copy link

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 (echo $? of the same command in the shell returns 0). So I dont understand why it is flagging unsuccessful execution. The error object's information is unhelpful too.

But the most disturbing thing about this is e.code, whose value is 'false', something that should be an 8-bit integer, not boolean. I have checked this example with the branches 'v0.9.6-pre' and 'v0.8.17-release'

(This issue was discussed in StackOverFlow )

@bnoordhuis
Copy link
Member

Sorry, can't reproduce. Your test case doesn't run standalone but when I modify it like this:

var cp = require('child_process');
cp.exec("cc -Wall /tmp/test.c -o /tmp/test", function(e, stdout, stderr) {
  console.log(stdout);
  console.log(stderr);
  if (e) throw e;
});

Then it produces the following output:

$ echo 'main(){}' > /tmp/test.c
$ ../v0.8/out/Release/node tmp/issue4590.js

/tmp/test.c:1:1: warning: return type defaults to ‘int’ [-Wreturn-type]
/tmp/test.c: In function ‘main’:
/tmp/test.c:1:1: warning: control reaches end of non-void function [-Wreturn-type]

$ ../master/out/Release/node tmp/issue4590.js

/tmp/test.c:1:1: warning: return type defaults to ‘int’ [-Wreturn-type]
/tmp/test.c: In function ‘main’:
/tmp/test.c:1:1: warning: control reaches end of non-void function [-Wreturn-type]

@isaacs
Copy link

isaacs commented Jan 16, 2013

What is ee.prototype.removeAllListeners()? I think you might be confused about how classes in JavaScript work.

@shrikrishnaholla
Copy link
Author

Well, that was a mistake, yes. But correcting that didn't affect the results

@jgillich
Copy link

I just had the same issue (assuming this is just about the weird error message that is Command failed:).

Windows 7 64 bit, Node 0.10.29. Initially with a CLR executeable, but also reproduceable with node itself

// child.js:
process.exit(2);

// main.js:
var child_process = require('child_process');
var child = child_process.exec('node child.js', function (error, stdout, stderr) {
    if(error) console.log(error);
});
$ node main.js
{ [Error: Command failed: ] killed: false, code: 2, signal: null }

@0xAshish
Copy link

0xAshish commented Jan 2, 2017

having same problem
nodejs code
var exec=require("child_process").exec;
//var execF=require("child_process").execFile;

var cmd="gcc a.c -o code";
exec(cmd,(err,stdout,stderr)=>{
if(!stderr){
console.log("output:");
cmd ="./code"
exec(cmd,null,(err,stdout,stderr)=>{
if(!err){
console.log(stdout);
}else{
console.log(err+" output "+stdout);
}
})
}else{
console.log(stderr);
}
})

and c code and out put
#include<stdio.h>
void main(){
printf("hello world!!");
}
output:
output:
Error: Command failed: /bin/sh -c ./code
output hello world!!
i don't know why it returns error when is has that output/stdout
Node version 4.2.6

@njtrettel
Copy link

njtrettel commented Mar 24, 2017

Same as well, even with a simple diff. Error code 1

exec('diff '+fname+' '+compname, function(error, stdout) {
    if(error) {
        console.log(error);
        callback(error);
        return;
    }
    console.log("difference: "+stdout);
    callback(stdout);
});

and error message:

Error: Command failed: diff myFile.txt compFile.txt

The files definitely exist. Command works fine from terminal.

EDIT:
This is definitely a bug with the code. Even though I got an 'error', the correct result is still passed in the stdout parameter. I did not realize this because I return after catching the error. Also for those that have the following in their code (I am keeping it in mine for reference): if(!err) or if(err), you need to instead check the code field: if(!err.code). However, I bypass the error altogether because I believe it is broken; I am always getting an err.code as 2.

In short, this code is reporting false errors.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants