cider

A channel dedicated to the Clojure Interactive Development Environment that Rocks (aka CIDER). :cider:
william 2021-03-21T15:39:25.021100Z

@dpsutton is there a way to inject the cider-nrepl middleware after doing M-x cider-connect if I want to connect to a specific repl?

dpsutton 2021-03-21T15:49:06.021300Z

don't believe so

dpsutton 2021-03-21T15:49:19.021600Z

that stuff needs to be registered at the beginning as far as i know

william 2021-03-21T15:51:56.023100Z

could you give us some ideas for the last thread currently in #lsp @dpsutton? My intent is having the server that's opened by clojure-lsp to include the cider-nrepl middleware

william 2021-03-21T15:52:32.024Z

honestly I don't even understand how we can use the same server for both lsp and nrepl, but let's maybe unify the discussion theer

dpsutton 2021-03-21T15:52:34.024200Z

do you understand how cider normally starts a project up?

dpsutton 2021-03-21T15:53:04.024900Z

nrepl is just a port listening for instructions and executing in your program. it can exist in any arbitrary project, which is how nrepl works in all clojure projects

dpsutton 2021-03-21T15:54:09.026500Z

it might be a bit difficult though. nrepl expects to be the "main". if lsp also expects to have its own main to do its own startup, it might be easier to just add a call to startup an nrepl server in lsp so there's no conflict on what the entry point is

william 2021-03-21T15:54:12.026700Z

here's my understanding: when I issue cider-jack-in, cider opens an nrepl server with all the middleware I'm interested in, and an nrepl client which I activate from the emacs side

dpsutton 2021-03-21T15:54:38.027200Z

yes. and if you do that and look at the top of the repl buffer you'll see the exact command that is run to accomplish this

william 2021-03-21T15:55:16.027900Z

right, and I can see that when I start a project that uses clojure-cli. It's something like:

[nREPL] Starting server via /run/current-system/sw/bin/clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.8.3"} cider/piggieback {:mvn/version "0.5.2"} refactor-nrepl {:mvn/version "2.5.1"} cider/cider-nrepl {:mvn/version "0.25.9"}} :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

william 2021-03-21T15:56:28.029200Z

now though, @ericdallo suggests some steps to hack on clojure-lsp that involve issuing cider-connect on the port in which clojure-lsp has exposed an nrepl server used for debugging

william 2021-03-21T15:57:23.030200Z

the point is that that doesn't inject the cider-nrepl stuff, so it seems that my option would be convincing clojure-lsp to create a server with that dependency in the first place

william 2021-03-21T15:57:52.030500Z

I think that should involve adding the dependency here https://github.com/clojure-lsp/clojure-lsp/blob/master/project.clj#L27

william 2021-03-21T15:58:23.031500Z

so that that line looks more like:

:debug ^:leaky {:dependencies [[nrepl/nrepl "0.8.3"]
                                            [cider/cider-nrepl "0.25.9"]]}

william 2021-03-21T15:59:39.032300Z

but this seems not to be enough to start the nrepl server with the extension (I'm issuing lein with-profile +debug bin)

dpsutton 2021-03-21T16:00:33.033200Z

i would add the deps from the -Sdeps from that startup command to your .clojure/deps.edn and then set your clojure lsp startup command to clj -M:cider/nrepl:debug

dpsutton 2021-03-21T16:03:51.035100Z

{:aliases
 {:cider/nrepl
  {:deps {nrepl/nrepl {:mvn/version "0.8.3"}
          refactor-nrepl {:mvn/version "2.5.1"}
          cider/cider-nrepl {:mvn/version "0.25.9"}}
   :main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware"}}}
add this alias to your ~/.clojure/deps.edn file and then clj -M:debug:cider/nrepl and you should have what you need. again, with the caveat that you might need to manually "start" the lsp stuff since the nrepl will be the main instead of whatever clojure-lsp expects to be the main

william 2021-03-21T16:05:00.035500Z

does this work even if clojure-lsp is based on lein?

dpsutton 2021-03-21T16:08:04.036200Z

no. this is just if you're using deps.edn which i think will be the easier way to do this. but clojure-lsp has a deps.edn file and now that the java is compiled i thought you said it works

william 2021-03-21T16:09:41.037300Z

no, @ericdallo explained that there's a WIP branch for clojure-lsp which will switch to clojure-cli, but currently clojure-lsp is a lein project

dpsutton 2021-03-21T16:18:32.038Z

~/p/c/clojure-lsp ❯❯❯ clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.8.3"} cider/cider-nrepl {:mvn/version "0.25.9"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[cider.nrepl/cider-middleware]"]}}}' -M:cider/nrepl
nREPL server started on port 59664 on host localhost - <nrepl://localhost:59664>

