Generating files from ppx with dune

I’m attempting to develop a ppx that consumes code in an extension point (i.e. [%txt "foobar"]) and puts a result in a file somewhere.

I’ve written the ppx part which seems to work fine, but I’m trying to figure out if there’s a way to write a dune rule to depend on the generated file from the ppx. So far I’ve tried writing something like

(rule
  (deps my_file.pp.ml)
  (targets my_file.txt)
  (action (run true)))

among other deps (such as my_file.exe, my_file.ml) but dune seems to always say

Error: Rule failed to generate the following targets:
  src/my_file.txt

I’m wondering if this is something that I’m messing up (i.e. putting my_file.txt in the wrong place), or if this isn’t generally supported with dune.

Any help is greatly appreciated!

2 Likes

I think that a solid approach would be running PPX rewriter again in the (run ...) stanza. The rewriter should be compiled to executable beforehand.

I considered this but it seems like it has a few distinct drawbacks:

  • Running the ppx twice is obviously inefficient, especially for one like this which is doing a fair amount of IO. This is compounded by the fact that dune can combine multiple ppxs into one AST traversal
  • When I specify a ppx in the preprocessing field, it’s automatically run over all modules within the specified stanza. If I write a rule (unless I’m mistaken) I’d have to enumerate all modules in my library/executable for which I want the ppx to be run over

If that’s what I end up having to do I can accept that but I think there should be some other method for specifying this action.

1 Like

The solution might be adding src/my_file.txt into preprocessor_deps in your library/executable

Have you tried something like this?