emacs-lsp / lsp-mode

Emacs client/library for the Language Server Protocol
https://emacs-lsp.github.io/lsp-mode
GNU General Public License v3.0
4.8k stars 892 forks source link

lsp-find-definition: (wrong-type-argument xref-item nil) #3930

Closed lassemaatta closed 1 year ago

lassemaatta commented 1 year ago

Thank you for the bug report

Bug description

lsp-find-definition throws an error (see attached stack trace).

Also happens with M-x lsp-start-plain.

This primarily seems to happen when applying lsp-find-definition to functions defined in external libraries. Navigating to functions defined in the project seems to work.

Steps to reproduce

  1. Open a clojure project
  2. Open a namespace, which invokes a library function
  3. Try to navigate to the library function with lsp-find-definition
  4. See error.

Expected behavior

Navigating to functions with lsp-find-definition should not throw an error.

Which Language Server did you use?

clojure-lsp

OS

Linux

Error callstack

Debugger entered--Lisp error: (wrong-type-argument xref-item nil)
  xref-item-location(nil)
  xref-pop-to-location(nil nil)
  xref-show-definitions-buffer(#f(compiled-function (&rest _) #<bytecode 0x6b9aca8a7344924>) ((window . #<window 3 on proxy_services_stest.clj>) (display-action)))
  lsp-show-xrefs(nil nil nil)
  lsp-find-locations("textDocument/definition" nil :display-action nil)
  lsp-find-definition()
  funcall-interactively(lsp-find-definition)
  command-execute(lsp-find-definition)

Anything else?

Running GNU Emacs 30.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.36, cairo version 1.17.6) of 2023-01-24 and LSP :: lsp-mode 20230124.1058, Emacs 30.0.50, gnu/linux.

yyoncho commented 1 year ago

@ericdallo willing to take a look?

ericdallo commented 1 year ago

I can't repro that, maybe it could be something that only happens on emacs master? @lassemaatta could you:

That would help understand better, we need to understand why lsp--locations-to-xref-items is returning nil, but for that we need to know what server is returning

lassemaatta commented 1 year ago

Sure, I'll test another version of emacs and check the logs tomorrow 👍

yyoncho commented 1 year ago

I doubt it is from the emacs version. It is something between the client and the server.

lassemaatta commented 1 year ago

@ericdallo: I did a quick test (trying to find the definition of mount.core/start) without switching my emacs version:

[Trace - 05:03:02 pm] Sending request 'textDocument/definition - (35)'.
Params: {
  "textDocument": {
    "uri": "file:///home/lassemaatta/work/lupapiste/stest/lupapalvelu/proxy_services_stest.clj"
  },
  "position": {
    "line": 21,
    "character": 1
  }
}

[Trace - 05:03:02 pm] Received response 'textDocument/definition - (35)' in 2ms.
Result: {
  "uri": "zipfile:///home/lassemaatta/.m2/repository/mount/mount/0.1.16/mount-0.1.16.jar::mount/core.cljc",
  "range": {
    "start": {
      "line": 280,
      "character": 6
    },
    "end": {
      "line": 280,
      "character": 11
    }
  }
}

Nothing else appears in the *lsp-log: clojure-lsp:<port>* log/buffer after that.

lassemaatta commented 1 year ago

And just for comparison; running the same for a project-local symbol (which works fine):

[Trace - 05:08:57 pm] Sending request 'textDocument/definition - (38)'.
Params: {
  "textDocument": {
    "uri": "file:///home/lassemaatta/work/lupapiste/stest/lupapalvelu/proxy_services_stest.clj"
  },
  "position": {
    "line": 19,
    "character": 1
  }
}

[Trace - 05:08:57 pm] Received response 'textDocument/definition - (38)' in 2ms.
Result: {
  "uri": "file:///home/lassemaatta/work/lupapiste/test-utils/lupapalvelu/itest_util.clj",
  "range": {
    "start": {
      "line": 201,
      "character": 5
    },
    "end": {
      "line": 201,
      "character": 14
    }
  }
}

Perhaps there's something funky going on with that zipfile URL.

ericdallo commented 1 year ago

Oh yeah, that zip file doesn't work for emacs, the initializationOption dependecy-scheme should be jar for emacs, that's weird though since we always set that on lsp-clojure.el

ericdallo commented 1 year ago

Could you paste the output of lsp-clojure-server-info ?

lassemaatta commented 1 year ago

Sure. Here's a somewhat cleaned up version without the classpath jars etc.

{:final-settings     {:source-aliases                    #{:test :dev},
                      :uri-format                        {:upper-case-drive-letter? false, :encode-colons-in-path? false},
                      :cljfmt-config-path                ".cljfmt.edn",
                      :document-formatting?              true,
                      :source-paths                      ["a bunch of source paths omitted"],
                      :text-document-sync-kind           nil,
                      :project-specs                     ({:project-path  "project.clj",
                                                           :classpath-cmd ["/usr/bin/lein" "with-profile" "+test,+dev" "classpath"]}
                                                          {:project-path  "deps.edn",
                                                           :classpath-cmd ["/usr/bin/clojure" "-A:test:dev" "-Spath"]}
                                                          {:project-path  "build.boot",
                                                           :classpath-cmd ["boot" "show" "--fake-classpath"]}
                                                          {:project-path  "shadow-cljs.edn",
                                                           :classpath-cmd ["/usr/local/bin/npx" "shadow-cljs" "classpath"]}
                                                          {:project-path  "bb.edn",
                                                           :classpath-cmd ["/usr/bin/bb" "print-deps" "--format" "classpath"]}),
                      :dependency-scheme                 "jar",
                      :linters                           {:unused-public-var {:level :off}},
                      :lint-project-files-after-startup? true,
                      :show-docs-arity-on-same-line?     true,
                      :document-range-formatting?        true,
                      :clean                             {:ns-inner-blocks-indentation :same-line,
                                                          :sort                        {:ns true, :require true, :import true, :refer true}}},
 :classpath          #{"omitted a billion jars and a few directories.."},
 :cljfmt-raw         "{}",
 :client-settings    {:dependency-scheme             "jar",
                      :show-docs-arity-on-same-line? true,
                      :text-document-sync-kind       nil,
                      :source-paths                  nil,
                      :source-aliases                nil,
                      :cljfmt-config-path            ".cljfmt.edn",
                      :document-formatting?          true,
                      :document-range-formatting?    true},
 :project-root-uri   "file:///home/lassemaatta/work/lupapiste",
 :port               "NREPL only available on :debug profile (`bb debug-cli`)",
 :project-settings   {:source-paths                      #{"a bunch of source paths"},
                      :show-docs-arity-on-same-line?     true,
                      :lint-project-files-after-startup? true,
                      :linters                           {:unused-public-var {:level :off}},
                      :clean                             {:ns-inner-blocks-indentation :same-line,
                                                          :sort                        {:ns true, :require true, :import true, :refer true}}},
 :server-version     "2022.12.09-15.51.10",
 :clj-kondo-version  "2022.12.08",
 :log-path           "/tmp/clojure-lsp.12885567199498124818.out",
 :classpath-settings nil}

I guess the relevant part is the :dependency-scheme "jar"

yyoncho commented 1 year ago

@lassemaatta can you upgrade your clojurelsp?

ericdallo commented 1 year ago

that looks updated an correct, we can see there is a :dependency-scheme "jar" which is the correct, I wonder why clojure-lsp is retuning zipfile. Anyway, looks like a server bug, we can close this and create a issue on clojure-lsp, it'd be nice to confirm if that happens in small simple repros as well @lassemaatta, I usually use clojure-sample to test some things

lassemaatta commented 1 year ago

Sounds good. I'll try to find the time to dig deeper, try to isolate the problem, and then open a ticket for clojure-lsp with details. It looks like this problem is somehow project-specific; I have a couple of large projects where (as far as I know) only one of them exhibits this problem.