fslang-design icon indicating copy to clipboard operation
fslang-design copied to clipboard

[style-guide] Presence of shebangs in scripts

Open mataha opened this issue 3 years ago • 6 comments

Writing F# scripts is a great way of, among other things, creating project tooling to improve the development and build processes, by e.g. managing git hooks or setting up CI. Saying that, the preferable, cross-platform way of invoking a F# script is to prepend it with a shebang directive (in this case #!/usr/bin/env -S dotnet fsi) and make it executable in order to run it on Linux distros, macOS, WSL et al.

However, this is not mentioned at all in the de-facto F# code formatting guidelines and, as a consequence, formatters implementing the coding conventions of F# remove it entirely. I believe this should not be done for F# scripts (.fsx), while code and signature files should be fine as they currently are.

Using e.g. Fantomas, the following:

#!/usr/bin/env -S dotnet fsi --warn:5 --warnon:1182 --warnaserror+

open System
// Rest of the code...

Is rewritten as:

open System
// Rest of the code...

That should not be the case.

mataha avatar Jul 02 '22 01:07 mataha

@nojaf fyi, might be interesting for you

vzarytovskii avatar Jul 02 '22 10:07 vzarytovskii

If the lexer/parser can capture this in the syntax tree, Fantomas can restore it.

nojaf avatar Jul 04 '22 08:07 nojaf

This can be captured, yes, we should treat this as an FCS/ fantomas bug

dsyme avatar Jul 06 '22 08:07 dsyme

What's the next step then? Do I file a bug report in Fantomas, then try to fix it before v5 hits?

mataha avatar Jul 15 '22 07:07 mataha

The next step would be to raise a PR to dotnet/fsharp that adds the shebang as ParsedHashDirective (I think).

You can open an issue in Fantomas, but you would get the same reply there. This doesn't hinder the v5 release, we would accept a fix at any time for this.

nojaf avatar Jul 15 '22 07:07 nojaf

Actually, after taking another look at this, it can be solved quite easily on Fantomas' side without any compiler changes.

Looking at the AST, the shebang is parsed as a line comment. There is a bug in Fantomas that doesn't recognize this as a line comment on a single line because it does not start with //.

@mataha, please report an issue on Fantomas' side. You can do this using the online tool.

nojaf avatar Jul 15 '22 07:07 nojaf

@dsyme this was implemented in Fantomas a while ago, do we still need to do anything here?

nojaf avatar Sep 30 '22 06:09 nojaf

This can be closed as completed.

cartermp avatar Sep 30 '22 06:09 cartermp