🙌 1
dpsutton 2021-03-21T16:18:38.038200Z

works for me

dpsutton 2021-03-21T16:19:22.038500Z

i just copied the normal cider startup command and ran it in clojure-lsp

dpsutton 2021-03-21T16:20:34.038700Z

connects, etc

dpsutton 2021-03-21T16:21:05.038900Z

debugging:

william 2021-03-21T16:22:15.039400Z

I see I see, let me try 🤯

bozhidar 2021-03-21T16:23:44.040100Z

Btw, you can actually inject cider-nrepl into a running nREPL instance, but CIDER can't do this just yet.

bozhidar 2021-03-21T16:24:04.040800Z

(you can inject any middleware in a running nREPL server)

dpsutton 2021-03-21T16:24:06.040900Z

i thought i remembered some functionality like that but i figured it would complicate things at this stage

bozhidar 2021-03-21T16:24:42.042200Z

The functionality exists, but I never got to implementing the support for it in CIDER - I do know that some people played with it and it worked for them (on other editors).

dpsutton 2021-03-21T16:24:44.042300Z

i had a patch to create the startup command and copy it for exactly these situations but i don't know where its gone. some gist, branch, or stash somewhere

dpsutton 2021-03-21T16:25:24.043300Z

as of now have to crank up some test project, copy from the repl buffer, and then kill it, then start up a project from the command line and connect to it

william 2021-03-21T16:25:35.043600Z

@dpsutton what happens when I start the command you pasted in the clojure-lsp directory is that a server starts, then I open the clojure-lsp codebase in my editor, cider-connect there,

william 2021-03-21T16:25:47.044Z

and when I try to mark something for debug I get:

william 2021-03-21T16:26:17.044300Z

https://i.imgur.com/SRKTR2Q.png

dpsutton 2021-03-21T16:27:16.044700Z

did you require that namespace first?

dpsutton 2021-03-21T16:27:28.045100Z

i don't know what's going on at the moment. which function are you trying to instrument?

william 2021-03-21T16:28:15.046Z

