Skip to content

Commit

Permalink
fix: Return a user errormessage when OCAMLFIND_CONF unset / no ocamlf…
Browse files Browse the repository at this point in the history
…ind in PATH

Signed-off-by: Antonio Nuno Monteiro <[email protected]>
  • Loading branch information
anmonteiro committed Mar 29, 2023
1 parent cecbdd6 commit 668b185
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/dune_rules/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -298,17 +298,27 @@ module Build_environment_kind = struct
| Some s -> Opam2_environment s
| None -> Unknown)))

let findlib_paths ocamlfind ~kind ~context ~findlib_toolchain ~env ~dir =
let findlib_paths ocamlfind ~kind ~findlib_toolchain ~env ~dir =
match (ocamlfind, query ~kind ~findlib_toolchain ~env) with
| ( Some ocamlfind
, ( Cross_compilation_using_findlib_toolchain _
| Opam2_environment _
| Unknown ) ) -> Ocamlfind.conf_path ocamlfind
| None, Cross_compilation_using_findlib_toolchain _toolchain ->
Code_error.raise
"Could not find ocamlfind in PATH or an environment variable \
OCAMLFIND_CONF"
[ ("context", Context_name.to_dyn context) ]
| None, Cross_compilation_using_findlib_toolchain toolchain ->
User_error.raise
[ Pp.textf
"Could not find ocamlfind in PATH or an environment variable \
`OCAMLFIND_CONF' while cross-compiling with toolchain `%s'"
(Context_name.to_string toolchain)
]
~hints:
[ Pp.enumerate
[ "`opam install ocamlfind' and/or:"
; "Point `OCAMLFIND_CONF' to the findlib configuration that \
defines this toolchain"
]
~f:Pp.text
]
| _, Hardcoded_path l ->
List.map l ~f:Path.of_filename_relative_to_initial_cwd
| None, Opam2_environment opam_prefix ->
Expand Down Expand Up @@ -413,7 +423,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
in
let build_dir = Context_name.build_dir name in
let default_ocamlpath =
Build_environment_kind.findlib_paths ocamlfind ~kind ~context:name ~env
Build_environment_kind.findlib_paths ocamlfind ~kind ~env
~findlib_toolchain ~dir
in
let ocaml_config_ok_exn = function
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
Dune shows an error message when ocamlfind isn't in PATH and OCAMLFIND_CONF
isn't set

$ cat > dune-project <<EOF
> (lang dune 3.7)
> (package (name repro))
> EOF
$ cat > dune <<EOF
> (executable
> (name gen)
> (modules gen)
> (enabled_if
> (= %{context_name} "default"))
> (libraries libdep))
> (rule
> (with-stdout-to
> gen.ml
> (echo "let () = Format.printf \"let x = 1\"")))
> (library
> (name repro)
> (public_name repro)
> (modules foo))
> EOF

$ mkdir ocaml-bin
$ cat > ocaml-bin/ocamlc <<EOF
> #!/usr/bin/env sh
> export PATH="\$ORIG_PATH"
> exec ocamlc "\$@"
> EOF
$ chmod +x ocaml-bin/ocamlc

$ DUNE_PATH=$(dirname `which dune`)
$ SH_PATH=$(dirname `which sh`)
$ env ORIG_PATH="$PATH" PATH="$SH_PATH:$DUNE_PATH:$PWD/ocaml-bin" dune build @install -x foo
Error: Could not find ocamlfind in PATH or an environment variable
`OCAMLFIND_CONF' while cross-compiling with toolchain `foo'
Hint:
- `opam install ocamlfind' and/or:
- Point `OCAMLFIND_CONF' to the findlib configuration that defines this
toolchain
[1]

0 comments on commit 668b185

Please sign in to comment.