Fork me on GitHub
#lsp
<
2022-10-07
>
Drew Verlee04:10:53

Do i need to tell lsp about extra-deps defined in my global deps file .clojure/deps.edn?

Drew Verlee04:10:26

the answer seems to be yes given i get the lsp auto complete when i include the deps directly.

Drew Verlee04:10:49

i can figure this out, i feel like i ask this same question in a different way ever 3 months...

Drew Verlee04:10:05

i need to set the source path

practicalli-johnny08:10:10

I use :source-aliases to tell Clojure LSP to include specific project and user level aliases. Aliases I use for every Clojure project go in my user level configuration for Clojure LSP, e.g :env/dev and :env/test https://github.com/practicalli/clojure-lsp-config#clojure-lsp-configuration

๐Ÿ‘ 1
ericdallo12:10:23

clojure-lsp will just delegate the classpath finding to lein or clj , and derive the source-paths from that, if you want more source-aliases to be included on lein's with-profiles or deps -A: you should add to source-aliases โ˜๏ธ

๐Ÿ‘ 1
ericdallo12:10:38

by default clojure-lsp consider dev and test aliases only.

skylize20:10:57

On my wishlist: Regex comments recognized as comments by lsp.

(let [pattern #"(?x)  # comment flag:
                      # enables free whitespace
                      # and # delineated comments
                
                foo   # literal foo
                \     # literal space
                \w+   # 1+ word chars"]
  (re-find pattern "tofoo bar"))
;; => "foo bar"

borkdude21:10:15

Which language is this, CLJ or CLJS?

borkdude21:10:05

and what do you expect lsp to do with this?

borkdude21:10:35

(I just tested and this seems to work in CLJ)

skylize21:10:33

CLJ. I would expect it to be marked as a comment, so editor can apply comment syntax highlighting like any other comment.

borkdude21:10:26

so just for highlighting

skylize21:10:27

I imagine it's not at the top of anybody's priorities, though. I'm probably just dreaming.

ericdallo21:10:04

Wow, didn't know that worked

ericdallo21:10:38

We would need clj-kondo work, also, not sure editors already know that like clojure-mode for emacs and vscode internals

ericdallo21:10:38

In the end is just a string as usual right? Not sure if we should hightlight that

borkdude21:10:54

yeah, I don't think editors support this

skylize21:10:32

I see other syntax highlighting inside regex literals already.

skylize21:10:35

Oh,, nevermind. That's just highlighting escapes. So I guess interpreted as a string like you say.

seancorfield21:10:05

I'm looking at clojure-lsp.api since @ericdallo suggested I use it instead of our custom ns sort script. I'm a bit puzzled as to how to use it and the docs aren't helping. Our project structure might be the issue here:

wsmain -- the repo
  +-- .clj-kondo
  +-- .lsp
  +-- clojure -- where our Clojure source code is
        +-- deps.edn
In order to use clojure-lsp.api via an alias, I need to be in wsmain/clojure to run... well, the docs don't quite say... but it looks like it should be clojure -T:lint-ns (since there's :replace-deps). However, that definitely won't work since it won't know it needs to use :dev and :everything aliases to pick up deps and paths to find the source code (almost everything is :extra-deps with :local/root since this is a Polylith monorepo). I tried changing that to :extra-deps and running clojure -X:dev:everything:lint-ns but that still doesn't find any code to analyze/fix. Also, when I run it, it creates .clj-kondo and .lsp in the clojure folder which is probably not what I want -- so I figure I need :project-root but I can't supply that from the command-line because the API code requires that be a java.io.File rather than a string. Halp?!?!

seancorfield21:10:58

I added a shim in our development folder so ws.lsp/clean-ns! becomes the -T function and it in turn calls clojure-lsp.api/clean-ns! and passes :project-root (io/file "..") and that seems to have resolved the problem. If :project-root could be a string, I wouldn't need to do this.

seancorfield21:10:52

I'm a bit surprised it seems to recompute the classpath and re-analyze the project each time -- I would have expected that to be cached?

ericdallo21:10:05

Yes but that won't work since your deps.edn is in another folder different than your .LSP folder you mentioned which is odd

ericdallo21:10:35

Why you would like to have clojure tools configs like clj-kondo and lsp not in the same folder of your clojure folder like deps.edn

ericdallo21:10:27

The API works similar to the editor, if you open your project in your editor in the clojure folder, that's the project root you should pass for example

seancorfield21:10:49

wsmain is where the editor opens. That's the project root.

seancorfield21:10:18

(I've already had to go through this dance with VS Code/Calva and the way it invokes LSP etc)

ericdallo21:10:36

And how clojure-lsp knows that the deps.edn is the clojure folder? Do you customize the classpath somehow?

seancorfield21:10:33

Yes. Like I say, I already spent a lot of time configuring this for Calva ๐Ÿ˜

ericdallo21:10:20

if works for calva it should work the same, clojure-lsp.api just calls what the editor code calls

seancorfield21:10:46

wsmain/.lsp/config.edn:

{:project-specs [{:project-path "clojure/deps.edn"
                  :classpath-cmd ["build/bin/classpath.sh"]}]
 :source-aliases #{:build :dev :everything :runner :test}}
and classpath.sh:
#!/bin/sh

cd /Developer/workspace/wsmain/clojure

../build/clojure/bin/clojure -A:build:dev:everything:runner:test -Spath
But we run all the build/dev/test tooling in wsmain/clojure because that's where the main deps.edn file is. Hence needing to specify :project-root which, annoyingly, does not accept a string.

seancorfield22:10:57

Without :project-root, it does not seem to look up the directory tree to find wsmain/.lsp/config.edn and then work with the .lsp and .clj-kondo folders at that level.

seancorfield22:10:10

So:

;; copyright (c) 2022 world singles networks llc

(ns ws.lsp
  (:require [ :as io]))

(defn clean-ns! [opts]
  ((requiring-resolve 'clojure-lsp.api/clean-ns!)
   (assoc opts :project-root (io/file ".."))))

seancorfield22:10:59

And in wsmain/clojure/deps.edn:

:sort-ns
  {:replace-paths ["development/src"]
   :replace-deps {com.github.clojure-lsp/clojure-lsp {:mvn/version "RELEASE"}}
   :exec-fn ws.lsp/clean-ns!
   :exec-args {:dry? true
               :settings
               {:clean
                {:ns-inner-blocks-indentation :same-line
                 :ns-import-classes-indentation :same-line
                 :sort {:refer {:max-line-length 1}}}}}}
which gets us close to the behavior we want.

seancorfield22:10:28

What I've learned from this exercise is that we are not very consistent with our :import clauses! ๐Ÿ˜†

ericdallo22:10:11

yeah receive as a file https://github.com/clojure-lsp/clojure-lsp/pull/455#discussion_r659250513. > Without :project-root, it does not seem to look up the directory tree to find wsmain/.lsp/config.edn and then work with the .lsp and .clj-kondo folders at that level. without project-root will use the root as where the command was run, so we need to checks from where you are running that. > What I've learned from this exercise is that we are not very consistent with our :import clauses! ๐Ÿ˜† Yeah, that's one good thing about not having custom scripts, you never know if your script is really doing that other tools do

ericdallo22:10:00

all of that headache is probably because your deps.edn is in a place different than the project root (where you have your lsp configs), I suggest in the future you move everything related to clojure to the clojure folder indeed

seancorfield22:10:09

The diff output from the above is about 1,600 lines... so this is going to be a big, one-off PR to reformat our code and then keep it this way. Which is fine. Now I just need to integrate this into our build.clj file (and then I can remove the ws.lsp shim now I have it working.

seancorfield22:10:27

No, the deps.edn must be in the Polylith workspace -- which is wsmain/clojure

ericdallo22:10:53

got that, but why .lsp and .clj-kondo are outside that clojure folder?

ericdallo22:10:06

why don't have everything related with clojure, in the clojure folder ๐Ÿ˜… ?

seancorfield22:10:26

Because Calva runs LSP in the project root -- where the editor opens -- as I said above.

ericdallo22:10:53

so, sounds like a Calva issue, you can open a project in /foo/bar in emacs for example and have lsp start in /foo/bar/baz

seancorfield22:10:18

You can search back in the discussions here but I went round and round with this when I switched to Calva -- and you and @U0ETXRFEW were in that discussion ๐Ÿ™‚

seancorfield22:10:05

I tried a lot of combinations of file placements and configuration and what I've shown in this thread was pretty much the only configuration that actually works properly.

ericdallo22:10:23

I suggest opening a issue on calva, where you could specify the project root

seancorfield22:10:36

I don't care, to be honest, now that I have it working. And I really don't want to move stuff around in the repo again at this point (otherwise I'd have moved everything in clojure up to the top-level ages ago -- even tho' that means having a build tree full of scripts and config files and a documentation tree full of markdown files mixed in with our Clojure code which... ugh!).

ericdallo22:10:32

Sure, glad it's working

seancorfield22:10:10

Re: issue 455 -- I should have known that it would be vemv who suggested not accepting a string :rolling_on_the_floor_laughing:

๐Ÿ‘€ 1
seancorfield22:10:52

Thank you for your help/input! I have the PR in at work to switch the tooling from our custom sort script to LSP's clean-ns! and I'm now working on the actual PR for all of the reformatting changes ๐Ÿ™‚

ericdallo22:10:27

Awesome! Happy to see it's working! Happy refactor :p

pez22:10:07

Iirc we found this to be a limitation in vscode. Calva canโ€™t set the project root unless we find another way to do it, non-standard. The only way to do it in VS Code is to use workspace folders and have the one you want as project root be the topmost way folder.

๐Ÿ‘ 1
๐Ÿ˜” 1