How to "run" a leiningen project in vscode/calva?

I have Calva set up and connecting to a REPL within a Leiningen project. I can evaluate individual forms within my core.clj file by pressing Alt+Enter. Is there some shortcut that actually “runs” the program the same way lein run does it?

So far the only way I have found is to add (-main) to core.clj and then pressing Ctrl+Alt+C Enter. Am I missing something here?

When doing REPL driven development, invoking -main or whatever your entry points into your program are is the way to go.

If you want to run it the same way lein run does just type lein run at the shell, but you should know how you plan to distribute your application and have users run it, and that will almost never be through having them use lein run. It be better for you to test using the same way of launching your app as you plan to have your users use, and for all other use case, just call -main in the REPL.

1 Like

You can use a “rich comment” at the end of a clj file:

(comment

  (-main)

)

The reason for putting such code in a comment is that Clojure executes (evaluates) all the forms in the file whenever it loads the file as a namespace. This may happen on occasions when you do not intend to run the program: e.g., generating API docs, running tests, or AOT compiling to put the class files in a jar.

2 Likes

This helps! I have been trying out Clojure to work out last year’s Advent of Code and this is perfect for that. Thanks!

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.