Skip to content

Commit

Permalink
pkg: fix dev-tool bug where dune fmt would revert file
Browse files Browse the repository at this point in the history
The rule for running ocamlformat on source files when dev-tools are in
use did not depend on the input file, so changes to the input file
wouldn't cause ocamlformat to be re-run on the updated file. The
consequence of this is that `dune fmt` would promote stale versions of
files.

Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
  • Loading branch information
gridbugs committed Oct 14, 2024
1 parent 154739e commit 40720f4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/dune_rules/format_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ module Ocamlformat = struct
(let open Action_builder.O in
(* This ensures that at is installed as a dev tool before
running it. *)
let+ () = Action_builder.path path in
let+ () = Action_builder.path path
(* Declare the dependency on the input file so changes to the input
file trigger ocamlformat to run again on the updated file. *)
and+ () = Action_builder.path (Path.build input) in
let args = [ flag_of_kind kind; Path.Build.basename input ] in
Action.chdir (Path.build dir) @@ Action.run (Ok path) args |> Action.Full.make)
in
Expand Down
8 changes: 4 additions & 4 deletions test/blackbox-tests/test-cases/pkg/ocamlformat/gh10991.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@ Initial file:
$ cat foo.ml
let () = print_endline "Hello, world"

Formatting failed because the input file was not copied into the sandbox.
$ DUNE_CONFIG__LOCK_DEV_TOOL=enabled dune fmt
Solution for dev-tools.locks/ocamlformat:
- ocamlformat.0.0.1
cat: foo.ml: No such file or directory
File "foo.ml", line 1, characters 0-0:
Error: Files _build/default/foo.ml and _build/default/.formatted/foo.ml
differ.
Promoting _build/default/.formatted/foo.ml to foo.ml.
[1]

No formatting occured because input file wasn't copied to the sandbox.
After formatting the fake ocamlformat has added a suffix:
$ cat foo.ml
let () = print_endline "Hello, world"
(* formatted with fake ocamlformat *)

Update the file:
Expand All @@ -45,6 +44,7 @@ Update the file:
Promoting _build/default/.formatted/foo.ml to foo.ml.
[1]

No formatting occured because input file wasn't copied to the sandbox.
The update to the file persists after formatting it a second time:
$ cat foo.ml
let () = print_endline "Hello, ocaml!"
(* formatted with fake ocamlformat *)

0 comments on commit 40720f4

Please sign in to comment.