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

ruff VSCode extension erases entire file on save #9123

Closed
bdewilde opened this issue Dec 14, 2023 · 14 comments · Fixed by astral-sh/ruff-lsp#341
Closed

ruff VSCode extension erases entire file on save #9123

bdewilde opened this issue Dec 14, 2023 · 14 comments · Fixed by astral-sh/ruff-lsp#341
Labels
bug Something isn't working cli Related to the command-line interface

Comments

@bdewilde
Copy link

After updating/reloading a couple extensions in VSCode -- one of which may have been Ruff, I'm not 100% sure -- the Ruff extension suddenly started erasing entire Python files on save. I've disabled/re-enabled and uninstalled/re-installed, but that doesn't change the behavior. I've further narrowed it down to the "format imports" command -- "format document" does not erase the module -- but disabling the "organize imports" behavior in settings somehow does not stop saves from erasing the file. The corresponding configs in my VSCode settings and pyproject.toml tools have not changed.

I don't have a code snippet, but I do have a gif demonstrating the phenomenon:

ruff-surprise

This is quite unexpected, and I do not understand what's gone wrong. I have the latest version of both the ruff VSCode extension (v2023.52.0) and Python package (v0.1.8) installed. Here's the relevant config:

  "[python]": {
    "editor.defaultFormatter": "charliermarsh.ruff",
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
      "source.fixAll.ruff": "always",
      "source.organizeImports.ruff": "always"
    },
  }

and

[tool.ruff]
# ignore line-length violations, None comparisons
ignore = ["E501", "E711"]
line-length = 88
indent-width = 4
target-version = "py310"

[tool.ruff.format]
quote-style = "double"
indent-style = "space"
skip-magic-trailing-comma = false
line-ending = "auto"

[tool.ruff.lint.isort]
lines-after-imports = 2

[tool.ruff.per-file-ignores]
"__init__.py" = ["E402", "F401"] # imports not at top of cell and unused imports

Assuming this must be user error, but gosh I don't know what or how. Thanks in advance for your help.

@charliermarsh
Copy link
Member

Let me try this now -- I thought we had fixed this but it may've regressed.

@charliermarsh
Copy link
Member

Is this happening in every file, or only certain files?

@charliermarsh
Copy link
Member

I assume this persists across restarting VS Code?

@bdewilde
Copy link
Author

Hi! Thanks very much for the quick response. This does happen across VSCode restarts -- I even restarted my laptop, was getting desperate. This is happening for every Python file that I try to save.

@charliermarsh
Copy link
Member

Do you mind checking the version of Ruff that's running as reported by the extension in the Ruff logs?

Screen Shot 2023-12-13 at 10 17 44 PM

I can't reproduce with the latest extension and Ruff version, but if you're continuing to see this then I'll revert the change that likely caused it and re-release while we figure out what's gone wrong.

@charliermarsh
Copy link
Member

Also, if you could confirm that downgrading to the previous release fixes it for you, that'd be super helpful too!

@bdewilde
Copy link
Author

bdewilde commented Dec 14, 2023

Nice, I didn't realize you could peep on logs from particular extensions in VSCode. Here's what I see there re: version:

2023-12-13 22:19:28.313 [info] Name: Ruff
2023-12-13 22:19:28.313 [info] Module: ruff
2023-12-13 22:19:28.313 [info] Python extension loading
2023-12-13 22:19:28.313 [info] Waiting for interpreter from python extension.
2023-12-13 22:19:28.313 [info] Python extension loaded
2023-12-13 22:19:28.313 [info] Server run command: /Users/burtondewilde/.pyenv/versions/3.10.12/envs/colandr-py310/bin/python /Users/burtondewilde/.vscode/extensions/charliermarsh.ruff-2023.52.0-darwin-x64/bundled/tool/server.py
2023-12-13 22:19:28.313 [info] Server: Start requested.
2023-12-13 22:19:29.271 [info] 2023-12-13 22:19:29,269 INFO Starting IO server

