Repository: astral-sh/ruff-vscode

Issue #128: Auto-fixing sometimes leaves junk at the end of a file when the end result is shorter after the fix
Created: 2023-02-08T11:56:59Z
Ruff for vs code v2023.6.0
Easiest if I work through an example with screenshots.

Create a file with auto-fixable import issues:



Press save (command S, etc) and observe that one or more duplicate lines remain at the end of the file:


I notice, when hitting Undo, that there are in fact two steps being applied in quick succession. The intermediate step looks like this:

pyproject.toml contains the following relevant entries:
[tool.ruff]
select = [
  "I",   # iSort 
  "F",   # PyFlakes
  "E",   # pycodestyle
  "W",   # Warning
  "YTT", # flake8-2020
  "C4",  # flake8-comprehensions
  "T10", # flake8-debugger
  "TID", # flake8-tidy-imports
  "PLC", # Pylint Convention
  "PLE", # Pylint Error
  "PLW", # Pylint Warning
] 

ignore = [
  "E501", # line length violations
  "E722", # Bare except
  "E741", # Ambiguous variable name
  "E999", # Syntax Error as it doesnt understand match commands from Python 3.10+
]

The extension config within vscode are the defaults.

Duplicate issue #158: Last line of file duplicated on save
Created: 2023-03-17T17:41:08Z
When ruff inserts a newline after the imports, it also duplicates the last line in the file. I am using the preview version of ruff in VSCode (0.0.256) on Windows 10. (A quick test shows the same behavior with the release version of 0.0.252.) I have not configured ruff in any way. I have fixAll and organizeImports set to true:
    "[python]": {
        "editor.codeActionsOnSave": {
            "source.fixAll": true,
            "source.organizeImports": true
        }
    }
For example, the first code block below will turn into the second on save (when ruff is enabled):
import datetime

import pandas as pd

def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()
import datetime

import pandas as pd


def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()
    return pd.DataFrame()
If there is no final newline, then the code is formatted into this:
import datetime

import pandas as pd


def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()    return pd.DataFrame()
If the formatter inserts two lines, then the last two lines are duplicated. The first block turns into the second:
import datetime

import pandas as pd
def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()
import datetime

import pandas as pd


def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()
def foo(bar: datetime.date) -> pd.DataFrame:
    return pd.DataFrame()
With ruff disabled, the formatting works correctly.
Please let me know what other information I can provide.

Closing comment (2023-03-17T17:56:14Z): Duplicate of Auto-fixing sometimes leaves junk at the end of a file when the end result is shorter after the fix #128


Thank you! For the life of me, I could not find any similar issues. This is definitely a duplicate of #128. As mentioned there, disabling isort fixes the problem. (I did not realize isort was installed.) I don't think I have anything further to add, so this issue should be closed.
EDIT: Sorry, I should have closed as duplicate, but I can't figure out how to change that now.