Is it possible to use a git repo for `opam switch`'s compiler?

I can’t compile OCaml 4.06.1 on Fedora 32 because of a change in gcc’s default error settings, and I can’t quickly switch to a different compiler version for various reasons.

What I’m wondering is, can I fork the OCaml repo, add a couple extern's to the source code, and then tell opam to use my git repo as the source for a switch?

I found a hacky workaround where I initialize a broken switch, pin my branch, and then reinstall ocaml-base-compiler:

opam switch create 4.06.1 # wait for this to fail
opam pin add ocaml-base-compiler.4.06.1 git+

Is there a better way to do this?

You can use opam switch create <switch name> --empty instead of waiting for the normal command to fail.
Then the pin command should work, although I tend to pin ocaml-variants instead of ocaml-base-compiler.
I think it is recommended to also use opam switch set-base after that, but I don’t use it in my workflow so I don’t remember exactly the right arguments (without set-base, it will consider your compiler like a regular package and will prompt you to upgrade it everytime your git repo is updated, while with set-base it will not upgrade it unless you ask for it explicitly by passing the --unlock-base option).

1 Like

It turns out I have a need to do this very thing, in order to work on a couple of pretty-printing bugs in the base compiler. Do you have a script you could paste, that I could use? I confess that opam, while incredibly useful, is a bit of a black-box to me, and after a few tries to get this working (along the lines of your suggestion) I’m not making any progress.

Normally I’d just unpack a compiler-tree and build it manually, but if there were a way to do it with opam, that would be very, very nice.

@Chet_Murthy can’t you make these instructions work ?

1 Like

If you want to rely on opam to fetch the sources for you, there’s a command for that (opam source) but unfortunately it will only fetch an archive of the sources, which will be inconvenient when you want to submit your patches upstream.

Otherwise, @dbuenzli’s link should have the commands you need. If it still doesn’t work for you, please detail what steps you took, what you expected, and what you got, and I’ll try to help you fix the problems.

You forgot about opam source --dev which clones you directly the repo there for you.

I was under the impression that the compiler’s opam files did not define a dev-repo field, so this wouldn’t work on the compiler itself.

1 Like

Oh, lovely! Thank you! I had searched the opam documentation, but didn’t think to search the ocaml not-the-manual-but-still-docs.

Yes, it is indeed working, as predicted.