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

Support assigning a file to a formatter based on its shebang #815

Open
Silic0nS0ldier opened this issue Jan 25, 2024 · 5 comments
Open

Support assigning a file to a formatter based on its shebang #815

Silic0nS0ldier opened this issue Jan 25, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@Silic0nS0ldier
Copy link
Sponsor

It is common on POSIX systems for text files to be used as executables. Such files often lack a file extension which makes them difficult for formatters to pick up.

It would be nice if plugins could opt into formatting files matching certain shebang patterns.
e.g. #!/usr/bin/env bash, #!/bin/sh, #!/usr/bin/env node, #!/usr/bin/env python

Outside the scope of this feature request is;

  • Searching for files with a shebang (difficult to optimise as file reading is required).
  • Detecting executable files without a magic number (e.g. not #! nor .ELF).
    Context: POSIX mandates that such files be run with a POSIX compliant shell. (these may become more common as its used for https://justine.lol/ape.html and https://github.com/jart/cosmopolitan)
@dsherret
Copy link
Member

I think this is out of scope because it would be too slow to find the shebang patterns since it requires reading the files? The way to do this today would be to manually specify the file in a config using the "associations" feature: https://dprint.dev/config/#associations

@bradzacher
Copy link

@dsherret the issue is that more often than not hashbang'd files have no extension and are often uniquely named - so it's hard to keep them formatted as you need to remember to update each time you add / rename one of them.

I wonder if there's a way we can limit this to just extensionless files? I.e. Dprint always ignores file contents if the file has an extension, and if it has no extension then dprint will read the first 2 bytes, check if it is #!, ignore the file if it isn't, or read the first line if it is.

That would keep things as cheap as feasibly possible by limiting the scope and effort.

@dsherret
Copy link
Member

dsherret commented Jan 30, 2024

Seems reasonable if this is opt-in via a config property or cli flag. I'm wondering if it's worth it to have plugins say whether they can format a shebang. Maybe this is knowledge that should just live in the cli for simplicity as a shebang to extension mapping.

@dsherret dsherret added enhancement New feature or request and removed suggestion labels Jan 30, 2024
@bradzacher
Copy link

Yeah that sounds like the best option, I don't think dprint should do this by default as not every codebase is Unix or uses hashbang files, and I don't think dprint itself should have a baked-in mapping considering hashbangs can take any form - but a user-configurable option to opt-in sounds really reasonable.

The user could specify a mapping of hashbang to extension, maybe? First thought would be something like

{
  "hashbangs": {
    "#!/usr/bin/env bash": ".sh",
    "#!/usr/bin/env node": ".js"
  }
} 

Then dprint could probably essentially do path + mappedExt to resolve the file against the config and format it.

@dsherret
Copy link
Member

dsherret commented Jan 30, 2024

I like that. Takes the complexity out of handling different scenarios and leaves it up to the user to have full control.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants