phaer
phaer

Reputation: 51

How to use jbuild and ppx_driver with ppx_deriving

I am trying to use jbuilder together with ppx_deriving (ppx_deriving_yojson specifically) but got stuck for well over an hour now. My current approach is a jbuild file, containing the following:

(jbuild_version 1)
(executables
((names (my-binary))
(libraries
 (ppx_deriving
  ppx_deriving_yojson
  cohttp
  yojson))
(preprocess (pps (ppx_deriving_yojson ppx_driver.runner)))))

But that results in

Command [5] exited with code 1:
$ (cd _build/default && ../.ppx/default/ppx_deriving_yojson+ppx_driver.runner/ppx.exe --dump-ast -o src/my_file.pp.ml --impl src/my_file.ml)
File "src/my_file.ml", line 16, characters 5-13:
Error: Attribute `deriving' was not used

Running the generated ppx_driver in _build/.ppx/default/ppx_deriving_yojson+ppx_driver.runner/ppx.exe manually with -print-transformations gives empty output, so I am obviously missing something.

The code builds fine with topkg by just including ppx_deriving and ppx_deriving_yojson as dependencies.

Upvotes: 4

Views: 447

Answers (1)

hcarty
hcarty

Reputation: 1671

As of more recent versions of ppx_deriving_yojson this should be possible.

Code:

type t = {x: int; y: int} [@@deriving to_yojson]

let () = print_endline (Yojson.Safe.to_string (to_yojson {x= 1; y= 2}))

And a sample jbuild file:

(jbuild_version 1)

(executables
 ((names (main))
  (preprocess (pps (ppx_deriving_yojson)))
  (libraries (ppx_deriving_yojson.runtime))))

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

Upvotes: 2

Related Questions