Getting list of installed deps and versions with dune

I have spent some time on multiple occasions trying to figure out what libs are installed in a dune project so that I might lock them.

I can’t seem to find this information anywhere.

Lets say you have some random project you were checking out. You run it and it works or it doesnt and you fix it and it works. You now want to lock the dependencies.

How do you get a list of the dependencies in being used in the current project in the current directory.

I figured out that if I run opam install dep it will tell me it is already installed with the version with I can then add to the dune-project file. This is obviously extremely tedious.

How do the pros do it?

Thank you.

opam list will list all of the packages you’ve installed in the current switch, but it sounds like what you actually want is opam lock ., which will produce an opam file that explicitly specifies the versions of all of the packages depended upon in the “primary” opam file.

Projects should be listing their dependencies in their opam files. Check if the project has an opam file. If it doesn’t, you will need to request the project maintainer to create one (or create it yourself).

If the project has an opam file, check opam lock --help for instructions on getting a lock file.

Is there an equivalent dune version to update dune-project?

I don’t quite understand what you mean. The dune-project file has the ability to store some of the opam project manifest data, and dune can generate opam files from that. If the project is using that option, then it should be committing the auto-generated opam file in the repo, in which case the opam lock command should still work to generate the lockfile.

If any dependencies are changed in the dune-project manifest, then the lockfile will need to be updated as well (by building the project to generate the new version of the opam file, then generating the lockfile again).

Ah, right I guess I misunderstood what opam lock was doing - I was hoping there were some kind of command to automatically update the dune-project file’s project manifest data based on the required libraries in the dune file.

I was hoping there were some kind of command to automatically update the dune-project file’s project manifest data based on the required libraries in the dune file.

opam dune-lint will do this. It checks that every library listed in a dune file has a corresponding entry in your opam files and adds them if not (defaulting to the currently-installed version as a lower-bound). If you’re generating the opam files from dune-project, it will add them there instread.

So I guess you could just remove all the existing entries and run it to get all the current versions added.

2 Likes

Oh man, this is exactly what I was looking for.

One of my little annoyances with the OCaml ecosystem is how manual some of the dune setup is, and that I have to constantly keep referring back to the dune-read-the-docs webpage to do fairly mundane tasks.

Out of curiosity, how exactly does opam-dune-lint integrate with the main opam command - as in, how have you configured it such that opam dune-lint calls out to opam-dune-lint - is this somehow hardcoded into opam itself or can arbitrary packages do the same?

1 Like

If you put flags: plugin in your opam file then opam will consider it to be a plugin (see opam - Manual).

1 Like