ggreer / the_silver_searcher

A code-searching tool similar to ack, but faster.
http://geoff.greer.fm/ag/
Apache License 2.0
26.18k stars 1.43k forks source link

Windows CRCRLF newlines with redirected ag output #1263

Open segevfiner opened 6 years ago

segevfiner commented 6 years ago
C:\test>ag hello | "c:\Program Files\Git\usr\bin\xxd.exe"
00000000: 6865 6c6c 6f2e 7478 743a 313a 4865 6c6c  hello.txt:1:Hell
00000010: 6f2c 2057 6f72 6c64 2120 0d0d 0a         o, World! ...

I'm using this build ag-2018-08-08_2.2.0-2-gbd82cd3-x64.zip.

This is probably due to using \r\n for a line ending. Windows translates \n to \r\n on output to stdio in text mode. Just use \n when using stdio on Windows. (Powershell also translates the lone \r to \r\n so there you get doubled newlines).

segevfiner commented 6 years ago

The "\r\n" is from the searched file that has CRLF line endings. The matched line is just printed directly to the screen including the "\r\n". I think this is wrong even on Unix platforms when you are writing to a tty... Although you are a lot less likely to encounter CRLF there.

One way would be to avoid printing "\r\n", converting that to "\n" on output. It is also possible on Windows to disable "\r\n" using _setmode, but I'm not sure whether that's a good idea. We will get files with NL line endings like that, and I'm not sure if the Windows console requires "\r\n" or can handle just "\n".

P.S. The following always adds -O2 if there is no "-O" flag, that's kinda surprising for those expecting standard autoconf behavior. The comment should probably say that you need to add "-O0" to disable optimizations for debugging.

https://github.com/ggreer/the_silver_searcher/blob/bd82cd3d2f8fec2bef6037e697b3d303a90ee765/configure.ac#L27-L30

JFLarvoire commented 5 years ago

Hi, Try using this build of ag for Windows, which does not have this problem: https://github.com/JFLarvoire/the_silver_searcher/releases Jean-François

nebbish commented 3 years ago

I have loved 'ag' on linux & mac. When I tried to start using it in a Windows dev environment I am building up... I want to install Ag with chocolatey.

Unfortunately, the version installed by Chocolatey is still showing the same output problem today in April of 2021.

C:\Users\username>ag --version
ag version 2.1.0

Features:
  +jit +lzma +zlib

C:\Users\username>which ag
C:\ProgramData\chocolatey\bin\ag.exe

C:\Users\username>

I have not setup a WIndows dev box in a long while, and I was excited to learn that all my favorite dev tools are available in Chcolatey :D

However, the "current" Chocolatey delivered 'Ag' has got this problem and it is killing my VIM user experience on Windows because I use 'Ag' as my search tool within Vim.

In my case, Vim is not completely choking on the extra '0d' characters, instead.. the 'quickfix' window is loaded with results that have the '^M' character at the end of each line -- this is damaging the ability to parse line/column numbers out of the output for jumping around.

JFLarvoire commented 3 years ago

Hi Jason,

The ag version in Chocolatey is just the Linux version recompiled for Windows, and it does not behave well in Windows. (To say the least.) I recommend that you install my build of ag optimized for Windows using the Microsoft winget package manager:

1) If you don't already have it, get the latest winget from https://github.com/microsoft/winget-cli/releases 2) Run winget install "The Silver Searcher"

Jean-François