cider icon indicating copy to clipboard operation
cider copied to clipboard

cider-jack-in-cljs failing to add deps from .dir-locals.el file as i expected.

Open drewverlee opened this issue 3 years ago • 8 comments

Expected behavior

Given this .dir-locals file

 (clojurescript-mode
  .
  ((cider-clojure-cli-aliases  . "-A:fig")
   (cider-default-cljs-repl . figwheel-main))
  )


And this deps file

{
....
 :aliases {
           :fig       {:extra-deps {com.bhauman/figwheel-main {:mvn/version "0.2.13"}}}
           ...
          }}}

In spacemacs if i use the command cider-jack-in-cljs i expected to see figwheel-main injected into the deps. Like so

[nREPL] Starting server via /usr/local/bin/clojure -Sdeps '{:deps {com.bhauman/figwheel-main {:mvn/version "..."}



nrepl/nrepl {:mvn/version "0.9.0"} cider/piggieback {:mvn/version "0.5.2"} refactor-nrepl/refactor-nrepl {:mvn/version "3.1.0"} cider/cider-nrepl {:mvn/version "0.27.4"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}}}' -M:cider/nrepl

Actual behavior

Instead the dep isn't there...

[nREPL] Starting server via /usr/local/bin/clojure -Sdeps '

nrepl/nrepl {:mvn/version "0.9.0"} cider/piggieback {:mvn/version "0.5.2"} refactor-nrepl/refactor-nrepl {:mvn/version "3.1.0"} cider/cider-nrepl {:mvn/version "0.27.4"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware,cider.piggieback/wrap-cljs-repl]"]}}}' -M:cider/nrepl

Steps to reproduce the problem

Steps in the description.

Environment & Version information

CIDER version information

cider 1.2.0 (Nice)

Emacs version

➜ .emacs.d git:(develop) ✗ emacs --version GNU Emacs 27.2

Operating system

Ubuntu

drewverlee avatar Jan 01 '22 19:01 drewverlee

