I'm currently trying to create some errorformat rules to parse the output of the black formatter. The output of this formatter is as follows:

would reformat /home/ricks/Development/personal/errorformat/fmts/testdata/resources/python/num_guess.py
would reformat /home/ricks/Development/personal/errorformat/fmts/testdata/resources/python/subfolder/queen_problem.py
Oh no! 💥 💔 💥
256 files would be reformatted.

With the help of @filbranden (see this question) I designed the following errorformat syntax to parse this:

%-GOh no!%.%#
%-G%\d\+ file%.%#
%m %f

You can see this parsing in action on the following playground. After I asked the question I, however, noticed that the black formatter could also throw an error when it finds a syntax error:

error: cannot format /home/ricks/Development/personal/gh-actions/action-black/testdata/num_guess.py: Cannot parse: 12:6: test===jan
would reformat /home/ricks/Development/personal/gh-actions/action-black/testdata/subfolder/queen_problem.py
Oh no! 💥 💔 💥
1 file would be reformatted, 1 file would fail to reformat.

I am therefore trying to improve my errorformat rules such that it also parses the error message. I came up with the following error syntax to parse this error (see this syntax in action on the following playground):

%trror: %.%# %f: %.%#: %l:%c: %m

This, however, results in the error message that only contains the test===jan text where I want it to include any of the following:

  • The full error message: cannot format /home/ricks/Development/personal/gh-actions/action-black/testdata/num_guess.py: Cannot parse: 12:6: test===jan
  • A shorter error message: Cannot parse: test==jan.
  • A custom error message: Syntax error found: test==jan.

I also tried the following syntax %trror: %.%# %f: %m: %l:%c: %m but I can not get the relevant parts of the message together.

Judging from this stack question the biggest problem with parsing this error message is that the text I want to see in the message field of the quick-fix list is disconnected in the original output:

error: cannot format /home/user/repo/src/file1.py: Cannot parse: 2:5: some source code here
       ^^^^^^^^^^^^^                               ^^^^^^^^^^^^       ^^^^^^^^^^^^^^^^^^^^^

The question mentioned above suggests first pre-processing the black output (using regex) before passing it to the vim errorformatter. In my case, this is also possible but not easy. I was, therefore looking if there is a solution that doesn't require this pre-processing. I came up with the following solutions, but I can not get them to work:

  1. Find a way (maybe through regex) to combine the separated parts into one error message.
  2. Prase the filename(%f), line number (%l) and column number (%c) strings but also include them into the error message (%m).
  3. Prefix the error message with some additional text (e.g. A syntax error occured: : test===jan.

Solution 1

According to this StackOverflow question, this can be done using regex. I therefore first tried using the following errorformat syntax:

%trror: %\\%%(cannot\ format%\\)%\\@=%f: %.%#: %l:%c: %m

This, however, doesn't seem to work in the playground. This is because the playground (and thus the error parser I'm using) doesn't support the Vim Regex syntax (see this page).

Solution 2

I also did not find a way to match an expression but also include it into the error message. Judging for the solution above, this should also be possible using regex. As I could not get the regex solution to work with the playground, I also was not able to find the right syntax for also including the %f, %c, %l operators into the regex syntax.

Solution 3

I searched the forum, but I did not yet find a way to add some additional text to the error format message. I had something like this in mind:

%trror: %.%# %f: %m: %l:%c: %{A syntax error occurred:}m


My main question, therefore, is: Do you think it is possible to achieve what I have in mind (without using regex)? If not, I will try to implement the pre-processing solution. Thanks a lot in advance!

  • 1
    Not an answer to your question but an alternative approach that I take: black is meant to be a formatter, rather than a linter. I use flake8 to lint my code (in fact, every time I write the file it runs flake8 and displays errors/warnings in the statusline and location list). I only format right before committing. Depending on your use case, you could run flake8 or similar prior to running black. Jan 3, 2021 at 12:50
  • @AndrewHo-Lee Thanks for your comment. That's also what I'm using for my own actions. I'm however asking this question since I am trying to add the behaviour to an action that is used by multiple people. You, however, raise an excellent point. Since black is a formatter, and not a linter people generally do not expect to get very in-depth error messages. Catching the error as a fully parsed error message might therefore not be worth the effort. Nevertheless, since I'm curious to see if there is a solution I keep this question open a little longer.
    – rickstaa
    Jan 3, 2021 at 16:51


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.