tools-deps

cjohansen 2024-07-05T06:32:05.577509Z

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?

Alex Miller (Clojure team) 2024-07-05T11:43:32.686149Z

Can you say more about what the :test alias is and the macro

cjohansen 2024-07-05T15:00:42.090519Z

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"}

cjohansen 2024-07-05T15:01:11.590829Z

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.

Alex Miller (Clojure team) 2024-07-05T15:05:23.900879Z

-X is going to do the equivalent of ((requiring-resolve f) arg-map), there is no change in ns

cjohansen 2024-07-05T15:20:09.074829Z

*ns* behaves differently depending on how I run this at least.

seancorfield 2024-07-05T16:44:06.527839Z

*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)

Alex Miller (Clojure team) 2024-07-05T17:04:18.580929Z

Yes, or in some cases nil

Alex Miller (Clojure team) 2024-07-05T17:05:08.381799Z

I think compiled class with a main may give you the latter

cjohansen 2024-07-05T17:06:43.661169Z

Interesting. Is there a reliable way to get the namespace of the caller in a macro?