2

I am trying to use ripgrep in node.js application running on my mac machine. ( VS Code Extension).

I tried ripgrep-js npm libray but it was not giving any response and application was just going to hang state.

I tried to debug and found that ripgrep-js is just running the rg command and grabbing its results and giving it back in a formatted way. I tried the running the simple command like

rg test .

I got the results.

But same thing which when I am running via node.js child_process its not giving any response. Here is the code which I am running.

// index.js
const cp = require('child_process');


let p = cp.spawn('rg', ['test'], { cwd: './'});
p.stdout.on('data', data => {
    console.log('stdout: ' + data.toString());
});

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

p.on('close', code => {
    console.log('closed: ' + code);
});

Not sure what could be the cause of it. I came around this question child_process.spawn doesn't emit any events which kind of matches with my use case where the ripgrep was not working. But it is not very clear what exactly was done to resolve the issue. Reaching out to community for the help.

In order to reproduce this, please create the index.js with above code and run with node as node index.js

Kiran Machhewar
  • 234
  • 2
  • 9
  • If ripgrep detects stdin and you haven't provided any paths to search, then ripgrep will try to search stdin. It's likely that whatever Node thing you're using is attaching something to stdin that makes it look like there's something there to search. So ripgrep does just that, and since there isn't actually anything there, it just hangs forever waiting. This is why `rg foo ./` works: because you're giving it a path to search. Bottom line, either use `rg foo ./` (why doesn't that work for you?) or tell Node to not spawn a process with stdin attached. – BurntSushi5 Aug 27 '21 at 10:23
  • @BurntSushi5 Thank you so much for reply. I passed the cwd as ./ , absolute path but still no success. But I was able to detach the stdin which is working stdio: ['ignore', 'pipe', 'pipe'] I am a bit curious to know how can we fix this by passing the cwd which I think I am passing ( if I am not missing anything ). Atleast for time being I have something to lean on, thank you for that. – Kiran Machhewar Aug 27 '21 at 16:10
  • This looks relevant: https://github.com/BurntSushi/ripgrep/discussions/1979 FWIW, "passing the CWD" is ambiguous to me. I can't tell if you mean you're setting the CWD at the Node process level, or if you're actually running `rg foo ./`. If the latter, then it should work and it's unclear why it wouldn't. For me to diagnose it, I'd need a complete reproduction, including showing me how to run it if it's from Node. – BurntSushi5 Aug 27 '21 at 17:31
  • @BurntSushi5 Updated question to reproduce this issue. Atleast as of now I was able to solve this error by updating one parameter to { cwd: './', stdio: ['ignore', 'pipe', 'pipe'] } basically ignoring the stdin. If you want to reproduce and see how ./ could solve the issue then it would be great. Otherwise I can post the answer which is working now. – Kiran Machhewar Aug 30 '21 at 07:11
  • Thanks for the reproduction. It works just fine when I pass `./`. All I did was change `['test']` to `['test', './']`. – BurntSushi5 Aug 30 '21 at 11:27

1 Answers1

4

Thank you @BurntSushi5 who helped to solve this question. To summarise we have two options to solve the problem.

Solutin 1: Ignore the stdin

    const cp = require('child_process');
    let p = cp.spawn('rg', ['test'], { cwd: './'
    ,stdio: ['ignore', 'pipe', 'pipe']
    });
    p.stdout.on('data', data => {
        console.log('stdout: ' + data.toString());
    });

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

    p.on('close', code => {
        console.log('closed: ' + code);
    }); 

Solution 2: Pass the ./

    const cp = require('child_process');
    let p = cp.spawn('rg', ['test','./'], { cwd: '.'});
    p.stdout.on('data', data => {
        console.log('stdout: ' + data.toString());
    });

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

    p.on('close', code => {
        console.log('closed: ' + code);
    }); 
Kiran Machhewar
  • 234
  • 2
  • 9