ah no, ok, if I evaluate all the buffer first, I can correctly instrument it (I never understood when/if I have to load namespaces myself, and when it's done automatically)

william 2021-03-21T16:28:31.046200Z

still though

william 2021-03-21T16:29:26.047200Z

I'm instrumenting the incoming function at clojure-lsp/src/clojure_lsp/feature/call_hierarchy.clj, and then I call that feature from an emacs command, and it doesn't stop at my breakpoint

william 2021-03-21T16:30:15.048100Z

I invoke the command M-x lsp-treemacs-call-hierarchy which is supposed to use incoming at some point

william 2021-03-21T16:31:01.048900Z

I suspect that happens because the running instance of the lsp server knows nothing about the server I created with the last command

dpsutton 2021-03-21T16:31:16.049300Z

i suspect that you're running the clojure-lsp server in the terminal and your emacs also started its own clojure-lsp server for editing

dpsutton 2021-03-21T16:31:29.049700Z

did you update the command that emacs uses ot create an lsp server?

dpsutton 2021-03-21T16:32:08.050400Z

but remember your goal is for emacs, when it starts up a clojure-lsp server, to start one that has an nrepl server you can connect to

william 2021-03-21T16:32:10.050600Z

I'm using:

(after! clojure-mode (setq lsp-clojure-custom-server-command '("bash" "-c" "/home/carlo/code/clojure/clojure-lsp/target/clojure-lsp")))
for that now, which probably isn't the one I want. Maybe this is the place for the command that we used before, not my shell

dpsutton 2021-03-21T16:35:27.051200Z

yes. that's starting a clojure-lsp as normal, without any cider stuff involved

william 2021-03-21T16:38:56.052Z

right, so I modified it to:

(after! clojure-mode (setq lsp-clojure-custom-server-command '("bash" "-c" "cd /home/carlo/code/clojure/clojure-lsp &amp;&amp; clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version \"0.8.3\"} cider/cider-nrepl {:mvn/version \"0.25.9\"}} :aliases {:cider/nrepl {:main-opts [\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[cider.nrepl/cider-middleware]\"]}}}' -M:cider/nrepl")))
but I can't start lsp-mode now

dpsutton 2021-03-21T16:40:14.052600Z

right. because you've swapped out the mains. go see what clojure-lsp is doing at the beginning, cider connect, and call that

william 2021-03-21T16:40:43.053400Z

could you explain what swapped out the mains means? What's a main in this context?

dpsutton 2021-03-21T16:40:45.053500Z

this is where it might be easier to just add an nrepl server with the right middleware to the main of clojure-lsp. it needs to start up its stuff

dpsutton 2021-03-21T16:41:21.054Z

main-opts [\"-m\" \"nrepl.cmdline\" \"--middleware\" \"[cider.nrepl/cider-middleware]\"] this is what is running at startup. its starting an nrepl server. it's NOT starting an lsp server.

dpsutton 2021-03-21T16:42:03.054400Z

https://github.com/clojure-lsp/clojure-lsp/blob/master/deps.edn#L21 is what you need to call to start an lsp server

william 2021-03-21T16:42:10.054900Z

right

william 2021-03-21T16:43:17.055700Z

so, is just swapping nrepl.cmdline -> clojure-lsp.main enough?

dpsutton 2021-03-21T16:43:38.056Z

well then you lose your nrepl server you want to connect to

dpsutton 2021-03-21T16:44:25.057Z

you need to "start" two things. and you only have a way to automatically do one. so its probably easier to just add cider and stuff to clojure-lsp and have the main start up an nrepl server with the correct middleware

william 2021-03-21T16:45:50.057500Z

ok, and I can accomplish this by modifying my https://github.com/clojure-lsp/clojure-lsp/blob/master/deps.edn

william 2021-03-21T16:46:19.058400Z

the main-opts in run is where I will have to insert the nrepl-call, and the dependencies can go in debug?

dpsutton 2021-03-21T16:46:28.058700Z

probably not. you want to add an (nrepl/start-server ...) code in the main run in clojure-lsp

william 2021-03-21T16:58:07.060100Z

I don't know how to accomplish this, it's possible that the task is beyond my current understanding 😂

william 2021-03-21T16:58:50.060400Z

what should I read to bridge the gap?

dpsutton 2021-03-21T17:01:32.061400Z

my suggestion would be start a small simple project and figure out how to start an nrepl server in code and then connect to it. once you've got that, figure out how to get cider's middleware into it and connect to it. once that is finished figure out how to do that in clojure-lsp and then you're done

☝️ 1
🙏 1
agilecreativity 2021-03-21T20:36:50.063300Z

Not sure if this is specific to cider, or maybe just Emacs. Is there a way to make sure that the ; in code does not get format to the right of the code in Clojure e.g. If the comment string is ;; then the code in Emacs indent with the code before/after it but not with the single ; string

bozhidar 2021-03-22T07:07:40.064Z

See also https://guide.clojure.style/#comments

👍 1
agilecreativity 2021-03-23T04:55:20.068800Z

Thanks @bozhidar will take a peek at the code

agilecreativity 2021-03-23T04:58:05.069Z

The reason is when I have to contribute to some project that they don't use Cider/Emacs the code will get reformatted on save which I don't want to introduce as part of my contribution. Thanks again for your pointer.

bozhidar 2021-03-23T07:31:07.069200Z

You're welcome!

West 2021-03-21T23:05:52.063500Z

Awww man, I've had a similar frustration with this as well, trying to add and delete ;; comments.