diff --git a/src/dune_rules/format_rules.ml b/src/dune_rules/format_rules.ml index 17f66f17a93..9a8ce5bd0a1 100644 --- a/src/dune_rules/format_rules.ml +++ b/src/dune_rules/format_rules.ml @@ -90,14 +90,19 @@ 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 let open Action_builder.With_targets.O in (* Depend on [extra_deps] so if the ocamlformat config file changes then ocamlformat will run again. *) - extra_deps dir >>> action + extra_deps dir + >>> action + |> With_targets.map ~f:(Action.Full.add_sandbox Sandbox_config.needs_sandboxing) ;; let action_when_ocamlformat_isn't_locked ~input kind = diff --git a/test/blackbox-tests/test-cases/pkg/ocamlformat/gh10991.t b/test/blackbox-tests/test-cases/pkg/ocamlformat/gh10991.t index 44a69d9b498..8c125460e05 100644 --- a/test/blackbox-tests/test-cases/pkg/ocamlformat/gh10991.t +++ b/test/blackbox-tests/test-cases/pkg/ocamlformat/gh10991.t @@ -44,7 +44,7 @@ Update the file: Promoting _build/default/.formatted/foo.ml to foo.ml. [1] -After formatting a second time, the recent change to the file was ignored: +The update to the file persists after formatting it a second time: $ cat foo.ml - let () = print_endline "Hello, world" + let () = print_endline "Hello, ocaml!" (* formatted with fake ocamlformat *)