Open shonfeder opened 5 months ago
If no
.ocamlformat
file is present andocamlformat
is not installed, thendune build @fmt
should run all other formatting rules associated with the@fmt
alias (e.g., formatting dune files, or running custom formatting rules) and then exit cleanly.
I'm not sure about that. The formatting rules are set up by default for the ocaml
dialect, and these depend on ocamlformat
. If that's not wanted for a project, it's possible to disable formatting rules for ocaml files.
Here is the current documentation of the @fmt
alias:
@fmt
====
This alias is used by formatting rules: when it is built, code formatters will
be executed (using :doc:`promotion </concepts/promotion>`).
``dune fmt`` is a shortcut for ``dune build @fmt --auto-promote``.
It is possible to build on top of this convention. If some actions are manually
attached to the ``fmt`` alias, they will be executed by ``dune fmt``.
Example:
.. code:: dune
(rule
(with-stdout-to
data.json.formatted
(run jq . %{dep:data.json})))
(rule
(alias fmt)
(action
(diff data.json data.json.formatted)))
note that ocamlformat
is not mentioned at all. IMO, expectation that the @fmt
alias does not require ocamlformat
in the presence of .ml
files is natural given the documentation. If users are meant to expect alias to fail because they have not installed a particularly binary, I think that should be noted very prominently in the documentation. If that is the design decision you go with, then I'd consider correcting/updating the docs to close this issue.
IMO, an explicit opt-in approach (whether indicated by the presence of an .ocamlformat
file or some flag in the dune-project
) would be more compatible with https://github.com/ocaml/dune/issues/3836 . I would also just find it to be less surprising behavior: currently, I cannot set up a simple, explicit rule to format, e.g., a json file, and have that associated with @fmt
without first adding an additional configuration saying I don't want to format something else that I never asked for! :)
My perspective is a bit different on this. If I have formatting for OCaml files enabled (which is the default since Dune 3.0 IIRC) and call dune build @fmt
I would expect:
As an analogy, if we take dune build
and have no ocaml
, should dune build
return 0 despite it not actually having built anything? I believe we all agree that this behavior would be quite surprising, bordering on malicious compliance territory.
My proposal on the other hand would be to update the documentation and state that @fmt
will depend on ocamlformat
in the presence of .ml
files and refmt
in the presence of .re
files. In the future, Dune might be able to retrieve and build these tools on its own (#10647) but silently doing the wrong thing seems to me to be a bad move.
Lots of interesting discussion here, but to summarize a few key points:
Aliases already have well defined semantics. Adding ad-hoc rules like:
should run all other formatting rules associated with the @fmt alias (e.g., formatting dune files, or running custom formatting rules) and then exit cleanly.
Isn't an option. A couple of other options to explore:
dune fmt
.Or maybe using aliases for formatting is not the right module for formatting in the first place?
I would also just find it to be less surprising behavior: currently, I cannot set up a simple, explicit rule to format, e.g., a json file, and have that associated with @fmt without first adding an additional configuration saying I don't want to format something else that I never asked for! :)
I understand the concern, but unfortunately a default is always going to leave a subset of users unsatisfied. Our default was chosen based on what would be the appropriate behavior for the majority of users.
My proposal on the other hand would be to update the documentation and state that @fmt will depend on ocamlformat in the presence of .ml files and refmt in the presence of .re files
That makes sense to me.
Expected Behavior
If no
.ocamlformat
file is present andocamlformat
is not installed, thendune build @fmt
should run all other formatting rules associated with the@fmt
alias (e.g., formatting dune files, or running custom formatting rules) and then exit cleanly.Actual Behavior
Assuming you have first uninstalled any ocamlformat in your path and you are in a dune project with an
.ml
file:Reproduction
https://github.com/ocaml/dune/pull/10579
Specifications
dune
(output ofdune --version
): 3.15.0, 3.15.2, 3,15.3, and on 0a3f50aac7db87650e66b7a6ca5b13722b5b6c60 (the current head).ocaml
(output ofocamlc --version
):4.14.1
and4.08
6.6.30-2-MANJARO
anddebian-12-4.08
Additional information
To my mind the main reason why we shouldn't want such a tight coupling between
dune build @fmt
andocamlformat
is that it unnecessarily limits the@fmt
alias, which is still useful via custom rules and formatting of dune files.But, it is also causing problems for ocaml-ci, as per https://github.com/ocurrent/ocaml-ci/issues/936
With the current behavior, we either have to burden every CI job with an install of ocamlformat, even when its not relevant, or we cannot support any formatting at all via the
@fmt
alias. If we decide for the former course, then we risk breakage of a future version ofocamlformat
starts deciding it will run formatting without requiring an.ocamlformat
file at the root.Related to #3836