Scrolling through, I also see an error -- maybe this is normal, but figured I'd call it out:

2023-12-13 22:19:29.415 [info] [Trace - 10:19:29 PM] Received notification 'window/logMessage'.
2023-12-13 22:19:29.415 [info] Running Ruff with: /Users/burtondewilde/.pyenv/versions/3.10.12/envs/colandr-py310/bin/ruff ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--output-format', 'json', '-', '--line-length=88', '--indent-width=4', '--stdin-filename', '/Users/burtondewilde/Desktop/example.py']
2023-12-13 22:19:29.428 [info] [Trace - 10:19:29 PM] Received notification 'window/logMessage'.
2023-12-13 22:19:29.428 [info] error: unexpected argument '--indent-width' found

  tip: to pass '--indent-width' as a value, use '-- --indent-width'

Which should I try downgrading: the package (from v0.1.8 to v0.1.7) or the extension?

@charliermarsh
Copy link
Member

Interesting, do you have --indent-width passed in as a command-line argument in your settings? (I was suggesting downgrading the extension to 2023.50.0.)

@charliermarsh
Copy link
Member

Ahhh okay, I think the issue is:

"ruff.lint.args": ["--indent-width", "4"]

Or similar in your settings.json. We're wiping the file on error, apparently. I'll fix this tonight.

@bdewilde
Copy link
Author

bdewilde commented Dec 14, 2023

Aha, yeah, I have these at the bottom of my VSCode user settings -- forgot about them, since I set them up a long while back when I was still trying to figure out if ruff could replace black+isort :)

  "ruff.lint.run": "onSave",
  "ruff.lint.args": [
    "--line-length=88",
    "--indent-width=4"
  ],

Should I remove them? Restructure the JSON as your example suggests?

update: Removing them stops the files from being erased on save, but changing the structure of the json does not.

@charliermarsh
Copy link
Member

Yeah you need to remove --indent-width=4 -- we only support a subset of arguments on the command-line, so Ruff is failing when it sees --indent-width=4, and we're (erroneously) wiping your file instead of surfacing the error.

@bdewilde
Copy link
Author

Understood. Thanks a ton for helping to resolve this so quickly!

@charliermarsh
Copy link
Member

No prob. Sorry for all the trouble... Will get this fixed for good ASAP.

@MichaReiser MichaReiser added bug Something isn't working cli Related to the command-line interface labels Dec 14, 2023
charliermarsh added a commit to astral-sh/ruff-lsp that referenced this issue Dec 14, 2023
## Summary

A user reported an issue in the VS Code extension
(astral-sh/ruff#9123) in which their files
were being erased on-save. The issue is that we don't check the exit
code of all commands, and so if `ruff check` fails with an _error_, then
we end up wiping the file entirely.

This PR adds exit code-checking for all commands in the LSP. Further, we
now surface such failures to the user directly (see bottom right):

<img width="1792" alt="Screen Shot 2023-12-13 at 10 57 20 PM"
src="https://github.com/astral-sh/ruff-lsp/assets/1309177/14f70b96-5cbd-4d0f-b2a3-532984167724">

Closes astral-sh/ruff#9123
@charliermarsh
Copy link
Member

Should be fixed in the latest version, just published.

azurelotus0926 added a commit to azurelotus0926/ruff-lsp that referenced this issue Jun 27, 2024
## Summary

A user reported an issue in the VS Code extension
(astral-sh/ruff#9123) in which their files
were being erased on-save. The issue is that we don't check the exit
code of all commands, and so if `ruff check` fails with an _error_, then
we end up wiping the file entirely.

This PR adds exit code-checking for all commands in the LSP. Further, we
now surface such failures to the user directly (see bottom right):

<img width="1792" alt="Screen Shot 2023-12-13 at 10 57 20 PM"
src="https://github.com/astral-sh/ruff-lsp/assets/1309177/14f70b96-5cbd-4d0f-b2a3-532984167724">

Closes astral-sh/ruff#9123
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working cli Related to the command-line interface
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants