Running dune through Emacs minor mode - can't find opam libraries

Hi there.

I’m pretty new to OCaml (on macOS). Currently getting into the ecosystem with development workflow.
Got a project setup properly now using dune.

Now since I’m a TDD’ler I’d like to run a Ounit2 test module right from Emacs instead of going to the terminal and type ‘dune test’.
(Is there a different way of doing this that I’m not aware of?)

So I thought I’ll simply create an Emacs minor mode that can do this for me with a key stroke.
The problem is that I can easily call ‘dune test’ via (call-process ...) in Emacs but the process output says:

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

Even though the ‘ounit2’ library is installed.

I’ve extended the exec-path in Emacs via setq exec-path and the env PATH via setenv in Emacs to include the path “~/.opam/default/bin”.

Now the question is why does ‘dune’ running through the Emacs shell process not see the installed libraries?
Any ideas?

Cheers,
Manfred

You may try to run opam exec dune test instead of dune test to get a properly initialized opam environment.

That works.
Many thanks.