ocamlocaml-dune

Include mly files nested in subdirectories when compiling with dune


Say I have the following directory:

.
├── dune-project
├── myproj.opam
└── src
    ├── dune
    ├── module1.ml
    ├── parser1
    │   └── file1.ml
    └── parser2
        └── file2.ml

Since I want everything to be in the same library, I have the following:

src/dune:

(include_subdirs  unqualified)

(library
 (name myproj)
 (modules module1 file1 file2)
  )

And it compiles perfectly well.

Now, the thing is, my file1 and file2 are actually mly files so I have this:

.
├── dune-project
├── myproj.opam
└── src
    ├── dune
    ├── module1.ml
    ├── parser1
    │   └── parser1.mly
    └── parser2
        └── parser2.mly

If I try to compile (by changing file(1|2) by parser(1|2) in the dune file), I have the following error:

❯ dune build
File "src/dune", line 5, characters 26-33:
5 |  (modules module1 parser1 parser2 )
                              ^^^^^^^
Error: Module Parser2 doesn't exist.

I edit my dune file:

(include_subdirs  unqualified)

(menhir
 (modules parser1 parser2)
)

(library
 (name myproj)
 (modules module1 parser1 parser2 )
  )

Two errors now:

❯ dune build
File "src/dune", line 3, characters 0-36:
3 | (menhir
4 |  (modules parser1 parser2)
5 | )
Error: No rule found for src/parser1.mly
File "src/dune", line 3, characters 0-36:
3 | (menhir
4 |  (modules parser1 parser2)
5 | )
Error: No rule found for src/parser2.mly

Ok, I then add two dune files in parser(1|2) directories:

(menhir
 (modules parser(1|2))
)

[EDIT] Looks like I missed something but this was actually working pretty well

And I now have the same error as before:

❯ dune build
File "src/dune", line 5, characters 26-33:
5 |  (modules module1 parser1 parser2 )
                              ^^^^^^^
Error: Module Parser2 doesn't exist.

Am I missing something to please dune? (I have the same problem if I have .mll files)


Solution

  • The menhir stanza doesn't work with subdirs. It is a bug that is not yet fixed1. There are a couple of workarounds, like adding the dune file to the folder where you have the parser (didn't check it myself). But I would suggest splitting the parsers into separate libraries or moving them to the src folder. The former is preferred, especially since then you won't need to write the modules stanza.


    1)As of January 2022