When running a function with clojure -A:dev -X:test I get nil for all of (:line (meta &form)) *file* and *ns* in a macro. These have values when evaluated from a REPL. Is this expected, or am I doing something wrong?
Can you say more about what the :test alias is and the macro
I was able to partially reproduce it like so:
;; deps.edn
{:paths ["src"]
:deps {org.clojure/clojure {:mvn/version "1.11.1"}}
:aliases
{:test
{:exec-fn macrox.core/run-test}}}
;; src/macrox/core.clj
(ns macrox.core)
(defmacro get-debug-data []
`(assoc ~(meta &form) :ns (str *ns*) :*file* *file*))
(defn ^:export run-test [& _]
(prn (get-debug-data)))
Evaluating (run-test) in a REPL gives me {:line 7, :column 8, :ns "macrox.core", :*file* ".../macrox/src/macrox/core.clj"} but running it with -X seems to execute the code from the user namespace:
clojure -X:test
{:line 7, :column 8, :ns "user", :*file* "NO_SOURCE_PATH"}
I say "partially" reproduce, because now I have line number and column. I didn't before, so maybe I've made a mistake in my original code.
-X is going to do the equivalent of ((requiring-resolve f) arg-map), there is no change in ns
*ns* behaves differently depending on how I run this at least.
*ns* is a really weird variable: it's pretty much never what people expect it to be. To capture the current ns, I typically do (def my-ns *ns*) since that captures it at load/compile time, otherwise it is bound to the initiating ns at runtime I think? (hence user)
Yes, or in some cases nil
I think compiled class with a main may give you the latter
Interesting. Is there a reliable way to get the namespace of the caller in a macro?