Working on a local project with opam

Hello :slightly_smiling_face:

Disclaimer:
I am a total beginner with opam as I am normally using esy as my package manager. But I wanted to try out opam just as a learning experience.

I did setup a local development environment as follows:

  1. I downloaded and installed opam 2.1.2 on my mac
  2. I am installing my projects dependencies with the following makefile command:
    if ! [ -e _opam ]; then \
      opam switch create . --empty --yes &&
      opam install --yes ocaml.4.14.0 ;
    fi
    opam install ./*.opam --deps-only --locked --with-test --yes
    
  3. I want to build my project with the following command:
    opam exec --working-dir -- dune build --root . --profile=release @all
    

This works for all files, which are already checked in to the git repository.
If I however want to add new files and build the project locally, dune doesn’t seem to know of the new files:
(creating a new file test.ml)

34 |   open Test
            ^^^^
Error: Unbound module Test

I thought the --working-dir flag is used to tell opam to look into the source files for changes as opposed to looking into the git repository, but this doesn’t seem to be the case.

So my question(s) would be:
How do I test the changes I made to my library locally (before committing them into git)?
And more generally, why does opam even care about the current state of my git repository?

Thank you for your help!
- Torben

I’ve tried somewhat similar things in the past. It appears that opam copies your build-tree over into its build-tree storage, and builds there. And (at least from my experience) it isn’t accurate at knowing when the recopy. So if you turn on logging, you should be able to learn the build-directory, go over there, and see that your new files aren’t present.

I think it’s not a question of git, but rather that opam isn’t synchronizing the build-dir with your source-dir.

I do build/install using “opam install --working-dir .” when I want to install a local working version. But I never do that except when I’ve check-into git, and I don’t use that workflow for development – I just invoke make (in your case, dune) the usualy way.

After all, OPAM is a package-manager, not a build-system. Or at least, that’s my view.

This can be simplified into

dune build --profile=release

You need to let Dune know about the file you just added by modifying the relevant dune file. I don’t know what’s the precise structure of your project, but for instance if a library is declared in lib/dune as follows:

(library
  (name mylib)
  (modules a b c))

you can decide to add the Test module to that library like so:

(library
  (name mylib)
  (modules a b c test))

then all executables using mylib will know where to find Test. The Dune documentation is available, but in my opinion it can be a lot to take in at once.

1 Like