Overriding transitive dependencies with a wrapped/modified version thereof?

or, “Is there any way to transparently wrap a module used by a dependency of yours, with a replacement module with compatible types?”

A concrete example: let’s say I wanted to modify the implementation of Cohttp_async.Client.post to add an additional header, no matter where it’s called from (including from code I don’t control; such as when it’s called by the sentry-ocaml library.)

(At the moment, the only way I can think of, is to actually modify the package in question, and add that modified package to our own opam mirror? That’s significantly untenable in practice; as in reality, there will be several HTTP libraries that will need wrapping — as well as dozens, or hundreds, of intermediate libraries that call into those various HTTP libraries.)

to avoid an xyproblem.info:

I’m doing a lot of work with OpenTelemetry; one of the core concepts therein is a W3C Trace Context header that needs to be propagated through one’s application, from in-bound HTTP requests, onwards to out-bound HTTP requests.

The general approach to this in the OTel space is so-called “automatic instrumentation” (ex. for JavaScript) — having common HTTP-interacting libraries swapped out from underneath you with versions that correctly propagate this HTTP header, without having to manually modify every single HTTP-related callsite in your entire application.

In OCaml, with much less dynamism, this is a significantly more difficult problem. /=

Unless you need to publish your package, in which case I think it will be easier from you to vendor the sources of the other packages, you don’t need a full mirror. You could have your own overlay repository (this is a repository containing a repo file and a packages folder). You can add this alongside the official opam repository but with higher priority (I think it is something like opam repo add custom --rank=1 --all repo_path_or_url), and you only need to drop in there your modified packages.