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 format on save doesn't modify file #345

Closed
abridgland opened this issue Nov 21, 2023 · 6 comments
Closed

Ruff format on save doesn't modify file #345

abridgland opened this issue Nov 21, 2023 · 6 comments

Comments

@abridgland
Copy link

Ruff VS Code extension version: v2023.50.0
Ruff version: not installed
Environment: VS Code miniconda devcontainer running on Mac host.
Python version: 3.10.13

I have installed Ruff in a vscode devcontainer and have set it as the default formatter. It successfully runs when running either "Format document" or "Ruff: format document" from the command palette but when I save a python file after editing it doesn't modify the file, even when when formatting is required. This can be confirmed by making edits that cause changes to be made when run manually (as previously described) and saving. The result is that the file saves but no changes are made.

When I inspect the logs it looks like ruff is run with different args depending on whether it's run on save or not, and when running on save the args don't seem sufficient to actually modify the file. However I'm new to ruff so I could be wrong, and I don't know what in my settings would cause this to happen!

Settings and logs pasted below.:

VS Code customizations in devcontainer.json.

	"customizations": {
		"vscode": {
			"extensions": [
				"ms-python.python",
				"mhutchie.git-graph",
				"charliermarsh.ruff"
			],
			"settings": {
				"python.analysis.typeCheckingMode": "basic",
				"[python]": {
					"editor.formatOnSave": true,
					"editor.formatOnSaveMode": "modifications",
					"editor.defaultFormatter": "charliermarsh.ruff",
					"editor.rulers": [
						90
					],
					"editor.codeActionsOnSave": {
						"source.organizeImports": "always"
					}
				},
				"files.exclude": {
					"**/__pycache__": true
				},
				"ruff.lint.run": "onSave"
			}
		}
	},

pyproject.toml:

[project]
# Support Python 3.10+.
requires-python = ">=3.10"


[tool.ruff]
line-length = 90
indent-width = 2

# Target Python 3.10
target-version = "py310"

[tool.ruff.format]
quote-style = "single"
indent-style = "space"

settings.json

{
	"python.defaultInterpreterPath": "/opt/conda/bin/python",
	"python.analysis.typeCheckingMode": "basic",
	"[python]": {
		"editor.formatOnSave": true,
		"editor.formatOnSaveMode": "modifications",
		"editor.defaultFormatter": "charliermarsh.ruff",
		"editor.rulers": [
			90
		],
		"editor.codeActionsOnSave": {
			"source.organizeImports": "always"
		}
	},
	"files.exclude": {
		"**/__pycache__": true
	},
	"ruff.lint.run": "onSave"
}

Logs when running manually

2023-11-21 17:00:40.731 [info] [Trace - 5:00:40 PM] Sending request 'textDocument/formatting - (5)'.
2023-11-21 17:00:40.735 [info] [Trace - 5:00:40 PM] Received notification 'window/logMessage'.
2023-11-21 17:00:40.735 [info] Interpreter executable (/opt/conda/bin/ruff) not found
2023-11-21 17:00:40.736 [info] [Trace - 5:00:40 PM] Received notification 'window/logMessage'.
2023-11-21 17:00:40.736 [info] Falling back to bundled executable: /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff
2023-11-21 17:00:40.736 [info] [Trace - 5:00:40 PM] Received notification 'window/logMessage'.
2023-11-21 17:00:40.736 [info] Found ruff 0.1.5 at /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff
2023-11-21 17:00:40.736 [info] [Trace - 5:00:40 PM] Received notification 'window/logMessage'.
2023-11-21 17:00:40.736 [info] Running Ruff with: /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff ['format', '--force-exclude', '--quiet', '--stdin-filename', '/workspaces/path/to/myfile.py']
2023-11-21 17:00:40.748 [info] [Trace - 5:00:40 PM] Received response 'textDocument/formatting - (5)' in 17ms.
2023-11-21 17:00:40.798 [info] [Trace - 5:00:40 PM] Sending notification 'textDocument/didChange'.
2023-11-21 17:00:41.012 [info] [Trace - 5:00:41 PM] Sending request 'textDocument/codeAction - (6)'.
2023-11-21 17:00:41.017 [info] [Trace - 5:00:41 PM] Received response 'textDocument/codeAction - (6)' in 5ms.

Logs when saving

2023-11-21 17:02:13.499 [info] [Trace - 5:02:13 PM] Sending notification 'textDocument/didSave'.
2023-11-21 17:02:13.505 [info] [Trace - 5:02:13 PM] Received notification 'window/logMessage'.
2023-11-21 17:02:13.505 [info] Interpreter executable (/opt/conda/bin/ruff) not found
2023-11-21 17:02:13.505 [info] [Trace - 5:02:13 PM] Received notification 'window/logMessage'.
2023-11-21 17:02:13.505 [info] Falling back to bundled executable: /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff
2023-11-21 17:02:13.506 [info] [Trace - 5:02:13 PM] Received notification 'window/logMessage'.
2023-11-21 17:02:13.506 [info] Found ruff 0.1.5 at /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff
2023-11-21 17:02:13.506 [info] [Trace - 5:02:13 PM] Received notification 'window/logMessage'.
2023-11-21 17:02:13.506 [info] Running Ruff with: /root/.vscode-server/extensions/charliermarsh.ruff-2023.50.0-linux-arm64/bundled/libs/bin/ruff ['--force-exclude', '--no-cache', '--no-fix', '--quiet', '--output-format', 'json', '-', '--stdin-filename', '/workspaces/path/to/myfile.py']
2023-11-21 17:02:13.519 [info] [Trace - 5:02:13 PM] Received notification 'textDocument/publishDiagnostics'.
@zanieb
Copy link
Member

zanieb commented Nov 21, 2023

Those "on save" logs are for the linting command. Perhaps turn off linting on save to avoid the noise while debugging?

@abridgland
Copy link
Author

Ah thanks! Those were the only logs when saving, so I guess the linter is running but not the formatter? I can't work out why that would be though 🤔

If it's any help I am switching from black and found that this was working fine in black.

@zanieb
Copy link
Member

zanieb commented Nov 21, 2023

Perhaps it's related to "editor.formatOnSaveMode": "modifications", — we don't support that mode yet.

@abridgland
Copy link
Author

That's fixed it! Thanks so much for the quick help.

I now have a feature request :)

@zanieb
Copy link
Member

zanieb commented Nov 21, 2023

We need range formatting support, I think :) astral-sh/ruff#7233

@LazyRen
Copy link

LazyRen commented Dec 21, 2023

Perhaps it's related to "editor.formatOnSaveMode": "modifications", — we don't support that mode yet.

Ah, that explains why I'm also not able to activate format on save.

IMHO, if Ruff doesn't support modifications save mode, it should be documented somewhere to inform the users.

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

3 participants