Fork me on GitHub
#cider
<
2024-05-09
>
César Olea20:05:23

I’m using cider + lsp-clojure with the clojure-lsp backend. xref only works if the file containing the target function has already been opened before, otherwise it results in an error like xref--not-found-error: No definitions found for: tx/list-transaction but if the file containing tx/list-transactions has been opened before, it works. I have lsp-enable-xref t and cider-use-xref nil

César Olea20:05:43

I don’t recall exactly why I have cider-use-xref nil but I think it’s to use cider’s own xref backend instead of emacs. Also whenever I open a new file see a message in the minibuffer like this LSP :: Connected to [clojure-lsp:42929 /path/to/my/project/.

César Olea20:05:18

Any pointers in debugging this are appreciated! I’d like it so that I can jump to definitions in the project without having to open every file first.

vemv20:05:36

If you set cider-use-xref nil then CIDER won't plug itself into the xref system and only the lsp backend (and possibly other backends) will be present Note that at least for cider, you cannot change this at runtime - it's most recommended it to set it early in Emacs init

vemv20:05:34

You can M-: xref-backend-functions to check its runtime value. If you want lsp and nothing else, remove others

César Olea21:05:59

I see, I have (lsp--xref-backend t) and I tried setting cider-use-xref t before and it did nothing, but as you said, you can’t change this at runtime. I’ll try setting it early as suggested and report back.

César Olea21:05:18

Yeah, it works. Thank you @U45T93RA6! You need to be connected to the REPL for it to work, and I guess one of the advantages of lsp-clojure is the static analysis, but I guess the backend server doesn’t do the necessary indexing for xref to work project-wide without opening each file first? :man-shrugging:

vemv21:05:05

Most certainly neither needs the file to be open for analysis purposes. Both analyse stuff using Clojure (not Elisp). CIDER however had a pretty buggy initial xref implementation, and I recall fixing things such that it doesn't depend on whether there's a buffer open for displaying it. You'd need a reasonably recent CIDER (think: latest 9 months)

vemv21:05:16

Re implementations: lsp is superior as it works with clojurescript. CIDER's works because of a fortunate characteristic of the JVM Clojure compiler that makes a xref implementation easy for us You can learn about that nice hack here https://metaredux.com/posts/2019/05/04/discovering-runtime-function-references-in-clojure.html

César Olea21:05:26

Interesting! thanks for sharing, I’ll dive in. > Most certainly neither needs the file to be open for analysis purposes. Then it must be something with my configuration, because I can’t get xref to work properly when combining cider and lsp. It works in other non-clojure projects though. I’m happy with this solution though.