Skip to content

Commit

Permalink
fix: correctly handle absolute paths in dune ocaml top-module
Browse files Browse the repository at this point in the history
Signed-off-by: Ali Caglayan <alizter@gmail.com>
  • Loading branch information
Alizter authored and rgrinberg committed Jan 23, 2024
1 parent c50e892 commit a8b63f7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
25 changes: 19 additions & 6 deletions bin/ocaml/top.ml
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,10 @@ module Module = struct
let term =
let+ builder = Common.Builder.term
and+ module_path =
Arg.(required & pos 0 (some string) None & Arg.info [] ~docv:"MODULE")
Arg.(
required
& pos 0 (some string) None
& Arg.info [] ~docv:"MODULE" ~doc:"Path to an OCaml module.")
and+ ctx_name = Common.context_arg ~doc:{|Select context where to build/run utop.|} in
let common, config = Common.init builder in
Scheduler.go ~common ~config (fun () ->
Expand All @@ -203,12 +206,22 @@ module Module = struct
in
let+ directives =
let module_path =
let root = Common.root common in
Path.Source.relative
Path.Source.root
(root.reach_from_root_prefix ^ module_path)
if Filename.is_relative module_path
then Path.Local.of_string module_path
else (
let root =
(Common.root common).dir
|> Path.of_string
|> Path.to_absolute_filename
|> Path.of_string
in
match Path.drop_prefix ~prefix:root (Path.of_string module_path) with
| Some module_path -> module_path
| None ->
User_error.raise
[ Pp.text "Module path not a descendent of workspace root." ])
in
module_directives sctx module_path
module_directives sctx (Path.Source.of_local module_path)
in
Dune_rules.Toplevel.print_toplevel_init_file directives))
;;
Expand Down
2 changes: 2 additions & 0 deletions doc/changes/8249.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fix `dune ocaml top-module` to correctly handle absolute paths. (#8249, fixes
#7370, @Alizter)
17 changes: 9 additions & 8 deletions test/blackbox-tests/test-cases/top-module/load-from-lib.t
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ We try to load a module defined in a library with a dependnecy
$ ls _build/default/mydummylib/*.cma
_build/default/mydummylib/mydummylib.cma

$ dune ocaml top-module $PWD/foo/foo.ml 2>&1 | head -n7
Internal error, please report upstream including the contents of _build/log.
Description:
("Local.relative: received absolute path",
{ t = "."
; path =
"$TESTCASE_ROOT/foo/foo.ml"
})
$ dune ocaml top-module $PWD/foo/foo.ml
#directory "$TESTCASE_ROOT/_build/default/.topmod/foo/foo.ml";;
#directory "$TESTCASE_ROOT/_build/default/mydummylib/.mydummylib.objs/byte";;
#load "$TESTCASE_ROOT/_build/default/mydummylib/mydummylib.cma";;
#load "$TESTCASE_ROOT/_build/default/foo/.foo.objs/byte/foo__.cmo";;
#load "$TESTCASE_ROOT/_build/default/foo/.foo.objs/byte/foo__Bar.cmo";;
#load "$TESTCASE_ROOT/_build/default/.topmod/foo/foo.ml/foo.cmo";;
open Foo__
;;

0 comments on commit a8b63f7

Please sign in to comment.