登录
首页 >  Golang >  Go问答

错误报告:LSP 模式出错,原因是"会话中缺少视图"

来源:stackoverflow

时间:2024-03-25 20:45:42 478浏览 收藏

在使用 go-mode 和 lsp-mode 时,用户遇到了“会话中缺少视图”错误。问题出在 go 环境配置不当,导致 lsp-mode 无法识别 go 环境。解决方案是注释掉 lsp-mode.el 中的 go* exec-paths,并设置适当的环境变量(GOROOT、GOPATH、GO111MODULE),包括将 $GOROOT/bin 和 $GOPATH/bin 添加到 PATH。验证环境变量设置是否正确后,问题应得到解决。

问题内容

我想要实现的目标

我想同时使用 go-mode/lsp-mode。我一开始就很难让 lsp-mode 执行,最后通过附加路径让它工作: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)

解决方案


当 go 环境不正确时,我就遇到过这种情况。

作为诊断,请尝试从 lsp-mode.el 中注释掉 go* exec-paths 并设置适当的环境变量(GOROOTGOPATHGO111MODULE 如果您'正在使用它),包括将 $GOROOT/bin$GOPATH/bin 添加到 PATH

此外,如果仍然无法正常工作,请在 emacs 中验证这些环境变量是否设置正确。

对于 Linux,当然您可以导出所有内容并从终端运行 emacs 进行测试。

今天关于《错误报告:LSP 模式出错,原因是"会话中缺少视图"》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>