-
Notifications
You must be signed in to change notification settings - Fork 56
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
Bug: A file is reformatted via VS Code but not via CLI #318
Comments
What version does |
> ruff version
ruff 0.1.2 Since I have not overriden I can mention I checked the "[python]": {
"editor.defaultFormatter": "charliermarsh.ruff"
}, Is it possible this could be a VS Code Python extension problem? I reported another problem they had in microsoft/vscode-python#22338 but it should have been fixed. |
I seem to have the "opposite" problem, no formatting happens when I save a file in VS Code with the following snippet: def set_metadata(obj: Pa | Pd,
meta: Metadata,
clear: bool = False) -> Pa | Pd:
"""Store table- and column-level met ... The linter complains, but it will only format if I explicitly use the The log when saving is something like
While the log when explicitly formatting is
This is with the lastest versions of Ruff and extension, and everything enables, that I can think of: "[python]": {
"editor.formatOnSave":true,
"editor.formatOnPaste": true,
"editor.defaultFormatter": "charliermarsh.ruff",
"editor.tabSize": 4,
"editor.insertSpaces": true,
"editor.codeActionsOnSave": {
"source.fixAll":"always",
"source.organizeImports": true
},
"python.linting.enabled": true,
"python.linting.lintOnSave": true,
}, |
Hmm, that's a tricky one. Your configuration does look right, and if something were going wrong, I'd expect to see an error in the log (thank you for including those). You don't happen to have |
It's working now, after a restart. I'm pretty sure I'd already tried restarting VS Code, so not sure what changed. Maybe a subtle bug in the Python plugin (t also e.g. tries to run flake8 all the time, even though it's neither installed nor configured anywhere...) Anyway, successfully moved from black to Ruff 😃 |
Here is a minimal repo with the Python package import logging
import pymongo
logger = logging.getLogger(__name__)
pymongo.MongoClient([]) VS Code will add a line break between the two import statements whilst Ruff via CLI will not. I disabled all extensions, and enabled them one by one. Start with the Python extension (which also installed Pylance). The formatting was not done to the above. I then enabled the Ruff extension, and now the formatting was done to the file. So it proves it is the Ruff extension doing this. |
Can you try running the |
Also, when testing this from the VS Code output channel for Ruff, it also adds the line break: ruff --force-exclude --no-cache --no-fix --quiet --output-format json --fix --extend-ignore 'ALL' --extend-select 'I001' /home/userA/code/foo/tests/utils/mongo.py So this seems to be the culprit. Why is it running ruff multiple times on one save, with all these various extra things added? Should it just not respect |
Are you running Ruff via the CLI with |
@charliermarsh now I am a bit confused. When I run That does not format/add the line break. But saving in VS Code (also with the settings in the original post) does. I then proceeded to check what you mentioned and I tested in #318 (comment). I then also saw that the Output channel in VS Code was using the CLI as seen in the original post and also shown once more in #318 (comment). That was not me using that, it was the VS Code output channel. It adds the |
I think the confusion is perhaps that import sorting isn't part of So if you run To run import sorting locally, you'd want to do (e.g.) If you're using VS Code, we're probably running |
Sorry, we both run The important part is what I see in the output channel of VS Code. And when I try those commands in the CLI, it does add the line break, so those commands being run seems "wrong"? @charliermarsh did you check the output I pasted from VS Code in my original post? |
@thernstig - The original configuration you pasted above doesn't include That output all looks correct to me. Most of those arguments aren't relevant for Ruff's diagnostic behavior and are instead required for LSP interoperability (e.g., we need Ruff to output JSON). |
The original configuration I pasted in the first post is the config I have. Nothing else. Are you saying this line, from the Ruff output panel, is actually never executed in VS Code on save? 2023-10-27 10:40:32.616 [info] Using interpreter executable: /home/userA/code/foo/.venv/bin/ruff
2023-10-27 10:40:32.617 [info] Found ruff 0.1.2 at /home/userA/code/foo/.venv/bin/ruff
2023-10-27 10:40:32.617 [info] Running Ruff with: /home/userA/code/foo/.venv/bin/ruff ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--output-format', 'json', '-', '--fix', '--extend-ignore', 'ALL', '--extend-select', 'I001', '--stdin-filename', '/home/userA/code/foo/tests/utils/mongo.py'] |
That line is executed. That's from VS Code's I think you should either add |
@charliermarsh then I think this is sorted, thank you for the patience. I understand why this happens then. I think maybe "something" should be done? The confusion might be a docs issue, possibly in https://github.com/astral-sh/ruff-vscode, as that mentions The confusion stems from that I thought that even an explicit "editor.codeActionsOnSave": {
"source.organizeImports": true
} My solution for now is to add the Or it might be entirely possibly you believe no change should be made at all to this, and it is my mistake on assuming that the |
Thanks -- I went ahead and added a note to the docs! |
The following does nothing:
But saving the same file in VS Code does. (It removes a line break between some import statements).
Output from VS Code Ruff:
Ruff settings:
VS Code settings:
The text was updated successfully, but these errors were encountered: