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

Formatter: Improve handling of syntax errors #9311

Closed
zanieb opened this issue Dec 29, 2023 · 2 comments · Fixed by #9321
Closed

Formatter: Improve handling of syntax errors #9311

zanieb opened this issue Dec 29, 2023 · 2 comments · Fixed by #9321
Assignees
Labels
formatter Related to the formatter

Comments

@zanieb
Copy link
Member

zanieb commented Dec 29, 2023

The current user-facing messaging for syntax errors encountered during formatting are not friendly and are significantly worse than the linter's. The error should be normalized into a message instead of using the debug representation of ParseError

e.g.

Line number not included (existing issue):

error: Failed to format commands/liberrors.py: source contains syntax errors: ParseError { error: UnrecognizedToken(Name { name: "THE_INVALID_FILE" }, None), offset: 208, source_path: "<filename>" } 157 files left unchanged

Cell number not included in notebook errors

error: Failed to format course/videos/domain_adaptation.ipynb: source contains syntax errors: ParseError { error: UnrecognizedToken(Rpar, None), offset: 70, source_path: "<filename>" }

The previous error is much nicer via the linter

error: Failed to parse course/videos/domain_adaptation.ipynb:cell 9:1:71: Unexpected token ')'

@zanieb zanieb added the formatter Related to the formatter label Dec 29, 2023
@zanieb zanieb added this to the Formatter: Stable milestone Dec 29, 2023
@charliermarsh charliermarsh self-assigned this Dec 29, 2023
@charliermarsh
Copy link
Member

I can fix these.

@zanieb
Copy link
Member Author

zanieb commented Dec 29, 2023

It'd also be nice to exit with a specific code if we only fail due to syntax errors rather than some sort of other failure :) that way we can report it nicely in the ecosystem checks instead of treating it as a failed run of Ruff.

charliermarsh added a commit that referenced this issue Dec 29, 2023
## Summary

This helps a bit with (but does not close) the issues described in
#9311. E.g., now, we at least
see: `error: Failed to format main.py: source contains syntax errors:
invalid syntax. Got unexpected token '=' at byte offset 20`.
charliermarsh added a commit that referenced this issue Dec 31, 2023
## Summary

We now render parse errors in the formatter identically to those in the
linter, e.g.:

```
❯ cargo run -p ruff_cli -- format foo.py
error: Failed to parse foo.py:1:17: Unexpected token '='
```

Closes #8338.

Closes #9311.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants