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

Auto format dune-project files with dune fmt #10716

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion src/dune_engine/load_rules.mli
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ end
(** Load the rules for this directory. *)
val load_dir : dir:Path.t -> Loaded.t Memo.t

(** Return the rule that has the given file has target, if any *)
(** Return the rule that has the given file as target, if any *)
val get_rule : Path.t -> Rule.t option Memo.t

(** Return the definition of an alias. *)
Expand Down
37 changes: 26 additions & 11 deletions src/dune_rules/format_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,27 @@ module Alias = struct
let fmt ~dir = Alias.make Alias0.fmt ~dir
end

let add_diff_rule ~sctx ~loc ~alias_formatted ~dir ~output_dir ~version path =
let open Memo.O in
let input_basename = Path.Source.basename path in
let input = Path.build (Path.Build.relative dir input_basename) in
let output = Path.Build.relative output_dir input_basename in
(let open Action_builder.O in
let+ () = Action_builder.path input in
Action.Full.make (action ~version input output))
|> Action_builder.with_file_targets ~file_targets:[ output ]
|> Super_context.add_rule sctx ~mode:Standard ~loc ~dir
>>> add_diff sctx loc alias_formatted ~dir ~input ~output
;;

let gen_rules_output
sctx
(config : Format_config.t)
~version
~dialects
~expander
~output_dir
~project
=
assert (formatted_dir_basename = Path.Build.basename output_dir);
let loc = Format_config.loc config in
Expand Down Expand Up @@ -133,16 +147,17 @@ let gen_rules_output
Source_tree.Dir.dune_file source_dir
|> Memo.Option.iter ~f:(fun f ->
Dune_file0.path f
|> Memo.Option.iter ~f:(fun path ->
let input_basename = Path.Source.basename path in
let input = Path.build (Path.Build.relative dir input_basename) in
let output = Path.Build.relative output_dir input_basename in
(let open Action_builder.O in
let+ () = Action_builder.path input in
Action.Full.make (action ~version input output))
|> Action_builder.with_file_targets ~file_targets:[ output ]
|> Super_context.add_rule sctx ~mode:Standard ~loc ~dir
>>> add_diff sctx loc alias_formatted ~dir ~input ~output)))
|> Memo.Option.iter
~f:(add_diff_rule ~sctx ~loc ~alias_formatted ~dir ~output_dir ~version)))
and* () =
match Format_config.includes config Dune with
| false -> Memo.return ()
| true ->
Dune_project.file project
|> Memo.Option.iter ~f:(fun path ->
if String.equal "_build/default" (Path.Build.to_string dir)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not happy with this if check for the hard-coded build directory, but I didn't find a way to check if we are in the "_build/default" directory other than this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about checking it for equality against Context.build_dir?

then add_diff_rule ~sctx ~loc ~alias_formatted ~dir ~output_dir ~version path
else Memo.return ())
in
Rules.Produce.Alias.add_deps alias_formatted (Action_builder.return ())
;;
Expand Down Expand Up @@ -181,7 +196,7 @@ let gen_rules sctx ~output_dir =
let* project = Dune_load.find_project ~dir in
let dialects = Dune_project.dialects project in
let version = Dune_project.dune_version project in
gen_rules_output sctx config ~version ~dialects ~expander ~output_dir)
gen_rules_output sctx config ~version ~dialects ~expander ~output_dir ~project)
;;

let setup_alias ~dir =
Expand Down
20 changes: 20 additions & 0 deletions test/blackbox-tests/test-cases/formatting/feature.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,23 @@ settings as dictated by the dune language version.
> ;; formatting disabled by default
> EOF
$ (cd using-env && dune build @fmt)

dune fmt automatically formats dune-project files.

$ mkdir dune-project-files
$ touch dune-project-files/.ocamlformat
$ cat > dune-project-files/dune-project << EOF
> (lang dune 3.16)
> (name
> format_project)
> EOF
$ (cd dune-project-files && dune fmt)
File "dune-project", line 1, characters 0-0:
Error: Files _build/default/dune-project and
_build/default/.formatted/dune-project differ.
Promoting _build/default/.formatted/dune-project to dune-project.
[1]
$ cat dune-project-files/dune-project
(lang dune 3.16)

(name format_project)
Loading