python-lsp/python-lsp-ruff

Why formatting fixes things I don't ask to fix?

Onturenio opened this issue · 3 comments

Hi, I'm fairly unfamiliar with ruff and formatting in general, and so I'm trying to understand how to control its behaviour.

I have this configuration:

"pylsp.plugins.ruff.enabled": true,
"pylsp.plugins.ruff.extendSelect": ["E", "D", "C90", "I"],
"pylsp.plugins.ruff.ignore": ["E265"],
"pylsp.plugins.ruff.format": ["D"],
"pylsp.plugins.ruff.preview": true,
"pylsp.plugins.ruff.lineLength": 79,

Then, I introduce this typo: a = 1. This gets correctly signaled by ruff with E221 (this code requires preview). However, when I run the command :call CocAction('format') this error gets fixed. This is not what I expect, the error should not be fixed because this family of errors is not explicitly indicated in the ruff.format option. This does seem to work for I errors, though.

Am I doing anything wrong or is this the expected behaviour? To be honest, I'm not even sure if :call CocAction('format') is the correct way to invoke ruff format.

Thanks for the the help and more generally for this great plugin!

Hey @Onturenio, thanks a lot for this question, this is fixed when ruff format is invoked. We basically do this currently when invoking the format request: ruff format .; ruff check --select <format codes> --fix.
I tried to explain this in https://github.com/python-lsp/python-lsp-ruff#code-formatting, but if you think this is not clear enough I could highlight that the format option only specified additional errors to be fixed. Let me know what you think.

Thanks for the reply. The documentation was OK, it was mostly my fault for not having a clear idea of the difference between ruff check --fix and ruff format.

By the way, is there a way to run just one of the two? Or in other words, is :call CocAction('format') the only way to invoke the fixing tools? Perhaps it would be worth to let the user decide if he/she wants to invoke just one of the tools, either chech --fix or format right? Please this is not a complain, this is a honest (and maybe dumb?) question.

In theory, you could enable all fixable errors with the select option and run the "Fix all" action to fix those. Is there any reason why you are specifically unhappy about the formatting with ruff? Maybe as opposed to black?