Skip to content

Add NODE_DEBUG support for child_process #720

Description

@remixz

It would be super useful to have NODE_DEBUG support for child_process. The use-case that I'd have for this would be to track down when a command can't be found, since right now, all that's returned is Error: spawn ENOENT. It'd be nice to have what command and arguments it was trying to run.

Example:

var spawn = require('child_process').spawn

var child = spawn('fdjslkdf')

child.on('error', function (err) {
  throw err
})

Current output:

test.js:6
  throw err;
        ^
Error: spawn ENOENT
    at errnoException (child_process.js:1011:11)
    at Process.ChildProcess._handle.onexit (child_process.js:802:34)

Idea of output:

child_process: spawned `fdjslkdf` with arguments []
test.js:6
  throw err;
        ^
Error: spawn ENOENT
    at errnoException (child_process.js:1011:11)
    at Process.ChildProcess._handle.onexit (child_process.js:802:34)

Activity

  1. bmeck commented on Feb 5, 2015

    @bmeck
    Member

    I think going and having the error include the cmd during onexit would be sane as well.

  2. trevnorris commented on Feb 5, 2015

    @trevnorris
    Contributor

    That information is already included with the error:

    var spawn = require('child_process').spawn;
    
    var child = spawn('fdjslkdf');
    
    child.on('error', function (err) {
      console.error(err.syscall);
      console.error(err.path);
      throw err;
    });

    Output:

    $ ./iojs /tmp/cp-test.js 
    spawn fdjslkdf
    fdjslkdf
    /tmp/cp-test.js:8
      throw err;
            ^
    Error: spawn fdjslkdf ENOENT
    
  3. trevnorris commented on Feb 5, 2015

    @trevnorris
    Contributor

    What does seem to be missing is the list of arguments. I wouldn't mind an additional field being added for that.

  4. trevnorris commented on Feb 5, 2015

    @trevnorris
    Contributor

    Also. Looks like you aren't running the absolute latest. As of bc2c85c some of the information you're requesting has already been included.

  5. added
    fsIssues and PRs related to file-system APIs and the fs module.
    and removed
    fsIssues and PRs related to file-system APIs and the fs module.
    on Feb 5, 2015
  6. remixz commented on Feb 5, 2015

    @remixz
    ContributorAuthor

    Ah, shoot... I was running this with joyent/node, not io.js. My bad.

  7. trevnorris commented on Feb 5, 2015

    @trevnorris
    Contributor

    np. And if you want to PR to add a field to err that includes the arguments passed to spawn() you're welcome to.

  8. bmeck commented on Feb 5, 2015

    @bmeck
    Member

    it is only in errors, not for when things get spawned (which is useful anyway)

  9. remixz commented on Feb 5, 2015

    @remixz
    ContributorAuthor

    @trevnorris @bmeck How do you feel about this?

    var spawn = require('child_process').spawn;
    var path = require('path');
    
    var child = spawn('fdjslkdf', ['--foo', 'bar'], {
      cwd: path.join(process.cwd(), './bat')
    });
    
    child.on('error', function (err) {
      throw err;
    });
    $ NODE_DEBUG=child_process ./iojs tmp.js
    CHILD_PROCESS 72525: command: fdjslkdf
    CHILD_PROCESS 72525: arguments: --foo,bar
    CHILD_PROCESS 72525: options: {"cwd":"/Users/zach/Documents/github/remixz/io.js/bat"}
    /Users/zach/Documents/github/remixz/io.js/tmp.js:9
      throw err;
            ^
    Error: spawn fdjslkdf --foo bar ENOENT
        at exports._errnoException (util.js:738:11)
        at Process.ChildProcess._handle.onexit (child_process.js:1029:32)
        at child_process.js:1120:20
        at process._tickCallback (node.js:337:11)
        at Function.Module.runMain (module.js:489:11)
        at startup (node.js:111:16)
        at node.js:799:3
    

    It adds the arguments to the error, and adds the debug info. If you like it, I'll send a PR!

  10. cjihrig commented on Feb 5, 2015

    @cjihrig
    Contributor

    I have a small concern about adding the arguments to the error message, as it could get long in some cases. If it was just another property on the error object, that would be great.

    EDIT: I think a PR would be good.

  11. mscdex commented on Feb 5, 2015

    @mscdex
    Contributor

    +1 to adding them to the error object but not in the message

  12. cjihrig commented on Feb 5, 2015

    @cjihrig
    Contributor

    Closed in 9a8f186

  13. bmeck commented on Feb 5, 2015

    @bmeck
    Member

    Since you have to enable debug I think leaving it in is ideal, since it is during debugging, when you want a plethora of information.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    child_processIssues and PRs related to the child_process subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions