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

Avoid erasing files when Ruff fails with an error #341

Merged
merged 1 commit into from
Dec 14, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,14 @@ async def _lint_document_impl(document: Document) -> list[Diagnostic]:
result = await _run_check_on_document(document)
if result is None:
return []

# For `ruff check`, 0 indicates successful completion with no remaining
# diagnostics, 1 indicates successful completion with remaining diagnostics,
# and 2 indicates an error.
if result.exit_code not in (0, 1):
show_error(f"Ruff: Lint failed ({result.stderr.decode('utf-8')})")
return []

return _parse_output(result.stdout) if result.stdout else []


Expand Down Expand Up @@ -1097,7 +1105,12 @@ async def apply_format(arguments: tuple[TextDocument]):
document = Document.from_uri(uri)

result = await _run_format_on_document(document)
if result is None or result.exit_code != 0:
if result is None:
return None

# For `ruff format`, 0 indicates successful completion, non-zero indicates an error.
if result.exit_code != 0:
show_error(f"Ruff: Format failed ({result.stderr.decode('utf-8')})")
return None

workspace_edit = _result_to_workspace_edit(document, result)
Expand All @@ -1114,7 +1127,12 @@ async def format_document(params: DocumentFormattingParams) -> list[TextEdit] |
document = Document.from_cell_or_text_uri(params.text_document.uri)

result = await _run_format_on_document(document)
if result is None or result.exit_code:
if result is None:
return None

# For `ruff format`, 0 indicates successful completion, non-zero indicates an error.
if result.exit_code != 0:
show_error(f"Ruff: Format failed ({result.stderr.decode('utf-8')})")
return None

if not VERSION_REQUIREMENT_EMPTY_OUTPUT.contains(
Expand All @@ -1141,6 +1159,17 @@ async def _fix_document_impl(
extra_args=["--fix"],
only=only,
)

if result is None:
MichaReiser marked this conversation as resolved.
Show resolved Hide resolved
return None

# For `ruff check`, 0 indicates successful completion with no remaining
# diagnostics, 1 indicates successful completion with remaining diagnostics,
# and 2 indicates an error.
if result.exit_code not in (0, 1):
show_error(f"Ruff: Fix failed ({result.stderr.decode('utf-8')})")
return None

return _result_to_workspace_edit(document, result)


Expand Down
Loading