How to compile ocamlformat? Obvious steps missing?

Hi everyone :wave:

I’m trying to hack on ocamlformat. Unfortunately, the package doesn’t have instructions on how to build it. So I assume something default and obvious should work.

After looking at the CI config, I tried to do the following but without success :disappointed:

❯ opam switch create ocamlformat 4.14.1

<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><>  🐫
Switch invariant: ["ocaml-base-compiler" {= "4.14.1"} | "ocaml-system" {= "4.14.1"}]
lConstructing initial basis...

<><> Processing actions <><><><><><><><><><><><><><><><><><><><><><><><><><>  🐫
∗ installed base-bigarray.base
∗ installed base-threads.base
∗ installed base-unix.base
∗ installed ocaml-options-vanilla.1
⬇ retrieved ocaml-base-compiler.4.14.1  (
∗ installed ocaml-base-compiler.4.14.1
∗ installed ocaml-config.2
∗ installed ocaml.4.14.1
# Run eval $(opam env --switch=ocamlformat) to update the current shell environment

❯ eval $(opam env --switch=ocamlformat)

❯ opam install --deps-only -t .
[ERROR] Package conflict!
  * Missing dependency:
    - ocamlformat-lib < 0.25.1
    no matching version

No solution found, exiting

Am I missing something obvious? Does anyone know how to fix this problem and compile ocamlformat?

I’m on macOS Sonoma 14.2.

Wait, with this tweet, I got something working

opam switch create . --packages ocaml-system,dune --deps-only --no-install
dune subst
opam pin . --no-action
opam install . --deps-only --with-test
dune build

Thanks for your help, @lthms!

When running the first command from your instructions, I see the following error:

❯ opam switch create . --packages ocaml-system,dune --deps-only --no-install

<><> Installing new switch packages <><><><><><><><><><><><><><><><><><><><>  🐫
Switch invariant: ["ocaml-system" "dune"]
[ERROR] Could not determine which packages to install for this switch:
  * Missing dependency:
    - ocaml-system
    unmet availability conditions, e.g. 'sys-ocaml-version = "5.1.1"'

Is there some global configuration I should change? Should I be inside some switch or outside any switch at all before running this command? Or something else?

OCamlformat’s source code contains several packages where one (ocamlformat) depends on the other (ocamlformat-lib) and opam install --deps-only -t . considers them in the wrong order.

This should work:

opam install --deps-only -t ./ocamlformat-lib.opam ./ocamlformat.opam

@lthms @Juloo Thanks a lot for your help! The following commands helped me to finally compile ocamlformat successfully!

$ opam switch create ocaml-4.14 4.14.1
$ eval $(opam env --switch=ocaml-4.14 --set-switch)
$ opam install --deps-only -t ./ocamlformat-lib.opam ./ocamlformat.opam
$ make exe

make exe in this case is

dune build bin/ocamlformat/main.exe bin/ocamlformat-rpc/main.exe

I really appreciate your assistance in helping me to figure this out! It took a while even with you hints, and I don’t know how I could do it on my own :melting_face:

1 Like

Just for the sake of it, mostly this is because ocaml-system was a bad candidate for this one.

ocaml-system means “use whatever ocaml version comes with your package manager” and, apparently, is not even available on MacOS so… my bad for that.

I think just replacing ocaml-sytem by ocaml.5.1.1 in the script would have worked as well, that is

opam switch create . --packages ocaml.5.1.1,dune --deps-only --no-install
dune subst
opam pin . --no-action
opam install . --deps-only --with-test
dune build
1 Like