Using the Visitors ppx with Dune


#1

Hi,

I’m trying to use the visitors ppx by @fpottier along with dune. It doesn’t seem to be working – although AFAICT it is a ppx_deriving plugin, and ppx_deriving alone seems to be working with dune.

Here’s what I tried, on a minimal example:

test.ml

type t = A | B [@@deriving visitors { variety = "iter" }]
let _ = object inherit [_] iter end

jbuild

(library
 ((name test)
  (public_name test)
  (libraries (visitors))
  (ppx_runtime_libraries (visitors.runtime))
  (preprocess (pps (visitors.ppx)))))

Error:

File "test.ml", line 2, characters 8-16:
Error: Unbound class iter

I also tried the “classical ppx” workaround using ppxfind:

jbuild

(library
 ((name test)
  (public_name test)
  (libraries (visitors))
  (ppx_runtime_libraries (visitors.runtime))
  (preprocess (action (run ppxfind -legacy visitors.ppx --as-pp ${<})))))

Error:

File "_none_", line 1:
Error: broken invariant in parsetree: Tuples must have at least 2 components.

Any idea on what’s happening and what should be fixed?


#2

Update: I also noticed that in the first case, if I do (preprocess (pps (ppx_deriving.show visitors.ppx))) instead of (preprocess (pps (visitors.ppx))) in the jbuilder file (the ml file stays the same and does not use ppx_deriving.show), I get instead the error:

File "test.ml", line 1, characters 27-35:
Error: Cannot locate deriver visitors

#3

I finally found a fix. The -linkall option had to be passed during the build of the ppx, in the visitors build process.

Interestingly, the way I found this is by porting the build of visitors to dune, and then comparing the differences between the current META file and the dune-generated one; as well as comparing the respective build logs…


#4

FWIW you may find a jbuild file, in our Electrod project, which uses Visitors.

However, if you were able to devise a Dune-based build for Visitors, which also makes Visitors compatible with the pps section of Dune, I suggest you submit a PR to @fpottier .


#5

I submitted a PR that fixes the issue, and a release is on the way. With François we will try to switch visitors’ build system to dune in the future, but right now, the new release seems to fix issues when using visitors with dune.

It should now be enough to simply add (preprocess (pps (visitors.ppx))) to enable visitors.