bashhaskellcompiler-errorsghchaskell-stack

How do I get an error and warning count from GHC/Cabal/Stack?


If I run dotnet build in PowerShell to build a .NET project, at the end of the output, I see a message that tells me how many warnings and errors the compiler generated while building my project. I have attached an example below.

dotnet build output snippet indicating warning and error count

How can I get an error count with GHC? I am using stack, which uses cabal, which in turn uses GHC. A solution at any of those levels is fine.

I can get counts of the warnings or errors by piping the standard error of stack build into vim and using some search functionality in vim. However, I do not always get the correct results. When my source code or source file names contain the word "warning" or the word "error", the results of my search include false positives.

I would like a simple way to get the exact error and warning count. Perhaps, there is a command line flag or a simple wrapper that I can use? I looked through the man page for GHC and found no flag for error and/or warning counts.

I have read this similar stack overflow post for gcc. The answer to that question is not very encouraging, but I am curious if anyone else has some insight to share, and I am hoping GHC has a different story.

NOTE: I am using bash as my shell, so please gear any shell scripting towards bash.


Solution

  • You can use the -ddump-json flag to produce output as JSON and then use jq to filter based on severity. Finally, you have the good old wc -l for counting. This is how you get the number of errors, for example:

    ghc Main.hs -ddump-json | jq 'select(.severity == "SevError") .severity' | wc -l