Not able to access library code in tests in a Dune project

Hi folks,

Hopefully this is the right place for this question. I have a Dune project:

$ tree
├── bin
│   ├── dune
│   └──
├── compiler.opam
├── dune-project
├── lib
│   ├── dune
│   └──
└── test
    └── dune

where test/ contains the following:

open OUnit2

let tests =  
  "foo" >:::
  ["returns the value passed" >:: 
   (fun _ -> assert_equal 2  (foo 2))]

 let _ = run_test_tt_main tests

The function foo is defined in lib/ as

let foo x = x

When I try to build with dune build, I get:

$ dune build
File "test/", line 6, characters 30-33:
6 |    (fun _ -> assert_equal 2  (foo 2))]
Error: Unbound value foo

How do I get the tests to see the code in my library? My lib/dune contains

 (name compiler))

and test/dune has

 (name compiler)
 (libraries ounit2 compiler))


foo (the function) is not in scope. You can refer to it as in test/ (Compiler because it’s the name of the library, Foo because of that defines it)


Thank you, that works!

I’m using VS Code and even after I’ve done a dune build, the editor still doesn’t find it (Unbound module Compiler). Do you happen to know how to solve that?

Try using a different name for your library and your test, for example test/


Sorted, thanks again! I’m new to OCaml so am getting used to how to do things.