Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ripgrep returns exit code 1 with no stdout or stderr, can't work out why #190

Closed
dracan opened this issue Jan 16, 2023 · 3 comments
Closed
Labels

Comments

@dracan
Copy link

dracan commented Jan 16, 2023

Version

3.6.0

Details

I'm trying to use this to run Ripgrep, but it always returned errorcode 1 with nothing in the stdout. Not sure why.

Steps to reproduce

Here's an example...

image

On the left, I'm trying to run rg rabbit, and it fails with no reason. On the right, if I use rg --help, this does work.

Running this natively on the command line...

image

So I think that's proving that rg rabbit is writing to the stdout stream. But I can't see why it would fail when running the same command through CliWrap.

Code from screenshot is here:

var result = await Cli.Wrap("rg")
    .WithArguments(new[] {
        "rabbit",
    }, false)
    .WithWorkingDirectory(@"C:\Dump\GrepTest\")
    .WithValidation(CommandResultValidation.None)
    .ExecuteBufferedAsync();

result.Dump()

Ps. just want to also say thank you for the awesome project! I can't believe it's so difficult to do this kind of stuff in .NET natively! Very grateful of you spending the time to make this easier! 🙏🏼

@dracan dracan added the bug label Jan 16, 2023
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jan 17, 2023

Hi Dan.

This appears to be happening because rg notices that the standard input stream is redirected and assumes you're passing data to it. This causes it to switch to an alternative behavior, where it searches inside the stdin data, instead of the working directory.

image

You can reproduce this behavior in the terminal like so:

c:\Users\Tyrrr\Downloads\1>echo "foo rabbit bar" | rg rabbit
"foo rabbit bar"

CliWrap always redirects all standard streams and handles piping at a separate layer, so that's why the stdin is redirected even though you didn't pipe anything there. If the stdin is empty (which is the default), rg errors out with exit code 1. It's rather unfortunate that the tool doesn't provide more info in the stderr though.

Anyway, to fix this, you need to override rg's behavior by explicitly telling it to search in the current working directory. You can do that by passing . as the second argument:

var result = await Cli.Wrap("rg")
    .WithArguments(new[] {
        "rabbit",
        "."
    }, false)
    .WithWorkingDirectory(@"C:\Dump\GrepTest\")
    .WithValidation(CommandResultValidation.None)
    .ExecuteBufferedAsync();

result.Dump()

image

@Tyrrrz Tyrrrz added question and removed bug labels Jan 17, 2023
@Tyrrrz Tyrrrz changed the title When running Ripgrep - it returns errorcode 1 with no stdout or stderr. Can't work out why. Ripgrep returns exit code 1 with no stdout or stderr, can't work out why Jan 17, 2023
@dracan
Copy link
Author

dracan commented Jan 17, 2023

HI @Tyrrrz. Thank you so much for looking into that, and for your very detailed reply! Especially as it involved a different tool that you're not involved in 🙏🏼
Good to know for future reference that CliWrap is always redirecting input streams though - as I guess Ripgrep probably isn't going to be the only tool that does similar (I know of a few that act differently when being piped to).
Thanks again for the awesome library 🙂

@dracan dracan closed this as completed Jan 17, 2023
@Tyrrrz
Copy link
Owner

Tyrrrz commented Jan 17, 2023

HI @Tyrrrz. Thank you so much for looking into that, and for your very detailed reply! Especially as it involved a different tool that you're not involved in 🙏🏼 Good to know for future reference that CliWrap is always redirecting input streams though - as I guess Ripgrep probably isn't going to be the only tool that does similar (I know of a few that act differently when being piped to). Thanks again for the awesome library 🙂

You're welcome!

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

No branches or pull requests

2 participants