Skip to content

Commit

Permalink
fix: decoding dune-package crashes with (include_subdirs qualified) (#…
Browse files Browse the repository at this point in the history
…10269)

* test: add repro for #10264

This is only one part of the issue; the other part is more difficult to
test because it is related to `Modules.t` round-tripping correctly.

Signed-off-by: Etienne Millon <[email protected]>

* fix: make `Module.Kind.t` round-trip correctly

Signed-off-by: Etienne Millon <[email protected]>

* fix: make Modules.t round-trip

Fixes #10264

This part is harder to test because we can't easily load `dune-package`
files in tests, nor build `Modules.t` values.

Signed-off-by: Etienne Millon <[email protected]>

* changelog

Signed-off-by: Etienne Millon <[email protected]>

---------

Signed-off-by: Etienne Millon <[email protected]>
  • Loading branch information
emillon authored Mar 15, 2024
1 parent 6848420 commit b570e27
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
2 changes: 2 additions & 0 deletions doc/changes/10269.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- fix crash when decoding dune-package for libraries with `(include_subdirs
qualified)` (#10269, fixes #10264, @emillon)
2 changes: 1 addition & 1 deletion src/dune_rules/module.ml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ module Kind = struct
match next with
| None -> return (Alias [])
| Some _ ->
let+ path = Module_name.Path.decode in
let+ path = enter Module_name.Path.decode in
Alias path )
]
;;
Expand Down
2 changes: 2 additions & 0 deletions src/dune_rules/module.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ module Kind : sig
| Root

include Dune_lang.Conv.S with type t := t

val to_dyn : t -> Dyn.t
end

module Source : sig
Expand Down
3 changes: 2 additions & 1 deletion src/dune_rules/modules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,8 @@ module Group = struct
Module_name.Map.to_list_map modules ~f:(fun _ t ->
Dune_lang.List
(match t with
| Group g -> Dune_lang.atom "group" :: encode ~src_dir g
| Group g ->
Dune_lang.atom "group" :: Module_name.encode g.name :: encode ~src_dir g
| Module m -> Dune_lang.atom "module" :: Module.encode ~src_dir m))
;;

Expand Down
31 changes: 31 additions & 0 deletions test/expect-tests/module_tests.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
open Stdune
module Kind = Dune_rules.Module.Kind

(* See #10264 *)
let%expect_test "Module.Kind encoding round trip" =
let module_name s = Dune_rules.Module_name.of_string s in
let test k =
let ast = Kind.encode k in
let sexp = Dune_sexp.Ast.add_loc ~loc:Loc.none ast in
let decoded =
match Dune_lang.Decoder.parse Kind.decode Univ_map.empty sexp with
| r -> Ok r
| exception e -> Error e
in
let dyn =
Dyn.record
[ "ast", Dyn.string (Dune_sexp.to_string ast)
; "decoded", Or_exn.to_dyn Kind.to_dyn decoded
]
in
Dune_tests_common.print_dyn dyn
in
test Impl;
[%expect {| { ast = "impl"; decoded = Ok Impl } |}];
test (Alias []);
[%expect {| { ast = "alias"; decoded = Ok Alias [] } |}];
test (Alias [ module_name "A" ]);
[%expect {| { ast = "(alias (A))"; decoded = Ok Alias [ "A" ] } |}];
test (Alias [ module_name "A"; module_name "B" ]);
[%expect {| { ast = "(alias (A B))"; decoded = Ok Alias [ "A"; "B" ] } |}]
;;

0 comments on commit b570e27

Please sign in to comment.