dune icon indicating copy to clipboard operation
dune copied to clipboard

Automatically add opam dependencies to dune-project file

Open yawaramin opened this issue 2 years ago • 2 comments

In a dune project, automatically infer opam dependencies and add them to the dune-project file. This will save users from the drudge work of having to write these dependency lists in two different files since dune can just infer one of them.

Opam dependencies may be inferred by looking at the contents of the (libraries ...) field in dune files. Dune can then add these libraries into the (depends ...) field of the (package ...) stanza of the dune-project file. Ideally, with properly set version bounds, but to start with, it should be acceptable to add only the library names.

How to know which dependencies are opam libraries? Process of elimination. There's a limited set of libraries shipped with the OCaml distribution: str, unix, etc. We can eliminate those as coming from opam. Another set of libraries are defined in the project itself. We can eliminate those. The ones that are left over must then be from opam.

How to decide which (package ...) of the dune-project file to add the library dependencies into? The same way dune knows which library is in which package right now–using the (public_name ...) and (package ...) fields.

yawaramin avatar Mar 30 '23 02:03 yawaramin

To make this command/flag/option less intrusive, dune can ignore the deps that are already listed (with and without their versions), and merely append.

It’d be nice too if it also sorts alphabetically.

~May attempt this myself.~

dangdennis avatar Apr 12 '23 04:04 dangdennis

If I understand the feature request here correctly, it looks like the functionality here can be achieved using opam-dune-lint tool.

punchagan avatar Sep 06 '24 06:09 punchagan

IIUC, one complication is that opam files do not specify which libraries are provided by a package. IIUC, https://github.com/ocurrent/opam-dune-lint gets around this by assuming that the dependencies have been installed. But, in the general case (and in particular with dune pkg) dependencies may not be installed until after they have been added as deps to dune-project.

I think the situation is further complicated, in the general case, by the fact that multiple packages can provide libraries with the same name, and multiple libraries can provide modules with the same names...

shonfeder avatar Nov 18 '25 00:11 shonfeder

Realistically, what this ticket proposes cannot be done because the source of truth is the project file. This is what the user must write so that dune even has a chance of knowing what it can suggest to the user after it has downloaded the dependencies. On the flip side, it does mean that we can automate, or at least provide helpful error messages when things are missing from the libraries field. This will require some rather ugly parsing of error messages from the compiler, but at least it can be done.

rgrinberg avatar Nov 18 '25 14:11 rgrinberg