numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
570 stars 35 forks source link

Handle scripts with no file extension #33

Open zimbatm opened 3 years ago

zimbatm commented 3 years ago

Problem statement

Scripts often don't have a file extension. This means that the files = [] attribute won't be able to target them efficiently. The user would have to manually pass all of the filenames in there.

Proposed solution

If a file has the executable bit set, open the file and look at the shebang. This will make the detection more expensive but I don't really see a way around it.

In the formatter config, add a files_executable = [] section that lists all of the shebang executables the formatter should match on.

In cases where #!/usr/bin/env is being used, it should strip that and look at the next argument to find the executable name. Otherwise, it looks at the basename of the first argument.

sellout commented 1 year ago

shfmt does this internally (I don’t know if that is visible via treefmt or if the interface always passes specific files or what).

An shfmt --find or something will return a list of the files it would have formatted, so it exposes the ability to find shebangs … but it doesn’t let you specify what you might want to find on the shebang, so you’re restricted to shell scripts.