Dune libraries stanza leakage

Here is something that has been puzzling me. I have a multi-library package, with a bunch of different dune files for each library. The package itself has a number of dependencies, and then the libraries specify their individual dependenceis on these by including them in the libraries dune stanza.

I had assumed that if there is not an entry for foo in the libraries stanza of a given dune library, then the code of foo will not be in scope within that library. This seems to me to be a rather important hygiene property, and it really surprised me to find that it is false. As it happens, I can remove certainly libraries from the libraries stanza and their code continues to be in scope.

One hypothesis I had is that the libraries may have transitive semantics (so if I depend on a library bar that depends on foo, then I will have the content of foo in scope). If that is what is happening, it does make sense from an implementation point of view, but I think it does not make sense from a semantical point of view, nor from the point of view of dune files as documenting a library’s input interface in the world of separate compilation.

I am curious if this behaviour would be thought of as a bug, or a design decision — and, if the latter, whether there is some appetite to change the behaviour to allow users of dune to maintain clear and direct control over scope without ‘spooky action at a distance’.

Indeed, by default, (libraries ...) has transitive semantics. This can be switched to non-transitive semantics by putting (implicit_transitive_deps false) in your dune-project file. However, note that if you use this setting with versions of OCaml < 5.2, you may run into some issues (which is why this setting has never been the default). However, if you are on OCaml >= 5.2, then the new setting should work just fine (assuming you are using Dune language version >= 3.17).

See https://github.com/ocaml/dune/pull/10644 and the links therein if you want to learn more about the context.

Cheers,
Nicolas

4 Likes

This is fantastic, thanks for letting me know about the new feature! (EDIT: it works great!)