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

test: Repro 'dune fmt' crash in presence of Nix result #11202

Merged
merged 2 commits into from
Feb 8, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/dune_engine/hooks.ml
Original file line number Diff line number Diff line change
@@ -26,9 +26,13 @@ module Make () = struct
match !exns with
| [] -> ()
| exns ->
let exns = List.rev exns in
let open Dyn in
Code_error.raise "hooks failed" [ "exns", (list Exn_with_backtrace.to_dyn) exns ]
(match exns with
| [ { exn = User_error.E _ as e; backtrace = _ } ] -> raise e
| _ ->
let exns = List.rev exns in
Code_error.raise
"hooks failed"
[ "exns", (Dyn.list Exn_with_backtrace.to_dyn) exns ])
;;
end

8 changes: 7 additions & 1 deletion src/promote/diff_promotion.ml
Original file line number Diff line number Diff line change
@@ -52,7 +52,13 @@ module File = struct
let do_promote ~correction_file ~dst =
Path.Source.unlink_no_err dst;
let chmod = Path.Permissions.add Path.Permissions.write in
Io.copy_file ~chmod ~src:correction_file ~dst:(Path.source dst) ()
match Io.copy_file ~chmod ~src:correction_file ~dst:(Path.source dst) () with
| () -> ()
| exception Unix.Unix_error (e, _, _) ->
User_error.raise
[ Pp.textf "failed to promote %s" (Path.Source.to_string dst)
; Pp.text (Unix.error_message e)
]
;;

let correction_file { src; staging; _ } = Path.build (Option.value staging ~default:src)
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(executable
(name ocamlformat))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(lang dune 2.8)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(* Avoid adding dependencies to this cram test *)
let () = print_endline "(* formatted *)"
22 changes: 22 additions & 0 deletions test/blackbox-tests/test-cases/read-only-symlink-target.t/run.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Nix can leave a symlink to a store path in the tree, often called 'result'.
'dune fmt' crashes because of that.

$ RESULT=`mktemp -d`
$ echo "let x = 2" > "$RESULT/foo.ml"
$ chmod -R a-w "$RESULT"
$ ln -s "$RESULT" result

This command should succeed:

$ dune fmt
File "result/foo.ml", line 1, characters 0-0:
Error: Files _build/default/result/foo.ml and
_build/default/result/.formatted/foo.ml differ.
Promoting _build/default/result/.formatted/foo.ml to result/foo.ml.
Error: failed to promote result/foo.ml
Permission denied
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the fix! With this output, it's clearer that removing result would fix the issue.
But couldn't Dune avoid this issue ? Formatting files that are outside the workspace is surprising but could be part of someone's workflow. However, formatting files that are read-only feels like something Dune could avoid without bothering anyone.

Copy link
Member

Choose a reason for hiding this comment

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

However, formatting files that are read-only feels like something Dune could avoid without bothering anyone.

That's an interesting suggestion. It does seem like an improvement for the current behavior. There's no need to setup rules if we know they're good to fail. Do you want to try implementing this?

[1]

Allow Dune to remove temporary files (calling Dune crashes without this):

$ chmod -R u+w "$RESULT"