在 ocaml opam 中禁用可选的依赖项安装

Disable optional dependency installation in ocaml opam

文档说:

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.

我能以某种方式关闭它吗?通过使用这个react和lwt的例子,即使我安装了ceat,我也不希望在之后安装lwt时将lwt配置为react。

从技术上讲这是可能的,因为 opam 为打包程序提供了足够的选项来向用户公开对包配置的完全控制。实际上,它很少(如果有的话)被使用。

打包程序选项

作为包维护者,您可以使用 opam 的全局和开关配置让用户决定他们想要包的哪些可选部分。或者,您可以在可选依赖项和您的包之间引入一个额外的桥接包,以打破硬连接,例如,当 react 存在时不重新配置 lwt,只有在伪造的情况下才可以重新配置-package,例如,安装了 conf-lwt-enbale-react,这又取决于 react 包。

用户选项

如果您感兴趣的包没有为您提供足够的控制,那么您有几个解决方法,但理想情况下,您应该联系包维护者请求修复您的包1。在任何情况下,您都应该承担打包者的角色,至少是暂时的。

第一个方案是下载包源,修改opam文件,固定版本,如

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

其中 <lwt-source>opam source 下载源代码的目录名称。

另一种解决方案是复制 opam 存储库,在那里修复它,然后将您的存储库添加到 opam,例如,

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

打包者通用指南(自以为是)

当可选依赖项添加新功能,而不改变核心行为时,即当更改是纯附加时,使用可选依赖项或多或少是可以的。但是,请记住,这对用户来说可能会很烦人,尤其是对于像 lwt 这样的核心包(这可能涉及大量的重新编译和一连串的更改甚至失败)。因此,最好不要使用此功能并坚持使用显式包来管理代码库的不同功能部分的解决方案。

如果可选依赖项改变了包的核心行为,即,如果它不是扩展,而是修改,那么它不应该用 opam 可选依赖项机制表示,而是明确表示为单独的包或使用配置变量。


1) 请记住,最好的请求是拉取请求:))。