Getting a list of file with an error from cargo-build?

Hey all, I usually use vim as my code editor. Is there a way to easily make cargo build output a list of files that have errors so that I can just feed it into another CLI program? I am not seeing anything obvious in cargo build --help.

As an example, in the same way with grep I can do grep 'fn run' -rl * | vim, I would like to be able to do cargo build --error-file-list | vim to open all of the files that have errors in vim so I can work through them sequentially.

In the past, I have played with grep to get files from the output of other files, but it would be nice to avoid that if possible.

Thank you in advance!

I don't think there's such an option, but we can do better than grep with regex:

since cargo can output messages in json format, we can use jq to filter the file paths, and the precise location of the error (the error span contains both the byte offset and line/column information).

I don't remember the exact message format of cargo, but you can give a chat bot an example of a cargo error message and the result you want, it will give you the jq filter.

Thanks, I'll give it a go later. I don't allow any AI commits on my projects for reasons of code can't be guaranteed not be taking from another project without attribution.

well, you don't have to use an LLM, you can write the jq query manually, or you can even write a custom tool if you don't want to use jq.

the json schema of cargo output is available in the cargo-metadata crate. compile errors are defined in the Diagnostics struct, and the source file info (path, row, column, etc) is in the spans fields. since there may be multiple errors from a single source, rememeber to dedup the file paths.

I just can't remember the jq syntax since I don't use jq that often, so when I need it, I usually ask a chatbot, except for very simple cases.