Skip to content

Commit

Permalink
Avoid erasing files when Ruff fails with an error
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Dec 14, 2023
1 parent daaa941 commit e2f7590
Showing 1 changed file with 31 additions and 2 deletions.
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 != 0 and result.exit_code != 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:
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 != 0 and result.exit_code != 1:
show_error(f"Ruff: Fix failed ({result.stderr.decode('utf-8')})")
return None

return _result_to_workspace_edit(document, result)


Expand Down

0 comments on commit e2f7590

Please sign in to comment.