Skip to content

Ruff format on save doesn't modify file #345

@abridgland

Description

@abridgland

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'.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions