ruff icon indicating copy to clipboard operation
ruff copied to clipboard

operate on files with certain hashbang

Open spaceone opened this issue 2 years ago • 10 comments

ruff seems only to search for files with .py (maybe also `.pyi) file extensions?!

it would be good to detect some certain hashbangs like: !#/usr/bin/python !#/usr/bin/python3 !#/usr/bin/python -b !#/usr/bin/env python3

and it would be nice to be able to put further definitions in the config files, we e.g. have definitions like: #!/usr/share/test/runner python3 #!/usr/share/test/runner py.test-3

spaceone avatar Jan 26 '23 12:01 spaceone

Rather than looking for a specific shebang, maybe a configuration could be added to include certain files based on paths?

sciyoshi avatar Jan 26 '23 14:01 sciyoshi

Yeah, I'm extremely hesitant to check every file for a shebang.

If you pass a file to Ruff directly, it will check it, even if it's not a .py or .pyi file (like ruff /path/to/file). That's what most users have done in the past. Though I'd be open to some kind of opt-in list.

charliermarsh avatar Jan 26 '23 14:01 charliermarsh

I wouldn't use a configurable list of files as files are too often just changing by too many people. The files are tracked in git and all of them might be relevant.

What's wrong with checking hashbangs? I am using it currently and it is not really making a performance issue.

spaceone avatar Jan 26 '23 14:01 spaceone

I assume it will come at a performance cost. I'd need to benchmark it.

Is there a common extension on those files? (Or even: do they all have no extension?)

charliermarsh avatar Jan 26 '23 14:01 charliermarsh

I guess they all have no extension. simple scripts.

spaceone avatar Jan 26 '23 14:01 spaceone

We could consider checking non-extension files for hashbangs.

charliermarsh avatar Jan 26 '23 15:01 charliermarsh

That would be great. we have 462 python files without file extensions and 4 python files with (accidental) file extensions (.in, .conf, .3, .0-dev)

spaceone avatar Jan 26 '23 15:01 spaceone

Another simple workaround would be to find the files separately and pass those explicitly to ruff, e.g. (with ripgrep):

rg -l -0 '\A#!/usr/bin/env python' | xargs -0 ruff

sciyoshi avatar Jan 26 '23 15:01 sciyoshi

yes, this is kind of my current workaround.

spaceone avatar Jan 26 '23 15:01 spaceone

This workaround would also be needed in a pre-commit hook, where I did not yet find out how to achieve it.

spaceone avatar Jan 27 '23 11:01 spaceone

ok, pre-commit already supports it: it first searches for files with .py[i] extension and then for executable files with common python hashbangs.

spaceone avatar Feb 08 '23 21:02 spaceone

This could also be solved by https://github.com/charliermarsh/ruff/issues/3410, sort of, though you'd have to enumerate them explicitly.

charliermarsh avatar Mar 08 '23 22:03 charliermarsh

This could also be solved by #3410, sort of, though you'd have to enumerate them explicitly.

For me, it would suffice to include them explicitly and I agree that performance could suffer if ruff would read every file to search for the bang.

(Using the term "include" here, because the include option is how you can do it with Black.)

neingeist avatar Mar 08 '23 22:03 neingeist

hm, #3914 closed it but this allows only files with a specific extension. our files/scripts don't have any file extension.

spaceone avatar Apr 12 '23 07:04 spaceone

Ah yeah, the reasoning was that this now provides a manual workaround, analogous to Black's own configuration (whereby you can specify the script names in the extend-include vector). I'm open to re-opening this, although I don't yet have a solution that I'm happy with for supporting arbitrary scripts via shebangs, so I can't commit to improving it further.

charliermarsh avatar Apr 12 '23 14:04 charliermarsh