Did you run jack-in from a ClojureScript file? I think it's generally better to use clojure-mode (or nil) in .dir-locals.el, so it'd be applied in more cases (e.g. if you try to jack-in while you're in deps.edn).

bbatsov avatar Jan 01 '22 20:01 bbatsov

Did you run jack-in from a ClojureScript file?

i ran cider-jack-in-cljs from a .cljs file.

I think it's generally better to use clojure-mode (or nil) in .dir-locals.el, so it'd be applied in more cases (e.g. if you try to jack-in while you're in deps.edn).

my full .dir-locals file is

(

 (clojure-mode .
               ((cider-preferred-build-tool . clojure-cli)
                (cider-clojure-cli-aliases . "-A:fig:dev:docs")))


 (clojurescript-mode
  .
  ((cider-clojure-cli-aliases  . "-A:fig")
   (cider-default-cljs-repl . figwheel-main))
  )

 )

So i seem to be using clojure for something else.

Here is some more information from some light debugging. given (message "---> %s" cider-clojure-cli-aliases) in the context below. i would expect to see----> '-A:fig' but instead i get -----> nil

  "Create Clojure tools.deps jack-in dependencies.
Does so by concatenating DEPENDENCIES and GLOBAL-OPTIONS into a suitable
`clojure` invocation.  The main is placed in an inline alias :cider/nrepl
so that if your aliases contain any mains, the cider/nrepl one will be the
one used."
  (message "---> %s" cider-clojure-cli-aliases) 
  (let* ((deps-string (string-join
                       (seq-map (lambda (dep)
                                  (format "%s {:mvn/version \"%s\"}" (car dep) (cadr dep)))
                                ;; NOTE: injecting Lein plugins for deps.edn projects
                                ;; seems a bit dubious, worth revisiting at some point.
                                (append dependencies cider-jack-in-lein-plugins))
                       " "))
         (middleware (mapconcat
                      (apply-partially #'format "%s")
                      (cider-jack-in-normalized-nrepl-middlewares)
                      ","))
         (main-opts (format "\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[%s]\"" middleware)))
    (format "%s-Sdeps '{:deps {%s} :aliases {:cider/nrepl {:main-opts [%s]}}}' -M%s:cider/nrepl"
            (if global-options (format "%s " global-options) "")
            deps-string
            main-opts
            (if cider-clojure-cli-aliases
                ;; replace -A or -M in the jack-in-aliases to be concatenated
                ;; with cider/nrepl to ensure cider/nrepl comes last
                (format ":%s" (replace-regexp-in-string "^-\\(A\\\|M\\):" "" cider-clojure-cli-aliases))
              ""))))

drewverlee avatar Jan 01 '22 20:01 drewverlee

I'll have to test this myself at some point, but as .dir-locals.el are a built-in Emacs feature and we haven't changed anything in the jack-in code recently I can't imagine how this would suddenly stop working. On the other hand @Bost recently mentioned something similar here https://github.com/clojure-emacs/cider/issues/3121#issuecomment-1003049386

bbatsov avatar Jan 02 '22 07:01 bbatsov

//cc @dpsutton

bbatsov avatar Jan 02 '22 07:01 bbatsov

Initial thoughts: just check the value of those variables are in the clojurescript file. C-h v [ret] cider-clojure-cli-aliases.

Easy diagnosing steps:

  • comment out the clojure-mode stuff
  • (hack-local-variables) to refresh the dir local file
  • C-h v [ret] cider-clojure-cli-aliases to see what value is in effect here

Play around with this. Is the presence of both messing it up? If you revert this back to a trivial example with only one mode specified (or even nil as the mode) are those values being seen in the cljs buffer? Does it matter if you put the clojure-mode version first?

I'm checking on an older version of CIDER from about two months ago:

Each here running (insert cider-clojure-cli-aliases) so it just puts the value in that buffer image

I updated to the most recent commit. When I start up from the clojure buffer it seems to see the correct variables: /usr/local/bin/clojure -Sdeps '{...}' -M:fig:dev:docs:cider/nrepl. Doing a cider-jack-in from the cljs buffer (note not jack-in-cljs) is /usr/local/bin/clojure -Sdeps '{...}' -M:fig:cider/nrepl. So it seems to pick up the right stuff for me. I quickly did a cljs jack in and see the message /usr/local/bin/clojure -Sdeps '{...}' -M:fig:cider/nrepl (before it errors with missing fighweel config files) so it all seems to work on my side.

As far as I can tell CIDER is behaving correctly. I have seen in slack that you have had some issues with some ancillary CIDER packages (helm-cider) and I'm wondering if you are having issues with CIDER adjacent things rather than CIDER itself. I don't know what packages you are using and how they interact. Check what the values are with C-h v and then jack-in and see if the value that was present with C-h v is no longer in effect. I cannot reproduce this though.

dpsutton avatar Jan 02 '22 18:01 dpsutton

Btw, I've fixed the problem with helm-cider and I've released a new version (0.5). Sorry about the breakage there!

bbatsov avatar Jan 03 '22 06:01 bbatsov

I can confirm that aliases are loading for clojure-mode in the latest CIDER snapshot (I havent experience any issues in earlier versions either)

@drewverlee A working example can be found at: https://github.com/practicalli/practicalli.github.io/

This project just uses clojure-mode and works fine in a Clojure or ClojureScript file (and deps.edn) (Edit: I also tried with clojurescript-mode in the .dir-locals.el and the aliases were added correctly)

There is the limitation of .dir-locals.el not loading unless forced to do so. So if the .dir-locals.el file is created or updated after the buffer used to launch the REPL was opened, it will not pick up the file or changes.

SPC b R in Spacemacs or M-x revert-buffer on the current buffer should ensure the .dir-locals.el file is loaded.

Note: changing to another buffer that was opened before .dir-locals.el was created / updated will also neglect to pick up the change in the .dir-locals.el file unless that buffer is also reverted (or closed buffer closed and file re-opend).

There article also has some relevant examples: https://practical.li/spacemacs/clojure-projects/project-configuration.html

practicalli-johnny avatar Jan 03 '22 17:01 practicalli-johnny

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[bot] avatar Apr 17 '22 00:04 stale[bot]

So for a long time, like years already, none of this stuff has worked for me. I'm trying to get a simple project with Shadow-CLJS to start straight into the correct build using the correct .dir-locals.el variables, but was having no luck. I seem to have a memory of it working sometimes, but recently not at all. I think it's related to CLJS, but it's a hunch only. I generally have done more Leiningen + Clojure projects, where I don't use it.

Easy diagnosing steps:

  • comment out the clojure-mode stuff
  • (hack-local-variables) to refresh the dir local file
  • C-h v [ret] cider-clojure-cli-aliases to see what value is in effect here

With these, and looking at the file, restarting Emacs a few times, etc. I think I finally figured out my problem. It seems I was missing a closing ) from the end and a syntax error. So if you too have problems getting this to work, check first that you have a valid expression in .dir-locals.el. Also, sometimes Emacs asks you if you are sure you want to execute some suspicious statements from the file.

Thanks and hope this helps someone else too.

Macroz avatar Dec 13 '22 09:12 Macroz

I'm going to close this issue because:

  • there isn't a minimal project I can simply clone and run
  • there's a wealth of debugging advice in this thread
  • CIDER has kept improving, both for tools.deps integration and cljs integration

And most of all:

i would expect to see----> '-A:fig' but instead i get -----> nil

...indicates that cider-clojure-cli-aliases simply is nil. That is an issue related to dir-local variables evaluation. CIDER cannot do anything about that.

A fresh issue will be gladly attended.

Cheers - V

vemv avatar Aug 22 '23 15:08 vemv