shadow-cljs icon indicating copy to clipboard operation
shadow-cljs copied to clipboard

Requiring macro via `load-file` fails in the REPL if the macro is in top level namespace

Open PEZ opened this issue 3 years ago • 1 comments

From this discussion in Slack.

Following this example, for how to work with macros in ClojureScript, fails in the REPL if I define the macro in a top-level namespace:

util.clj:

(ns util)

(defmacro foo [& body]
  (apply str body))

util.cljs:

(ns util
  (:require-macros [util]))

my/app.cljs:

(ns my.app
  (:require [util]))

(print (util/foo 1 2 3))

If I now load the my/app.cljs file, I now should be able to use the macro, and expect it to print 123 when it is loaded, but it fails on the first load, and starts to work if I load the file again:

% npx shadow-cljs node-repl
shadow-cljs - config: /Users/pez/Projects/sass2garden/shadow-cljs.edn
[2022-11-01 23:41:15.678 - WARNING] TCP Port 9630 in use.
[2022-11-01 23:41:15.681 - WARNING] TCP Port 9631 in use.
shadow-cljs - server version: 2.20.7 running at http://localhost:9632
shadow-cljs - nREPL server started on port 58439
(lcljs.user=> oshadow-cljs - #4 ready!
ad-file "src/main/my/app.cljs")
------ WARNING - :undeclared-var -----------------------------------------------
 Resource: <eval>:4:9
 Use of undeclared Var util/foo
--------------------------------------------------------------------------------

cljs.user=> (load-file "src/main/my/app.cljs")
123
[]
cljs.user=> 

However, if I place the macro in my.util, things work as expected.

Please note, @thheller, that (require 'my.app) works. My repro in that Slack conversation was crap. This seems to be with load-file.

PEZ avatar Nov 01 '22 22:11 PEZ

I can reproduce the problem when using load-file and your "flat" namespaces. I can also reproduce moving the namespaces to a proper structure (ie. no single-segment namespaces, util becomes my.util) working just fine. Not yet sure why though.

thheller avatar Nov 02 '22 07:11 thheller