1

I'm trying to run ripgrep from my Node app and am seeing a strange behavior with child_process.spawn: none of the events fire and the app never finishes (is stuck somewhere inside the spawn call):

import { spawn } from 'child_process';

async function run() {
    await spawnWrapper('rg', ['-F', '"demo"'], { cwd: __dirname });
}

export function spawnWrapper(command, args, options) {
    return new Promise((resolve, reject) => {
        let stdout = '';
        let stderr = '';
        const child = spawn(command, args, options);
        console.log('spawn wrapper');

        child.on('close', (code, signal) => {
            console.log('close');
            resolve({ code, signal, stdout, stderr });
        });

        child.on('error', (error) => {
            console.log('error');
            (error as any).stderr = stderr;
            reject(error);
        });

        child.on('exit', (code, signal) => {
            console.log('exit');
            resolve({ code, signal, stdout, stderr });
        });

        child.stdout.setEncoding('utf8');
        child.stderr.setEncoding('utf8');

        child.stdout.on('data', (data) => {
            console.log('stdout data');
            stdout += data;
        });

        child.stderr.on('data', (data) => {
            console.log('stderr data');
            stderr += data;
        });
    });
}

I only get "spawn wrapper" in the console, no other events. I've never seen this behavior with other binaries, maybe it's something with ripgrep but still, shouldn't I be getting at least some hints by Node? Any suggestions on how to debug this?

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240
  • You can use a local debugger and set it to break on all JS exceptions, it should show you what fails the code then. – vfioox Oct 15 '17 at 15:25
  • https://gist.github.com/Stuk/6226938 this example wrapper around child_process doesn't utilize the close event, perhaps it's not necessary? Here's another example of such class https://github.com/mgenware/promised-spawn – vfioox Oct 15 '17 at 15:27

1 Answers1

0

It was caused by ripgrep waiting for input which was not obvious to me (on command line, it just executes straight away). Details here: https://github.com/BurntSushi/ripgrep/issues/410

Borek Bernard
  • 50,745
  • 59
  • 165
  • 240