Go语言 lsp模式,用于出现“会话中无视图”错误

hk8txs48  于 2023-09-28  发布在  Go
关注(0)|答案(2)|浏览(119)

我想做的事

我想一起使用go-mode/lsp-mode。一开始我很难让lsp模式执行,最后通过附加路径:facepalm:让它工作。

问题现在的问题是,当lsp-mode为工作的golang目录启动时,我收到以下错误:

LSP :: Error from the Language Server: no views in the session (Unknown error) [3 times]

我已经在互联网上寻找答案,但还没有找到任何与我的问题有关的东西。我在向社区寻求一些指导。

go-mode.el

(defun custom-go-mode ()
  (display-line-numbers-mode 1))

(use-package go-mode
:defer t
:ensure t
:mode ("\\.go\\'" . go-mode)
:init
  (setq compile-command "echo Building... && go build -v && echo Testing... && go test -v && echo Linter... && golint")  
  (setq compilation-read-command nil)
  (add-hook 'go-mode-hook 'custom-go-mode)
:bind (("M-," . compile)
("M-." . godef-jump)))

(setq compilation-window-height 14)
(defun my-compilation-hook ()
  (when (not (get-buffer-window "*compilation*"))
    (save-selected-window
      (save-excursion
    (let* ((w (split-window-vertically))
           (h (window-height w)))
      (select-window w)
      (switch-to-buffer "*compilation*")
      (shrink-window (- h compilation-window-height)))))))
(add-hook 'compilation-mode-hook 'my-compilation-hook)

(global-set-key (kbd "C-c C-c") 'comment-or-uncomment-region)
(setq compilation-scroll-output t)

lsp-mode.el

(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/npm")))
(setq exec-path (append exec-path '("/Users/seanh/.nvm/versions/node/v12.13.0/bin/")))
(setq exec-path (append exec-path '("/usr/local/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin")))
(setq exec-path (append exec-path '("/Users/seanh/go/bin/gopls")))
(setq exec-path (append exec-path '("/usr/local/go/bin/go")))

(use-package lsp-mode
  :ensure t
  :commands (lsp lsp-deferred)
  :hook (go-mode . lsp-deferred))

;;Set up before-save hooks to format buffer and add/delete imports.
;;Make sure you don't have other gofmt/goimports hooks enabled.

(defun lsp-go-install-save-hooks ()
  (add-hook 'before-save-hook #'lsp-format-buffer t t)
  (add-hook 'before-save-hook #'lsp-organize-imports t t))
(add-hook 'go-mode-hook #'lsp-go-install-save-hooks)

;;Optional - provides fancier overlays.

(use-package lsp-ui
  :ensure t
  :commands lsp-ui-mode
  :init
)

;;Company mode is a standard completion package that works well with lsp-mode.
;;company-lsp integrates company mode completion with lsp-mode.
;;completion-at-point also works out of the box but doesn't support snippets.

(use-package company
  :ensure t
  :config
  (setq company-idle-delay 0)
  (setq company-minimum-prefix-length 1))

(use-package company-lsp
  :ensure t
  :commands company-lsp)

;;Optional - provides snippet support.

(use-package yasnippet
  :ensure t
  :commands yas-minor-mode
  :hook (go-mode . yas-minor-mode))

;;lsp-ui-doc-enable is false because I don't like the popover that shows up on the right
;;I'll change it if I want it back

(setq lsp-ui-doc-enable nil
      lsp-ui-peek-enable t
      lsp-ui-sideline-enable t
      lsp-ui-imenu-enable t
      lsp-ui-flycheck-enable t)
wqnecbli

wqnecbli1#

当go环境不正确时,我会发生这种情况。
作为诊断,尝试从 lsp-mode.el 注解掉 go*exec-paths,并设置适当的环境变量(如果您正在使用GOROOTGOPATHGO111MODULE),包括将$GOROOT/bin$GOPATH/bin添加到PATH
同样,如果仍然不工作,请在emacs中验证这些环境变量是否设置正确。
当然,对于Linux,你可以导出所有这些,然后从终端运行emacs进行测试。

bvjxkvbb

bvjxkvbb2#

我在制作语言服务器协议客户端时遇到过这个错误(所以与emacs无关,但通常与语言服务器协议有关)。通常,客户端会以“initialize”作为第一个命令:

{"id":1,"jsonrpc":"2.0","method":"initialize","params": {...}, ...}

然后在初始化后,如果您尝试一个命令,例如:

{"id":2,"jsonrpc":"2.0","method":"textDocument/prepareCallHierarchy","params": {...}, ... }

你会得到“no views in session”错误的回应。
为了解决这个问题-在**“initialize”命令的响应之后,我发送了“initialized”**命令。它基本上有空参数,但你需要发送它们来满足某些版本的要求,请参阅:https://github.com/golang/go/issues/57459

{"id":2,"jsonrpc":"2.0","method":"initialized","params": {}, ... }

这至少为我解决了问题。

请注意initialized命令可能需要一段时间才能完成,这取决于您的项目大小(对我来说,在得到响应之前几乎需要3分钟)。

相关问题