# Help debugging a ppx extension?

**URL:** <https://discuss.ocaml.org/t/help-debugging-a-ppx-extension/1167>\
**Category:** Learning\
**Created:** [November 22, 2017, 3:39am UTC](https://discuss.ocaml.org/t/help-debugging-a-ppx-extension/1167 "2017-11-22T03:39:03Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [November 22, 2017, 3:39am UTC](https://discuss.ocaml.org/t/help-debugging-a-ppx-extension/1167/1 "2017-11-22T03:39:03Z")

</div>

I’ve been converting sedlex to use `Uchar.t` instead of `int` (partially as an exercise as I’m not very experienced with OCaml, partially because it seems worthwhile.) I’m pretty sure I messed up and forgot to do the conversion somewhere because running the tests I get this error:

```
$ make test
ocamlfind ocamlc -package gen -ppx "../src/syntax/ppx_sedlex --as-ppx" -I ../src/lib -o tokenizer -linkpkg sedlexing.cma tokenizer.ml
File "_none_", line 1:
Error: This expression has type Uchar.t option
   but an expression was expected of type int
make: *** [tokenizer] Error 2

```

Unfortunately, this error message is a bit opaque, because rather than telling me where in my actual code the problem is, it is telling me something went wrong in an unknown place in my generated code.

I’m pretty sure I could find this immediately if I could see the specific code that was generated by the ppx\_sedlex rewriter, but I’m not really aware of how to do that.

Can anyone tell me a reasonable way to produce the rewritten file, so I can run it through the compiler and get a line number corresponding to the piece of code with the problem and I can get a hint about what I messed up?

Alternatively, is there a better way to debug a ppx rewriter?

Also, I should note that this points out an unfortunate problem: if there’s a bug in ppx preprocessed code, the error messages you get are less than ideal. I’m not sure what to do about that, but it _is_ a problem…

---

<div class="post-metadata">

**Author:** ![xclerc](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/xclerc/32/504_2.png) [@xclerc](https://discuss.ocaml.org/u/xclerc)\
**Post date:** [November 22, 2017, 9:19am UTC](https://discuss.ocaml.org/t/help-debugging-a-ppx-extension/1167/2 "2017-11-22T09:19:58Z")

</div>

I usually pass `-dsource` to the compiler so that it will output the source to stderr.  
Then I redirect this output to a temporary file, edit it to remove the last lines (that  
are the error found by the compiler - as you reported it), and finally compile this  
temporary file.

You then get the location of the error in the generated code (that has been output  
to the temporary file) instead of the phantom location.
