Dune: depending on local module "Library not found"

Under my project root I have two folders, one for library code and one for the final binary built from main.ml. Unfortunately I’m missing something since dune can’t seem to find the library code

File "bin/dune", line 3, characters 12-15:
3 |  (libraries lib))
                ^^^
Error: Library "lib" not found.
Hint: try: dune external-lib-deps --missing @@default

Here’s the folder structure:

.
|-- bin
|   |-- dune
|   |-- dune-project
|   `-- main.ml
|-- dune-project
`-- lib
    |-- dune
    `-- math.ml

Here’s bin/dune:

(executable
 (name main)
 (libraries lib))

And lib/dune

(library
 (name lib))

From the dune documentation on library dependencies it seems like dune should automatically find libraries defined within the current scope / workspace. What am I doing wrong?

I ended up getting this to work after investigating a bit further, although I’m still not exactly sure why it works now.

I modified main.ml and added open Lib, after which dune built everything without complaint. I thought perhaps dune throws an error to prevent unused dependencies from being included, but I’ve also listed base as a dependency in the dune file, without opening it in main.ml, and dune is fine with that.

If anyone knows why I’m observing this behavior, I’d be curious to know.

Do you have a git repo where we could test that without having to reproduce it ourselves? You should not be required to use open Lib, a practice that I find pretty bad for readability of code in any case.

Unfortunately when I tried this again today I couldn’t reproduce the error. However, as I wrote that last sentence I realized the difference between the code now and before is that previously I had a stray dune-project file in the bin directory.

If you clone the sample repository I created and copy the dune-project file into the bin directory you can reproduce the error. To your point about not needing to open Lib - you’re right. I tested things out today and it works properly without opening the library.

Thanks!

Yes, so dune-project acts as “anchor” to determine where the project root is, and if it finds it in a folder from where there is no subfolder with a lib library this is exactly what happens.

I am glad we could figure out the reason for this weird behaviour you were seeing!

Yes, and dune doesn’t tell you what it does.
@evanm: you may reproduce something like your problem if your try to build one of the example that are still within dune source 2.5.1, say https://github.com/ocaml/dune/tree/2.5.1/example/sample-projects/hello_world .

Doing dune build does nothing (with no message, which is puzzling and disappointing).
Doing dune build --root . (dot for current directory) from within the hello_world directory works (you have to dig in _build/default because nothing is visible).
You should also think about doing dune runtest --root . and dune clean --root . to make something happen.

What happens if you try --root . your files tree?
Also, try --verbose to gain visibility and let us know.