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 removing all contents when formatting file with syntax error #314

Merged
merged 5 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
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
18 changes: 13 additions & 5 deletions ruff_lsp/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -1095,10 +1095,14 @@ async def apply_organize_imports(arguments: tuple[TextDocument]):
async def apply_format(arguments: tuple[TextDocument]):
uri = arguments[0]["uri"]
document = Document.from_uri(uri)
results = await _run_format_on_document(document)
workspace_edit = _result_to_workspace_edit(document, results)

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

workspace_edit = _result_to_workspace_edit(document, result)
if workspace_edit is None:
return
return None
LSP_SERVER.apply_edit(workspace_edit, "Ruff: Format document")


Expand All @@ -1108,8 +1112,9 @@ async def format_document(params: DocumentFormattingParams) -> list[TextEdit] |
# request itself can only act on a text document. A cell in a Notebook is
# represented as a text document.
document = Document.from_cell_or_text_uri(params.text_document.uri)

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add this to _lint_document_impl for good measure.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the patch in #314 (comment) would apply to all uses

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh you're right, we might exit 1 on lint errors.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In fact pretty sure we do.)

return None

if document.kind is DocumentKind.Cell:
Expand Down Expand Up @@ -1330,7 +1335,10 @@ async def run_path(
stdin=asyncio.subprocess.PIPE,
cwd=cwd,
)
result = RunResult(*await process.communicate(input=source.encode("utf-8")))
result = RunResult(
*await process.communicate(input=source.encode("utf-8")),
exit_code=await process.wait(),
)

if result.stderr:
log_to_output(result.stderr.decode("utf-8"))
Expand Down
3 changes: 2 additions & 1 deletion ruff_lsp/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ def version(executable: str) -> Version:
class RunResult:
"""Object to hold result from running tool."""

def __init__(self, stdout: bytes, stderr: bytes):
def __init__(self, stdout: bytes, stderr: bytes, exit_code: int):
self.stdout: bytes = stdout
self.stderr: bytes = stderr
self.exit_code: int = exit_code
26 changes: 26 additions & 0 deletions tests/test_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,33 @@ async def test_format(tmp_path, ruff_version: Version):
with handle_unsupported:
result = await _run_format_on_document(document)
assert result is not None
assert result.exit_code == 0
[edit] = _fixed_source_to_edits(
original_source=document.source, fixed_source=result.stdout.decode("utf-8")
)
assert edit.new_text == expected


@pytest.mark.asyncio
async def test_format_code_with_syntax_error(tmp_path, ruff_version: Version):
source = """
foo =
"""

test_file = tmp_path.joinpath("main.py")
test_file.write_text(source)
uri = utils.as_uri(str(test_file))

workspace = Workspace(str(tmp_path))
document = Document.from_text_document(workspace.get_text_document(uri))

handle_unsupported = (
pytest.raises(RuntimeError, match=f"Ruff .* required, but found {ruff_version}")
if not VERSION_REQUIREMENT_FORMATTER.contains(ruff_version)
else nullcontext()
)

with handle_unsupported:
result = await _run_format_on_document(document)
assert result is not None
assert result.exit_code == 2
Loading