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
Checker python-ruff
fails for Python buffers not visiting a file.
#2061
Comments
Perhaps we can make this some configuration option or something contingent on the presence of an actual python source buffer? |
As a test, I replaced I've made the change locally and so far everything seems to work. Of course, I don't know what the original motivation was for using |
I don't think so, see below
It will (or at least should?) break imports. The ( |
I haven't seen any issues yet. Which is no guarantee that there won't be any, of course. Looking at the doc string of
Regarding
and also:
which seems to suggest that the best way would be to define two ruff checkers, one for buffers backed by a file and one for buffers not backed by a file, and to use Anyway, I'm no expert by any means on flycheck or on ruff, so I'm not the person to say which solution should be chosen. |
This is the important part: the symbol isn't being used as the primary input here. (the primary input is stdin) |
Can you try replacing |
Sure. Just tried it, but I don't see any difference. To be clear, I've now tried four different ways to define the checker:
I've tried all four by opening a Python source file and by opening an Org file containing Python source blocks and editing a source block with Option 1, which is the original definition, gives the error above in Org source blocks. The other three options all appear to behave the same: I experience no issues in a Python source buffer (imports, even local ones, are recognised correctly), and in Org source blocks the checker works as well. The only issue in Org source blocks is that identifiers that are defined in an earlier source block are not recognised in later source blocks, even though all source blocks are evaluated in the same session. But that's to be expected, I assume. (Ruff doesn't have access to the Python session in which the source blocks are evaluated, of course. It can only look at the source block itself.) |
Perfect, if number 4 works then that's what we should use. Thanks a lot for testing. (The differences with the others are subtle: they depend on exactly what ruff does with stdin-filename, but broadly speaking they will give it incorrect information (either a temp file in the wrong directory, or a temp file in the same directory but with the wrong name) |
Sorry, didn't mean to close |
Perhaps we should do the same thing for other checkers that depend on an stdin filename. I suspect that the same issue would pop up in most of them. |
Actually, I just realised I do see a difference between options 2 & 3 on the one hand and option 4 (your suggestion) on the other: with option 4, ruff doesn't seem to provide line/column positions for the errors it finds: if an error is found, Emacs just underlines the first line in the buffer, not the character(s) where the error actually occurs. With options 2 & 3 OTOH the errors are indicated where they occur. Edit: I'm talking about Org source buffers here, in case that wasn't clear... |
Right, you'd need to add
Maybe that too should be the default when using |
This works for me:
I tested using a buffer without a file name in which I explicitly enabled python-mode. Can you confirm on your side? |
Yup. I don't know how you did it, but it works. 😉 Thanks! |
Thanks a lot!
I ran ruff on the command line. If you don't pass it a filename, it prints We should patch this, sorry for the trouble you ran into. We should also review the other checkers that use stdin; my hunch is that almost all of them will have this exact same issue. |
BTW, while I'm here, allow me to nitpick a bit. The doc string is this:
Ruff's documentation capitalises the name and doesn't use the definite article, so it should probably read:
|
Not at all! That's how the process works. 🙂 Thanks for all the work you put into flycheck! |
Bug description
Support for the Python checker ruff was added in #1974. Unfortunately, the way the checker is defined it cannot handle Python buffers that are not visiting a file. When I edit a Python source block in an Org file, I consistently get the following error:
This appears to be due to the definition of the checker
python-flycheck
, which has the following:Specifically, the option
---stdin-filename
to ruff requires a filename, butsource-original
is replaced with an empty string if the source buffer is not visiting a file, which is the case when editing an Org source block (i.e., withC-c '
).Steps to reproduce
Steps to reproduce the behavior:
eglot
withpython-lsp-server
andpython-lsp-ruff
).C-c '
.It is possible to pass code to ruff via stdin that's not associated with any file. Ruff will not apply any fixes, but it will report errors, so it should be usable in Org source buffers. I just don't know the best way to define the checker so that this works and other use cases aren't affected.
System configuration
Emacs configuration:
Emacs 29.1, flycheck installed from Melpa (20240229.628).
The text was updated successfully, but these errors were encountered: