macroexpand-1 doesn't work on clojurescript
jiacai2050 opened this issue ยท 9 comments
Steps to reproduce the problem
(defproject om-tutorial "0.1.0-SNAPSHOT"
:description "My first Om program!"
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/clojurescript "1.8.51"]
[org.omcljs/om "1.0.0-beta1"]
[com.cemerick/piggieback "0.2.1"]
[figwheel-sidecar "0.5.14"]]
:repl-options {:nrepl-middleware [cemerick.piggieback/wrap-cljs-repl]}
:clean-targets ^{:protect false} [:target-path "out" "resources/public/js"]
:plugins [[lein-figwheel "0.5.14"]
[lein-cljsbuild "1.1.7"]]
:cljsbuild {:builds [{:id "dev"
:figwheel true
:source-paths ["src"]
:compiler {:main om-tutorial.core
:asset-path "js"
:output-to "resources/public/js/main.js"
:output-dir "resources/public/js"
:verbose true}}] })
With project.clj
above, I was able to launch a figwheel repl.
In core.cljs
, I have an om component defined like below
(defui HelloWorld
Object
(render [this]
(dom/div nil (get (om/props this) :title))))
When I expand this macro using C-c RET
, it didn't expand at all, in *cider-macroexpansion*
buffer I got the same thing back, however, if I expand this manually, macroexpand did work.
(macroexpand-1 '(defui HelloWorld
Object
(render [this]
(dom/div nil (get (om/props this) :title)))))
;; this works as expected.
Is there anything I was missing?
Thanks in advance.
Environment & Version information
CIDER version information
;; CIDER 0.15.1 (London)
;; Clojure 1.8.0, java version "1.8.0_144"
Lein/Boot version
Leiningen 2.7.1 on Java 1.8.0_144 Java HotSpot(TM) 64-Bit Server VM
Emacs version
GNU Emacs 25.3.1 (x86_64-apple-darwin16.7.0) of 2017-09-12
Operating system
macOS Sierra 10.12.6
See what are the messages exchanged between CIDER and nREPL as described here http://cider.readthedocs.io/en/latest/troubleshooting/
I see we've got an cljs implementation for the macroexpansion, so in theory everything should be working fine.
Through experimentation I've confirmed that the problem goes all the way down to (cljs.analyzer/macroexpand-1 (cljs/grab-cljs-env msg) (reader/read-string code))
not properly macroexpanding the form.
I suspect this has to do with cljs.analyzer/macroexpand-1
behaving differently in a CLJ vs. CLJS context. See Slack discussion.
For posterity (older Slack messages eventually become unavailable):
Evaluating
cljs.core/macroexpand-1
for a :clj: / :cljs: project in a :clj: buffer differs from the result in a :cljs: buffer.E.g. given a file:
(ns om-tutorial.core (:require [om.dom :as dom] [om.next :as om :refer-macros [defui]])) (defui HelloWorld Object (render [this] (dom/div nil (get (om/props this) :title))))Callling
cljs.core/macroexpand-1
'(defui ...)
in a :clj: buffer does nothing, while calling it from a :cljs: buffer gives:(do (do (clojure.core/defn HelloWorld [] (cljs.core/this-as this__41180__auto__ (.apply js/React.Component this__41180__auto__ (cljs.core/js-arguments)) ...as expected.
Why is this?
Well, passing (cljs/grab-cljs-env msg)
to cljs.analyzer/macroexpand-1
is definitely not the right thing to do. The env
needed by cljs.analyzer/macroexpand-1
is not the compiler env.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!
any progress?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contribution and understanding!
Any chance this might get renewed attention?
I just recently started work in a large ClojureScript codebase that makes extensive use of macros. It's fun times โ I've been away from Clojure/Script for some years, and it's great to be back.
Today I tried to use the cider-macroexpand-
facilities and found they basically don't work โ either the forms don't get expanded (just echoed back) or I get very strange results (what got expanded wasn't a match with the form at point and the results are bizarre).
CIDER is awesome ๐ , and still useful for ClojureScript even if macro expansion isn't working as hoped, but it would be a big deal for me and probably for many other developers if it did work. I would be glad to help fix it, but after digging around at the problem for several hours today, I wasn't able to figure out what's going wrong.
As of the start of today, I had completely forgotten that back in 2013, when I was neck deep in Clojure/Script, I was struggling with macro expansion and I wrote some helpers that mostly worked:
https://gist.github.com/michaelsbradleyjr/7509505
While doing some googl'ing re: clojurescript
and macroexpand-all
I found my own gist! I was puzzled for a moment and then remembered all about it. ๐คฆ ๐
I tried adapting that code to present-day ClojureScript. Long-short is that with a macroexpand-all
based on my original code, I'm able in a CIDER REPL to expand forms that involve basic things like (cond ...)
but if my macroexpand-all
encounters a macro defined in one of my own .cljc
files then it doesn't work. I tried for hours today to get it to work, but either there's an aspect to the solvable problem that I don't understand or it's simply not solvable for various reasons.
related code:
Someone else also encounter this issue:
@michaelsbradleyjr i just someone who has built a clever workaround for the whole "macroexpend in macros in cljs" problem flowstorm instrument