2

With leiningen, it is possible to reconfigure global vars within a profile [1]. This was convenient to enable options like *warn-on-reflection* for a whole module only during special stages like tests. Looking for the same with Clojure CLI, I could not find an explicit way to do so. Looking at various commands like compiling clojure code that requires the following clojure -M -e "(compile 'some.namespace)", it is possible that I have to resort to a similar trick.

For the record, various sources like [2] suggest to put it in each file, but it is quite cumbersome.

[1] https://github.com/technomancy/leiningen/blob/6c8cdeebb07972f2a24b95bfc64b339c9895d3bc/sample.project.clj#L285-L290
[2] https://github.com/clj-easy/graal-docs#reflection

Kineolyan
  • 723
  • 8
  • 24

1 Answers1

1

After find later this other SO [1], it seems the way to do it is as follow:

; deps.edn
{:aliases {:my-profile {:main-opts ["-e" "(set! *warn-on-reflection* true)"]}}}

It is correctly received by other -e command as seen in this test:

clojure -Mmy-profile -e '(println "warn-on-reflection =" *warn-on-reflection*)'
; => warn-on-reflection = true

One drawback reported in the comments is that only one :main-opts option from different aliases can be applied - last one wins.
There is one hope for the future: one question on ask.clojure.org [2] asking for a JVM option.

I hope this will help others that struggled like me to make the conversion.

[1] How to set default value for *print-length* in deps.edn
[2] https://ask.clojure.org/index.php/3787/theres-enable-warn-reflection-from-command-running-clojure

Kineolyan
  • 723
  • 8
  • 24
  • 3
    It's worth noting that :main-opts do not combine/merge when you have them in multiple aliases so you can't combine your :my-profile alias on the command-line with something that, for example, tries to run code via -m the.main.namespace (such as a test runner!) -- last one wins in the behavior for :main-opts. In addition, if you want to start a REPL, you would need clj -M:my-profile -r (you need -r to tell clojure.main to start a REPL after running the -e options). – Sean Corfield Oct 31 '21 at 22:05
  • Thanks @SeanCorfield for the additional information. If so, do you have any suggestion on how to merge the two list of values? This could be the topic of a new question but if I can update the self-answer in the best way, we all win. I noted the `-r` for the REPL, which I new about. I initially wanted something I could launch at build time, but this is more java thinking than clojure, where it is all centered around the REPL. Equiped with all that knowledge, I will adapt my process. – Kineolyan Nov 02 '21 at 13:18
  • "how to merge the two lists" -- no, `:main-opts` handling is built into the CLI and `tools.deps` that way (last one wins). Since I don't type into the REPL -- I always evaluate code from an editor into my running REPL -- I just have `(set! *warn-on-reflection* true)` in every source file so I don't have to worry about this. It would be nice if this was something you could set via a JVM option because those do combine. – Sean Corfield Nov 03 '21 at 17:49
  • I was just reminded of this: https://ask.clojure.org/index.php/3787/theres-enable-warn-reflection-from-command-running-clojure -- so there's a ticket in place to add a JVM option for global warn-on-reflection. – Sean Corfield Nov 03 '21 at 18:43