Search code examples
clojuredeps-edn

How to compile all source and tests before running -M or -X from deps.edn?


My project has some code that requires AOT compilation.

Below is a shortened version of my deps.edn. The only way I have found to compile some code before each test run is to add a -e (compile,'my.project.namespace) pair to the :main-opts. This requires knowledge of the exact namespaces that require compilation, which changes periodically during development. Even simple experimentation in a unit test may require a temporary change to the deps.edn which is very annoying.

{:paths   ["src" "classes"]
 :deps    {}
 :aliases {:test           {:extra-paths ["test"]}
           :test/cognitect {:extra-deps {com.cognitect/test-runner {:git/url "..." :sha "..."}}
                            :main-opts  ["-e" "(compile,'my.project.namespace1)"
                                         "-e" "(compile,'my.project.namespace2)"
                                         "-e" "(compile,'my.project.namespace3)"
                                         "-m" "cognitect.test-runner"]
                            :exec-fn    cognitect.test-runner.api/test}}

This would be invoked with clj -M:test:test/cognitect.

Is there an easy way to compile the entire project (including tests) when -M (or -X) is invoked with a certain alias?

I know that -e can handle any Clojure expression so one option would be to write an entire mini program that will enumerate all the namespaces and call compile on them. I would be okay with this as long as the code is simple. Is this a good idea?

In general, how would one setup a deps.edn to AOT compile the entire project?


Solution

  • Take a look at tools.build: https://clojure.org/guides/tools_build

    This is the core team's answer to this problem.