ruff-pycharm-plugin icon indicating copy to clipboard operation
ruff-pycharm-plugin copied to clipboard

Can't undo ruff autoformat (or ruff check --fix on 'save file')

Open glarione opened this issue 2 years ago • 16 comments

Describe the bug When I make some fixes to code and if settings like on picture, if I want to undo

To Reproduce Steps to reproduce the behavior:

  1. Go to some file and add a few empty rows (for example).
  2. Push Ctrl+Alt+L (or save file if this setting is checked).
  3. Write some text.
  4. Try to undo changes till before p.1.

Expected behavior I can undo fixes by Ruff.

Screenshots image

image

Environments (please complete the following information):

  • IDE: [PyCharm Community 2022.3.1]
  • OS: [Fedora Linux 37]
  • Ruff Version [0.0.259]
  • Plugin version [ 0.0.10 ]

glarione avatar Mar 26 '23 16:03 glarione

+1 for this. I had no choice but to disable the plugin. But I don't know how could it be fixed? It's literally an external process that edits the file out of the editor's scope. So, the fix should come from JetBrains probably.

metoikos avatar Apr 17 '23 20:04 metoikos

Thank you for creating the issue. I just confirmed the problem!!

So, the fix should come from JetBrains probably. Why do you think this?

I tested undo action before releasing the plugin. It works fine. JetBrains may change the behavior :thinking:

koxudaxi avatar Apr 18 '23 01:04 koxudaxi

We need a setting to turn off the "--fix" option for the plugin or at least while editing. I would not mind a separate action to run "ruff check --fix". Typically I'm going to run ruff on the command line if I'm going to use --fix. In my case rule F401 kept on removing unused imports whenever the editor was saving the file. This is a unit testing file and one of the tests was just importing the library. I ended up having to do a "# noqa: F401" on the line before I typed the import statement.

cfxegbert avatar Apr 18 '23 04:04 cfxegbert

So, the fix should come from JetBrains probably.

So, an external binary/process changes the file's contents, and the problem seems like a sync issue (my theory 😅). Second, I searched the internet, and similar plugins (with external formatter, etc.) had the same problem.

And +1 for the --fix. Sometimes it's really, let's say, not annoying, but it's tiresome that the editor removes the imports immediately (this is not a complaint) :)

Thank you for the plugin and the support you show.

metoikos avatar Apr 18 '23 07:04 metoikos

Für disabling specific rules during --fix: https://github.com/koxudaxi/ruff-pycharm-plugin/issues/135

jankatins avatar Apr 18 '23 08:04 jankatins

@cfxegbert @metoikos
It's a good point. I don't reformat before using a new variable and imported module :sweat_smile: @jankatins I checked your issue. How do we set up the rules to ignore specific actions? Would you be able to use setting preference or project toml? :thinking:

koxudaxi avatar Apr 18 '23 15:04 koxudaxi

@glarione @metoikos Would you happen to know how to reproduce the problem? I tried the steps. But I can't reproduce it. :(

To Reproduce Steps to reproduce the behavior:

Go to some file and add a few empty rows (for example). Push Ctrl+Alt+L (or save file if this setting is checked). Write some text. Try to undo changes till before p.1.

koxudaxi avatar Apr 18 '23 15:04 koxudaxi

To reproduce for me.

Make sure you have the "F" rules enabled (pyflakes)

def some_function() -> None:
  import re

Wait a few seconds for the file to be saved automatically or change mouse focus to another window. You will end up with

def some_function() -> None:
  pass

cfxegbert avatar Apr 18 '23 18:04 cfxegbert

@koxudaxi specifying some rules in the intelij preferences (with a good default) and then calling ruff with e.g. --unfixable=F841,F401 (untested) to overwrite anything in any config file.

jankatins avatar Apr 18 '23 21:04 jankatins

If we have an option to turn off fix by default could we have a "Ruff fix" menu item under the Tools menu?

cfxegbert avatar Apr 19 '23 01:04 cfxegbert

@jankatins Nice. I will implement it.

koxudaxi avatar Apr 19 '23 01:04 koxudaxi

@cfxegbert Thank you for providing the example. But, I can't reproduce my machine. :( IDE: [PyCharm 2023.1] OS: [macOS 12.5] Ruff Version [0.0.254] Plugin version [ 0.0.10 ]

If we have an option to turn off fix by default could we have a "Ruff fix" menu item under the Tools menu?

we have ruff action. we can add a shortcut. Can the feature fulfill your requirements? image

koxudaxi avatar Apr 19 '23 01:04 koxudaxi

@cfxegbert Thank you for providing the example. But, I can't reproduce my machine. :( IDE: [PyCharm 2023.1] OS: [macOS 12.5] Ruff Version [0.0.254] Plugin version [ 0.0.10 ]

I'm using Ruff Version [0.0.261] I also had Run ruff when the python file is saved enabled

cfxegbert avatar Apr 19 '23 03:04 cfxegbert

@cfxegbert I can't reproduce the problem in debug mode in IntelliJ IDEA. But, I met the error in PyCharm with the GitHub copilot plugin. Do you use Copliot or another plugin to change code?

koxudaxi avatar Apr 20 '23 16:04 koxudaxi

I do not use Copilot. I have several internal work plugins. I have disabled those plugins and it still reformats.

cfxegbert avatar Apr 21 '23 21:04 cfxegbert

And +1 for the --fix. Sometimes it's really, let's say, not annoying, but it's tiresome that the editor removes the imports immediately (this is not a complaint) :)

I'll come out and say it's annoying: I'm from a generation where saving is reflexive, I'll regularly save while reading a file just in case. In fact I regularly C-s in a browser window.

I found this issue because I've spent the last 5mn trying to understand why pycharm won't let me have variables I'm not using yet anymore. I'd really like a checkbox to control this behaviour on normal ruff runs (whether there's an action or not for explicit fixing I don't really care).

xmo-odoo avatar Sep 04 '23 09:09 xmo-odoo

I believe this issue has been resolved as we've changed the method of running ruff on save to use the actionOnSave API. If you encounter any problems, please reopen the issue.

koxudaxi avatar Oct 18 '24 14:10 koxudaxi