ripgrep: `--pre` doesn't work on Windows (CMD, MSYS2 bash, Powershell)

Please tick this box to confirm you have reviewed the above.

  • I have a different issue.

What version of ripgrep are you using?

ripgrep 14.1.0 (rev e50df40a19)

How did you install ripgrep?

Download ripgrep-14.1.0-x86_64-pc-windows-msvc.zip from GitHub release page, unzip, add PATH.

What operating system are you using ripgrep on?

Windows 10

Describe your bug.

Read and run examples of GUIDE from top to bottom, everything is OK until the preprocess example: $ rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf.

What are the steps to reproduce the behavior?

  1. Download ripgrep-14.1.0-x86_64-pc-windows-msvc.zip from GitHub release page, unzip, add PATH.
  2. Download poppler-windows from https://github.com/oschwartz10612/poppler-windows/releases/, unzip, add PATH.
  3. Copy 1995-watson.pdf to C:/work2/rg-test-dir
  4. Following GUIDE, create preprocess file in C:/work2/rg-test-dir
    #!/bin/sh
    exec pdftotext - -
    
  5. Launch MSYS2 (no matter via mintty, windows legacy console or windows terminal)
  6. Ensure $PATH include
     /c/work2/rg-test-dir:/c/env/ripgrep/ripgrep-14.1.0-x86_64-pc-windows-msvc:/c/env/poppler/poppler-24.02.0/Library/bin
    
  7. cd /c/work2/rg-test-dir
  8. Run rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf
    $ rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf
    rg: 1995-watson.pdf: preprocessor command could not start: '"C:\\work2\\rg-test-dir\\./preprocess" "
    1995-watson.pdf"': %1 is not a valid Win32 application. (os error 193)
    

What is the actual behavior?

  • Run rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf
    $ rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf
    rg: 1995-watson.pdf: preprocessor command could not start: '"C:\\work2\\rg-test-dir\\./preprocess" "
    1995-watson.pdf"': %1 is not a valid Win32 application. (os error 193)
    
  • Run rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf --debug
    $ rg --pre ./preprocess 'The Commentz-Walter algorithm' 1995-watson.pdf --debug
    rg: DEBUG|rg::flags::parse|crates/core\flags\parse.rs:97: no extra arguments found from configuration file
    rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1260: found hostname for hyperlink configuration: DESKTOP-OM1MD9D
    rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:1270: hyperlink format: ""
    rg: DEBUG|rg::flags::hiargs|crates/core\flags\hiargs.rs:174: using 1 thread(s)
    rg: DEBUG|grep_cli::decompress|crates\cli\src\decompress.rs:502: lz4: could not find executable in PATH
    rg: DEBUG|globset|crates\globset\src\lib.rs:453: built glob set; 0 literals, 0 basenames, 11 extensions, 0 prefixes, 0 suffixes, 0 required extensions, 0 regexes
    rg: 1995-watson.pdf: preprocessor command could not start: '"C:\\work2\\rg-test-dir\\./preprocess" "1995-watson.pdf"': %1 is not a valid Win32 application. (os error 193)
    

P.S.

  1. If I do not set the working directory /c/work2/rg-test-dir, it will report

     rg: ./preprocess: could not find executable in PATH
    

    This is a bit weird, although GUIDE says preprocess must be put in PATH.

  2. I also tried CMD and Powershell by the following preprocess scripts, none of them works (and with the same error).

    preprocess.cmd

    @echo off
    pdftotext %1 -
    

    preprocess.ps1

    $PSDefaultParameterValues['*:Encoding'] = 'utf8'
    Start-Process -NoNewWindow -Wait -FilePath "pdftotext.exe" -ArgumentList $Args[0], "-"
    

Thanks.

What is the expected behavior?

No error.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 16 (8 by maintainers)

Most upvoted comments

@ltrzesniewski Thank you!!!

Ok maybe I could help a bit 🙂

Windows has essentially two functions to start processes: CreateProcess and ShellExecuteEx.

The first one is the low-level API which can “only” run .exe and .bat files, while the second one is the higher-level shell API which is able to map file extensions such as .ps1 to their executables such as powershell.exe. Their behvior can be very different depending on what you’re trying to do, but Rust uses CreateProcess.

The following has been added recently to the Rust process module docs:

//! On Windows, `Command` uses the Windows API function [`CreateProcessW`] to
//! spawn new processes. An undocumented feature of this function is that,
//! when given a `.bat` file as the application to run, it will automatically
//! convert that into running `cmd.exe /c` with the bat file as the next argument.
//!
//! For historical reasons Rust currently preserves this behaviour when using
//! [`Command::new`], and escapes the arguments according to `cmd.exe` rules.
//! Due to the complexity of `cmd.exe` argument handling, it might not be
//! possible to safely escape some special chars, and using them will result
//! in an error being returned at process spawn. The set of unescapeable
//! special chars might change between releases.
//!
//! Also note that running `.bat` scripts in this way may be removed in the
//! future and so should not be relied upon.

I tested it quickly, and ripgrep is able to run a .bat file with --pre but not a .ps1 script (as that would require ShellExecuteEx, which in any case doesn’t support output redirecting so it’s a no-go).

Here’s C:\Temp\test.bat:

@echo off
echo Hello, world!

Here’s C:\Temp\test.ps1:

Write-Output "Hello, world!"

And there’s also an empty file in C:\Temp\empty.txt, just to show that the searched file content is irrelevent.

Here are the results:

rg --no-config --pre C:\Temp\test.bat Hello C:\Temp\empty.txt
1:Hello, world!

rg --no-config --pre C:\Temp\test.ps1 Hello C:\Temp\empty.txt
rg: C:\Temp\empty.txt: preprocessor command could not start: '"C:\\Temp\\test.ps1" "C:\\Temp\\empty.txt"': %1 is not a valid Win32 application. (os error 193)

So you should be able to get the output from a .bat file just fine. In case it’s relevant, I compiled my ripgrep with Rust 1.78.0, which includes changes to Command under Windows, so that may play a role in the results.