ocamlformat icon indicating copy to clipboard operation
ocamlformat copied to clipboard

Multi version support with binary selection

Open EmileTrotignon opened this issue 1 year ago • 4 comments

This gives ocamlformat multiversion support.

The workflow would be the following :

When you opam install ocamlformat.0.27.4, you get two packages : ocamlformat with the ocamlformat executable that does not format anything. You also get ocamlformat-0-27-1 that has the executable that does the formatting, named ocamlformat-0-27-1.

Then when you try to format a project that requires version 0.15.3, the ocamlformat executable goes looking for an ocamlformat-0-15-3 executable. If it is found, it is executed, and if it is not, instruction to do opam install ocamlformat-0-15-3 are printed.

The objective is to have the maximum number of ocamlformat-%version% packages be co-installable, but that might not be possible due to dependency conflicts.

There will be some work to repackage previous version along these lines, but it should be doable.

Regarding implementation, I chose to use the same pipeline that executes ocamlformat for real to fetch the required version. This allows me to ensure that the logic to, for instance, get the config file, is the same. Minor changes where required to disable warning because they were otherwise printed twice.

I have not touched the RPC at all, I would like some help on that front.

EmileTrotignon avatar Mar 25 '24 13:03 EmileTrotignon

The test fail if run cold after a dune clean. If you run dune build first, they work. This needs to be fixed.

EmileTrotignon avatar Mar 29 '24 16:03 EmileTrotignon

What's the relationship with https://github.com/CraigFe/ocamlformat-vmux ?

samoht avatar Apr 05 '24 18:04 samoht

@samoht

What's the relationship with https://github.com/CraigFe/ocamlformat-vmux ?

It is inspired from it, but the big difference is that here we are laying the ground work for having multiple ocamlformat versions on the same switch, which can only be done inside ocamlformat.

Our fix is also slightly less hackish : it uses the path, and therefore does not assume anything about opam switches. This could mean higher compatibility with the platform installer, or in the future, dune package management (we are going to double check for the latter).

This will allow for a familiar workflow were when you do not have the right ocamlformat version installed, you can solve the issue with a single opam install command (and you are given this command in the error message).

We also use ocamlformat as lib to locate the config file which makes this more maintainable : if we change the way the config file is located, the version selector will be updated as well, and we automatically cover corner-cases for that.

I think Craig's solution is great for people who dig around and are quite familiar with the ocaml ecosystem, we intend to bring a fix that is going to be usable without any prior knowledge.

EmileTrotignon avatar Apr 08 '24 08:04 EmileTrotignon

As I mentioned in our conversation, it seems this would be addressed naturally as Dune package management arrives, since Dune would do the "multi-version management" for you.

If you implement this, Dune will likely need to find and use the ocamlformat-<vsn> binary/package directly anyway.

leostera avatar Apr 15 '24 06:04 leostera

This would be too difficult to maintain and dune package management would solve the issue, so I am closing it.

EmileTrotignon avatar Jul 26 '24 07:07 EmileTrotignon