Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Line break at file end can't be added by PyCharm when running the Ruff plugin #149

Open
simonpercivall opened this issue Apr 20, 2023 · 5 comments

Comments

@simonpercivall
Copy link

simonpercivall commented Apr 20, 2023

Describe the bug
I think this is some weird interaction / race condition between PyCharm and the Ruff plugin running ruff on save.

Running the Ruff plugin on save removes the line break PyCharm is trying to add the the end of the file.

To Reproduce

  1. I Have PyCharm with the setting Editor->General->Ensure every saved file ends with a line break,
  2. and the Ruff plugin set to "Run ruff when the python file is saved".
  3. Now, if I have a file with a \n at the end, everything works as expected (i.e. there's nothing weird going on). But, if I remove that line break, and (with the cursor at some other line) try to save, the line break gets added and then immediately removed, and the file is marked as having unsaved changes (see the flashing 4 in the gif).
  4. If I disable "Run ruff when the python file is saved" everything works as expected.

Expected behavior
A line break is added at the file end, and the file is saved.

Screenshots
disappearing-line-break

Environments (please complete the following information):

  • IDE: PyCharm Professional 2023.1
  • OS: macOS 13.3.1
  • Ruff Version 0.0.261
  • Plugin version 0.0.11
@koxudaxi
Copy link
Owner

@simonpercivall
Thank you for creating the issue.
I can't reproduce the problem.
Could you please share the ruff config like pyproject.toml?

@simonpercivall
Copy link
Author

simonpercivall commented Apr 20, 2023

Weird, I've verified it on different computers with several projects. But it is inconsistent, in that in one case it does add the line break, but leaves the file in an "unsaved changes" state.

[tool.ruff]
select = ["E", "F", "B", "I", "UP", "C4", "DJ", "ISC", "G", "INP", "PT", "RET", "SIM", "TCH", "PTH", "PLE", "PLW", "RUF"]
line-length = 105
ignore = ["E731", "C408", "PLW2901", "RET504", "RET505"]

@koxudaxi
Copy link
Owner

koxudaxi commented May 7, 2023

@simonpercivall
Thank you for sharing the information.
I tested your config and examples. But I can't represent the problem :(

@simonpercivall
Copy link
Author

Well, thanks for checking! I guess it's some interaction between different PyCharm plugins.

I do wonder, is the plugin getting the changed text back or running ruff to fix the files directly? If the latter, it'll always be susceptible to race conditions. But I don't know how complex that change would be, to run ruff with --diff on save (if it isn't already, of course).

@koxudaxi
Copy link
Owner

@simonpercivall

I do wonder, is the plugin getting the changed text back or running ruff to fix the files directly? If the latter,

PyCharm invokes the plugin's method with the file's text to reformat before saving the file.
I should check the order of calling plugin methods.

But I don't know how complex that change would be, to run ruff with --diff on save (if it isn't already, of course).

I feel it's big changes 😅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants