lsp-mode for go erring out with "no views in session"

2.8k views Asked by At

What I'm trying to accomplish

I'd like to use go-mode/lsp-mode together. I struggle to get lsp-mode to even execute at first, finally got it to work by appending the paths :facepalm:.

The issue The issue now is that when lsp-mode starts up for the working golang directory, I receive this error:

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

I've searched around the internet for answers, but have yet to find anything that is relevant to my problem. I'm reaching out to the community for some guidance.

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)
2

There are 2 answers

0
draganstankovic On

I've had this error pops out to me while making a language server protocol client (so not relevant to emacs but generally to language server protocol). Commonly, a client will start with an "initialize" as the first command:

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

and then after initialization if you try a command, e.g.:

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

you will be getting the "no views in session" error in response.

To solve this - after "initialize" command's response I sent "initialized" command. It basically has empty params but you need to send them anyway to satisfy certain versions see: https://github.com/golang/go/issues/57459:

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

This solved it for me at least.

Please note: the initialized command can take a while to finish and it depends on your project size (for me it was almost 3 minutes before I got a response).

1
kstuart On

I've had this happen when the go environment is not correct.

As a diagnostic try commenting out the go* exec-paths from your lsp-mode.el and set appropriate environment variables (GOROOT, GOPATH and GO111MODULE if you're using it), including adding $GOROOT/bin and $GOPATH/bin to PATH.

Also if still not working verify within emacs those environment variables are set correctly.

For linux of course you can export all that and run emacs from the terminal to test.