4

How can I derive to_string methods for data types using ppx with jbuilder?

For instance, I'm trying to use @@deriving show to derive show_* methods for a data type. I have a simple main.ml file that looks like this:

open Core

type foo = Bar | Baz
  [@@deriving show]

let () = printf "%s\n" (show_foo Bar)

I have a jbuild file in the same directory that looks like this:

(jbuild_version 1)

(executables
 ((names (main))
  (libraries (core ppx_deriving))
  (preprocess (pps (ppx_deriving.show)))))

(install
 ((section bin)
  (files ((main.exe as my_foo)))))

When I run jbuilder build, I get the following error:

File "main.ml", line 6, characters 24-32:
Error: Unbound value show_foo

It seems like jbuilder doesn't run the ppx_deriving.show preprocessor, and doesn't generate the show_foo function.

Is my use of @@derive show correct? Do I need to add something to my jbuild file to get it to work correctly? Does ppx_deriving have a problem working with jbuilder? Should I be using a different ppx library? Should I be using a different build system? How do most OCamlers deal with these kind of build system problems?


edit: I am using OCaml version 4.04.2, jbuilder version 1.0+beta11, and ppx_deriving version 4.1.

glennsl
  • 28,186
  • 12
  • 57
  • 75
illabout
  • 3,517
  • 1
  • 18
  • 39
  • When posting questions about `jbuilder`, it is necessary to include a `*.opam` file? Does `jbuilder` look at this file to figure out how to build the executable? – illabout Aug 28 '17 at 08:34
  • 1
    I just tried updating `jbuilder` and `ppx_deriving` and now this appears to work. My `jbuilder` version is [1.0+beta12](https://github.com/janestreet/jbuilder/blob/1c85e646bc732189739e31e697b5e60bbd2221ba/CHANGES.md#10beta12-18082017) and `ppx_deriving` is version [4.2](https://github.com/ocaml-ppx/ppx_deriving/blob/7969277d1ec02517f441930e449d115d64a2a778/CHANGELOG.md#42). – illabout Aug 28 '17 at 10:28
  • Using the two above package versions, this code also works with OCaml version `4.05.0`. – illabout Aug 28 '17 at 10:45

1 Answers1

3

As you found in the comments, this requires particular handling in ppx_deriving which was only merged in version 4.2.

The underlying reason is that jbuilder uses ppx_driver to apply ppx rewriters and not the -ppx flag.

Étienne Millon
  • 3,018
  • 11
  • 27