Skip to content

Commit

Permalink
Merge pull request #11202 from Julow/nix-result-fmt-crash
Browse files Browse the repository at this point in the history
test: Repro 'dune fmt' crash in presence of Nix result
  • Loading branch information
rgrinberg authored Feb 8, 2025
2 parents 01ec6fd + 029d26e commit 5cdf5e8
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/dune_engine/hooks.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 7 additions & 1 deletion src/promote/diff_promotion.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
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
[1]

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

$ chmod -R u+w "$RESULT"

0 comments on commit 5cdf5e8

Please sign in to comment.