ryskajakub
ryskajakub

Reputation: 6431

Disable optional dependency installation in ocaml opam

Docs say:

A package can also have optional dependencies. These are dependencies that the package can make use of, but that are not mandatory. They will not be installed with the package by default, but opam install will take advantage of them if they are already installed while installing a package that optionally depends on them. For example, if you install react before installing lwt, opam install lwt will configure lwt to use react, but just installing lwt will not install react.

Can I somehow turn this off? By using this example of react and lwt, even though I have ceat installed, I don't want lwt to be configured with react when installing lwt afterwards.

Upvotes: 2

Views: 279

Answers (1)

ivg
ivg

Reputation: 35210

Technically it is possible, as opam provides enough options for a packager to expose to a user full control on the package configuration. In practice, it is rarely, if ever, used.

Packager Options

As a package maintainer, you can use opam's global and switch configurations to let the users decide which optional parts of your package they want. Alternatively, you can introduce an extra bridge package between the optional dependency and your package, to break the hard connection, e.g., instead of reconfiguring lwt when react is present, it could be reconfigured only if a pseudo-package, e.g., conf-lwt-enbale-react is installed, which in turn depends on the react package.

User Options

If the package in which you're interested doesn't provide enough control for you, then you have a couple of workarounds, but ideally, you should contact the package maintainer with a request to fix your package1. In any case you should adopt the role of the packager, at least temporary.

The first option is to download the package source, modify the opam file, and pin the fixed version, e.g.,

opam source lwt  
edit <lwt-source>/opam
opam pin add lwt <lwt-source>

where <lwt-source> is the name of directory to which the opam source downloaded the souce code.

An alternative solution would be to copy the opam-repository, fix it there, and then add your repository to opam, e.g.,

git clone https://github.com/ocaml/opam-repository
edit opam-repository/packages/<your-package-opam-file>
opam repo add fixed-deps ./opam-repository

General Guidelines for packagers (opinionated)

When an optional dependency adds new functionality, without changing the core behavior, i.e., when the change is pure additive, it is more or less ok to use the optional dependencies. However, remember, that this could be quite annoying for users, especially for such core packages as lwt (which may involve tons of recompilation and a cascade of changes or even failures). Therefore, it is better to refrain from using this feature and stick to a solution that uses explicit packages that manage different functionality pieces of your codebase.

If the optional dependency changes the core behavior of your package, i.e., if it is not an extension, but a modification, then it should never be represented with the opam optional dependency mechanism and instead explicitly denoted as a separate package or using configuration variables.


1) Remember, that the best request is the pull request :)).

Upvotes: 1

Related Questions