# Run (preprocess ..) conditionally in dune

**URL:** <https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948>\
**Category:** Ecosystem\
**Tags:** dune\
**Created:** [April 15, 2023, 3:40am UTC](https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948 "2023-04-15T03:40:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris-armstrong](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chris-armstrong/32/6267_2.png) [@chris-armstrong](https://discuss.ocaml.org/u/chris-armstrong)\
**Post date:** [April 15, 2023, 3:40am UTC](https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948/1 "2023-04-15T03:40:05Z")

</div>

I’m building a library that contains inline tests with ppx\_expect, and I’d like to be able to run ppx\_expect only when `dune runtest` is called.

The reason is that I’ve defined `ppx-expect` as a `with-test` dependency in my opam file, and I don’t want users to have to install ppx\_expect (and all its dependencies) just to use the library. It’s defined in my opam file like:

```auto
depends: [
  "dune" {>= "3.4"}
  "yojson"
  "containers"
  "fmt"
  "ppx_deriving"
  "ppx_expect" {with-test}

```

I’m including ppx\_expect it in my dune file, and I wonder if its possible to use a conditional so that ppx\_expect isn’t needed outside of the `dune runtest` alias.

I’ve tried the following, but dune gives a `Error: unknown constructor if` error:

```auto
(library (name my_library)
  (libraries fmt)
  (inline_tests)
  (preprocess (if (= %{inline_tests} enabled) (pps ppx_expect)))
)

```

---

<div class="post-metadata">

**Author:** ![shonfeder](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/shonfeder/32/424_2.png) [@shonfeder](https://discuss.ocaml.org/u/shonfeder)\
**Post date:** [April 15, 2023, 11:43am UTC](https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948/2 "2023-04-15T11:43:09Z")

</div>

There’s hopefully a more direct way to do what you want. But when I’ve been in this situation in the past I only add `ppx_expect` as a library dependency to my tests, and not to my main project. I’m still able to test everything I need, and I don’t pollute the main project deps. The cost is that the expect tests don’t live right alongside the implementation, but I’ve not found that to be a problem.

---

<div class="post-metadata">

**Author:** ![hhugo](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/hhugo/32/1143_2.png) [@hhugo](https://discuss.ocaml.org/u/hhugo)\
**Post date:** [April 15, 2023, 3:00pm UTC](https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948/3 "2023-04-15T15:00:22Z")

</div>

Ppx\_expext is required event if you don’t plan to run the test. You need a ppx to transform the extension point `let%expect` otherwise the compiler with fail on them.

---

<div class="post-metadata">

**Author:** ![chris-armstrong](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/chris-armstrong/32/6267_2.png) [@chris-armstrong](https://discuss.ocaml.org/u/chris-armstrong)\
**Post date:** [April 16, 2023, 12:09am UTC](https://discuss.ocaml.org/t/run-preprocess-conditionally-in-dune/11948/4 "2023-04-16T00:09:52Z")

</div>

Thanks - I’ll avoid the recommendations to put it into with-test and just include it as a standard dependency
