From 87ffc377484d8362c49d8a69be58969fa6c1cb2a Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Wed, 13 Dec 2023 22:58:28 -0500 Subject: [PATCH] Avoid erasing files when Ruff fails with an error --- ruff_lsp/server.py | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/ruff_lsp/server.py b/ruff_lsp/server.py index 27e4afa..23f8369 100755 --- a/ruff_lsp/server.py +++ b/ruff_lsp/server.py @@ -556,8 +556,17 @@ async def _did_change_or_save_notebook( 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 [] @@ -1097,7 +1106,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) @@ -1114,7 +1128,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( @@ -1141,6 +1160,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)