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

How to set up apheleia for ruff? #351

Open
Odysseus6000 opened this issue Feb 16, 2025 · 11 comments
Open

How to set up apheleia for ruff? #351

Odysseus6000 opened this issue Feb 16, 2025 · 11 comments

Comments

@Odysseus6000
Copy link

My configuration is as follows:
(use-package apheleia
:ensure t
:config
(apheleia-global-mode t)

(with-eval-after-load 'apheleia

(setf (alist-get 'python-mode apheleia-mode-alist)
'(ruff-isort ruff))
(setf (alist-get 'python-ts-mode apheleia-mode-alist)
'(ruff-isort ruff))
) )
You often get the following prompt in python files:
Failed to run ruff: exit status 2 (see buffer apheleia-ruff-log)?
Mac, emacs-plus@31

@raxod502
Copy link
Member

And what's in that buffer?

@Odysseus6000
Copy link
Author

Odysseus6000 commented Feb 17, 2025

x = 100
y = 100
result = 0
for m in range(x + 1):
    for n in range(y+ 1):

When the mouse jumps to the last line, the following hint appears in eldoc. The y in range(y + 1) is not formatted as range(y + 1), i.e. there is a space after y?

Image

Is it possible to make the following message not appear in eldoc? It is a bit intrusive as the message appears too often. Check in apheleia-ruff-log if needed.

@raxod502
Copy link
Member

You need to report what is in that log buffer, as otherwise it's not possible to tell what's wrong.

@Odysseus6000
Copy link
Author

The contents of the apheleia-ruff-log* buffer are as follows
Tue Feb 18 10:49:36 2025 :: /Documents/day6/
$ ruff format --silent --stdin-filename /Documents/day6/example.py -

(no output on stderr)

Command failed with exit code 2.

@raxod502
Copy link
Member

Sounds like the formatter is failing. Is the input code syntactically valid? If so, this is a bug that needs to be reported upstream.

@Odysseus6000
Copy link
Author

The above python code is syntactically correct.

@raxod502
Copy link
Member

Oh. You formatted it as a single line, but you meant to format it as multiple lines. I fixed it for you by editing your comment.

@raxod502
Copy link
Member

That said, your code is not syntactically correct. There's no statement after the last line. The formatter is thus correctly throwing an error.

@Odysseus6000
Copy link
Author

The original code is below:

x = 100
y = 100
result = 0
for m in range(x + 1):
    for n in range(y+ 1):

I expect the formatted code to look like this:

x = 100
y = 100
result = 0
for m in range(x + 1):
    for n in range(y + 1):

I'm not sure if it's possible to format it for the incomplete code snippet above.
The formatter is thus correctly throwing an error, I think it's because

(y+ 1)

not formatted.

@raxod502
Copy link
Member

No, that's not why it's throwing an error. It's throwing an error because your code isn't syntactically valid. If you want Ruff to be able to format the code you provided, you need to report an issue to that project. This doesn't relate to Apheleia.

@tepo4ka
Copy link

tepo4ka commented Feb 23, 2025

Can confirm that ruff doesn't seem to work on the following file:

#+title: Test apheleia ruff

#+begin_src python
import numpy as np

rng = np.random.default_rng()
#+end_src

#+RESULTS:
: None

In the apheleia-ruff-log:

Sun Feb 23 23:00:08 2025 :: /.../
$ ruff format --silent --stdin-filename /.../test.org -

(no output on stderr)

Command failed with exit code 2.

I'm not sure how to test the provided ruff command, but here's what I got:

$ ruff format --silent --stdin-filename /.../test.org -
import numpy
rng = numpy.random.default_rng()<C-d>
import numpy

rng = numpy.random.default_rng()

I'm using ruff v0.9.7, doom emacs with the following doom version

GNU Emacs     v29.4            nil
Doom core     v3.0.0-pre       grafted, HEAD -> master, origin/master, origin/HEAD 2bc05242 2025-01-14 13:57:18 -0500
Doom modules  v25.02.0-pre     grafted, HEAD -> master, origin/master, origin/HEAD 2bc05242 2025-01-14 13:57:18 -0500

and apheleia d8ccc0b.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

3 participants