Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

macroexpand-1 doesn't work on clojurescript #2099

Open
jiacai2050 opened this issue Oct 18, 2017 · 9 comments
Open

macroexpand-1 doesn't work on clojurescript #2099

jiacai2050 opened this issue Oct 18, 2017 · 9 comments
Labels

Comments

@jiacai2050
Copy link
Contributor

jiacai2050 commented Oct 18, 2017

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

@bbatsov
Copy link
Member

bbatsov commented Dec 31, 2017

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.

@xiongtx
Copy link
Member

xiongtx commented Feb 6, 2018

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?

@xiongtx
Copy link
Member

xiongtx commented Feb 6, 2018

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.

@stale
Copy link

stale bot commented May 8, 2019

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!

@stale stale bot added the stale label May 8, 2019
@jiacai2050
Copy link
Contributor Author

any progress?

@stale stale bot removed the stale label May 11, 2019
@stale
Copy link

stale bot commented Aug 9, 2019

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!

@stale stale bot added the stale label Aug 9, 2019
@bbatsov bbatsov added the high priority Tickets of particular importance label Aug 9, 2019
@stale stale bot removed the stale label Aug 9, 2019
@michaelsbradleyjr
Copy link

michaelsbradleyjr commented Apr 17, 2020

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.

@jiacai2050
Copy link
Contributor Author

jiacai2050 commented Jul 21, 2020

@Cyrik
Copy link

Cyrik commented Nov 5, 2021

@michaelsbradleyjr i just someone who has built a clever workaround for the whole "macroexpend in macros in cljs" problem flowstorm instrument

@vemv vemv pinned this issue Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants