Fork me on GitHub
#lsp
<
2023-02-07
>
juhoteperi14:02:22

I have a multi module project, with a/deps.edn, b/deps.edn etc., each module spawns its own LSP process which is fine in this case. But this project only has one shared clj-kondo config in .clj-kondo/config.edn. Clj-kondo searches for the config upwards until it finds .clj-kondo folder, but Clojure-lsp only checks for one relative to project-root-uri: https://github.com/clojure-lsp/clojure-lsp/blob/master/lib/src/clojure_lsp/kondo.clj#L24-L31 https://github.com/clj-kondo/clj-kondo/blob/master/src/clj_kondo/impl/core.clj#L195-L214

borkdude14:02:33

@U061V0GG2 The solution, if you want to have a shared clj-kondo config is to add {:config-paths ["../../clj-kondo-shared"]} or so in your project .clj-kondo/config.edn

borkdude14:02:45

the path is relative to the .clj-kondo directory

juhoteperi14:02:41

Yeah I can workaround this by adding config.edn in each of the 10 modules, but seems a bit incovenient to add these for clojure-lsp, while clj-kondo command works fine.

borkdude14:02:27

in this case I'll defer to @UKFSJSM38 :)

ericdallo14:02:14

For deps mono-repo, I suggest opening a single clojure-lsp session at the mono-repo project root, this would avoid multiple clojure-lsp process taking your machine resources, would make you navigate between submodules easily and you could have a single .clj-kondo/config.edn in the the monorepo root as well

ericdallo14:02:57

clojure-lsp is a mono-repo deps itself, you can use it as example, but the idea is to have a deps.edn at the root with dev alias linking all the submodules, similar to how polylith does

juhoteperi14:02:47

Hmm, makes sense. Also removes need for n+1 processes. We even already have something like that to generate classpath for some clj-kondo operations.

ericdallo15:02:57

seems like you should take a look at clojure-lsp diagnostics which takes care of all of that :) https://clojure-lsp.io/api/cli/

🪄 2
juhoteperi15:02:25

Yeahh... well others aren't using LSP and are "conservative" about the tooling.

2
Matthew Twomey16:02:17

I am just fine-tuning some personal preferences in my emacs / babashka setup. One thing I’d like to do is use the lsp on babashka script files that have no filename extension. Right now I am running into trouble with this: I have a script called update-gcp-job-env. When I open this file, emacs switches to clojure-mode (presumably because of the #!/usr/bin/env bb ?). However when I launch lsp - it has an error parsing the file:

[Trace - 09:49:25 AM] Received notification 'textDocument/publishDiagnostics'.
Params: {
  "uri": "file:///Users/mtwomey/Git_Repos/babashka-scripts/update-gcp-job-env",
  "diagnostics": [
    {
      "range": {
        "start": {
          "line": 0,
          "character": 0
        },
        "end": {
          "line": 0,
          "character": 0
        }
      },
      "tags": [],
      "message": "Can't parse /Users/mtwomey/Git_Repos/babashka-scripts/update-gcp-job-env, No matching clause: :unknown",
      "code": "syntax",
      "severity": 1,
      "source": "clj-kondo"
    }
  ]
}
I can rename this exact file to and in that case when I open it and launch lsp - it works fine. Is there something I can do to get lsp working when the file has no extension?

ericdallo16:02:38

Hum, I thought we fixed that kind of issue to fallback to clj when an unknown extension

Matthew Twomey16:02:41

Well even when it’s failing it does seem to correctly detect clojure:

[Trace - 09:49:25 AM] Sending notification 'textDocument/didOpen'.
Params: {
  "textDocument": {
    "uri": "file:///Users/mtwomey/Git_Repos/babashka-scripts/update-gcp-job-env",
    "languageId": "clojure",
    "version": 0,

ericdallo16:02:32

yeah, that's correct, what is giving this error is clj-kondo when we tell it to lint the file. I noticed we are indeed checking if the extension is unknown to not pass any lang to clj-kondo https://github.com/clojure-lsp/clojure-lsp/blob/5488311a2f135741abb7d6a8fb5f07472a7f17c5/lib/src/clojure_lsp/kondo.clj#L305-L306

ericdallo16:02:39

so clj-kondo would use the clj as default

ericdallo16:02:47

what clojure-lsp version you are using?

Matthew Twomey16:02:58

Is there a quick command to get version?

ericdallo16:02:24

lsp-clojure-server-info , should print a map and there will have server-version

Matthew Twomey16:02:44

Ah ok:

:server-version "2022.10.05-16.48.26-nightly",
 :clj-kondo-version "2022.10.05",

ericdallo16:02:18

I think we fixed after this version :)

ericdallo16:02:28

latest release should fix

Matthew Twomey16:02:48

Awesome, thanks so much. I will pull this down. Appreciate your help!

đź‘Ť 2
Matthew Twomey16:02:52

Reporting back: Updating LSP version did indeed fix the issue!! Thanks again.

ericdallo16:02:18

Nice! You're welcome!

Matthew Twomey17:02:09

One small followup @UKFSJSM38: Various lsp functionality is still a bit “weird” for me in files without extensions. lsp-docs and so forth. A specific example is when I do an lsp-find definition it’s not found. In the log I see:

[Trace - 11:50:05 AM] Sending request 'textDocument/definition - (18)'.
Params: {
  "textDocument": {
    "uri": "file:///Users/mtwomey/Git_Repos/babashka-scripts/update-gcp-job-env"
  },
  "position": {
    "line": 22,
    "character": 22
  }
}


[Trace - 11:50:05 AM] Received response 'textDocument/definition - (18)' in 20ms.
Result: null
Note - the line shown here is actually one off - (I actually triggered it on line 23, not 22). Maybe that’s just because it’s ignoring the #! line at the start.

Matthew Twomey17:02:39

(this same functionality works if I add the .bb extension)

ericdallo17:02:41

that happens probably because of missing classpath, lsp is running in "basic mode", to be able to have valid classpaths you wouldneed a bb.edn file with :srcs correct and the script in there

Matthew Twomey18:02:40

Ok - but how come it works if I simply add a .bb extension to the file name?

Matthew Twomey18:02:35

(also note - many things do work it detects references correctly, lsp-rename works, …etc)

ericdallo18:02:13

hum, maybe there is something to improve in lsp, TBH is not well supported files without extensions

ericdallo18:02:58

Probably not worth it, a lot of lsp code expect files to have extensions, maybe it's easier to rename to .bb?

Matthew Twomey18:02:12

Fair enough. I don’t object to digging in myself a bit and maybe do a PR. Could you give me a general pointer as to where I might start looking?

ericdallo18:02:08

Sure, I actually have a clue where is the issue: we use in a lot of places xf-same-lang https://github.com/clojure-lsp/clojure-lsp/blob/master/lib/src/clojure_lsp/queries.clj#L264, so it's checking if the definition lang (.clj) matches the current file (:unknown), that's probably the issue, we could maybe improve xf-same-lang to treat unknown as clj at least

Matthew Twomey18:02:46

Ok - thanks sir. I realize having the extension isn’t a big deal for most people. Just a small preference for me - so trying to improve it a little will just be a learning opportunity for me. Will keep you posted.

đź‘Ť 2
Matthew Twomey06:02:30

Ok - when you have a second @UKFSJSM38, I’d like your thought. I dug around and learned quite a bit about this code base. The issue turns out that the file uri looks like this: :uri "file:///Users/mtwomey/Git_Repos/babashka-scripts/update-gcp-job-env" (logging var-usage variable) The check in defn elem-langs [element] - that uses shared/uri->available-langs. shared/uri->available-langs has no case for no extension. As a test, I did this:

modified   lib/src/clojure_lsp/shared.clj
@@ -135,7 +135,7 @@
     (string/ends-with? uri ".bb") #{:clj}
     (string/ends-with? uri ".cljd") #{:clj}
     (string/ends-with? uri ".clj_kondo") #{:clj}
-    :else #{}))
+    :else #{:clj}))
 
 (defn ^:private conform-uri
   "Changes and returns URI given FORMAT-SETTINGS:
This fixes the doc / hover / find-definition issue. I am not sure if this is safe or desired though (defaulting to clj). What do you think?

ericdallo11:02:06

Yeah, I think it's ok to do that as it's the same kondo does for unknown langs, would be nice to have a find definition test for that as well

Matthew Twomey15:02:36

Ok - cool. Will add a test also and create a PR.