| 1 | --- |
| 2 | title: "Gopls: Using Emacs" |
| 3 | --- |
| 4 | |
| 5 | ## Installing `gopls` |
| 6 | |
| 7 | To use `gopls` with Emacs, you must first |
| 8 | [install the `gopls` executable](../index.md#installation) and ensure that the directory |
| 9 | containing the resulting binary (either `$(go env GOBIN)` or `$(go env |
| 10 | GOPATH)/bin`) is in your `PATH`. |
| 11 | |
| 12 | ## Choosing an Emacs LSP client |
| 13 | |
| 14 | To use `gopls` with Emacs, you will need to choose and install an Emacs LSP |
| 15 | client package. Two popular client packages are [LSP Mode] and [Eglot]. |
| 16 | |
| 17 | LSP Mode takes a batteries-included approach, with many integrations enabled |
| 18 | “out of the box” and several additional behaviors provided by `lsp-mode` itself. |
| 19 | |
| 20 | Eglot takes a minimally-intrusive approach, focusing on smooth integration with |
| 21 | other established packages. It provides a few of its own `eglot-` commands but |
| 22 | no additional keybindings by default. |
| 23 | |
| 24 | Once you have selected which client you want to use, install it per the packages |
| 25 | instructions: see [Eglot 1-2-3](https://github.com/joaotavora/eglot#1-2-3) or |
| 26 | [LSP Mode Installation](https://emacs-lsp.github.io/lsp-mode/page/installation/). |
| 27 | |
| 28 | ## Common configuration |
| 29 | |
| 30 | Both Eglot and LSP Mode can integrate with popular packages in the Emacs |
| 31 | ecosystem: |
| 32 | |
| 33 | * The built-in [`xref`] package provides cross-references. |
| 34 | * The built-in [Flymake] package provides an on-the-fly diagnostic overlay. |
| 35 | * [Company] mode displays code completion candidates (with a richer UI than |
| 36 | the built-in [`completion-at-point`]). |
| 37 | |
| 38 | Eglot provides documentation using the built-in [ElDoc] minor mode, while LSP |
| 39 | Mode by default provides documentation using its own [`lsp-ui`] mode. |
| 40 | |
| 41 | Eglot by default locates the project root using the [`project`] package. In LSP |
| 42 | Mode, this behavior can be configured using the `lsp-auto-guess-root` setting. |
| 43 | |
| 44 | ## Configuring LSP Mode |
| 45 | |
| 46 | ### Loading LSP Mode in `.emacs` |
| 47 | |
| 48 | ```elisp |
| 49 | (require 'lsp-mode) |
| 50 | (add-hook 'go-mode-hook #'lsp-deferred) |
| 51 | |
| 52 | ;; Set up before-save hooks to format buffer and add/delete imports. |
| 53 | ;; Make sure you don't have other gofmt/goimports hooks enabled. |
| 54 | (defun lsp-go-install-save-hooks () |
| 55 | (add-hook 'before-save-hook #'lsp-format-buffer t t) |
| 56 | (add-hook 'before-save-hook #'lsp-organize-imports t t)) |
| 57 | (add-hook 'go-mode-hook #'lsp-go-install-save-hooks) |
| 58 | ``` |
| 59 | |
| 60 | ### Configuring `gopls` via LSP Mode |
| 61 | |
| 62 | See [settings](../settings) for information about available gopls settings. |
| 63 | |
| 64 | Stable gopls settings have corresponding configuration variables in `lsp-mode`. |
| 65 | For example, `(setq lsp-gopls-use-placeholders nil)` will disable placeholders |
| 66 | in completion snippets. See [`lsp-go`] for a list of available variables. |
| 67 | |
| 68 | Experimental settings can be configured via `lsp-register-custom-settings`: |
| 69 | |
| 70 | ```lisp |
| 71 | (lsp-register-custom-settings |
| 72 | '(("gopls.completeUnimported" t t) |
| 73 | ("gopls.staticcheck" t t))) |
| 74 | ``` |
| 75 | |
| 76 | Note that after changing settings you must restart gopls using e.g. `M-x |
| 77 | lsp-restart-workspace`. |
| 78 | |
| 79 | ## Configuring Eglot |
| 80 | |
| 81 | ### Configuring `project` for Go modules in `.emacs` |
| 82 | |
| 83 | Eglot uses the built-in `project` package to identify the LSP workspace for a |
| 84 | newly-opened buffer. The `project` package does not natively know about `GOPATH` |
| 85 | or Go modules. Fortunately, you can give it a custom hook to tell it to look for |
| 86 | the nearest parent `go.mod` file (that is, the root of the Go module) as the |
| 87 | project root. |
| 88 | |
| 89 | ```elisp |
| 90 | (require 'project) |
| 91 | |
| 92 | (defun project-find-go-module (dir) |
| 93 | (when-let ((root (locate-dominating-file dir "go.mod"))) |
| 94 | (cons 'go-module root))) |
| 95 | |
| 96 | (cl-defmethod project-root ((project (head go-module))) |
| 97 | (cdr project)) |
| 98 | |
| 99 | (add-hook 'project-find-functions #'project-find-go-module) |
| 100 | ``` |
| 101 | |
| 102 | ### Loading Eglot in `.emacs` |
| 103 | |
| 104 | ```elisp |
| 105 | ;; Optional: load other packages before eglot to enable eglot integrations. |
| 106 | (require 'company) |
| 107 | (require 'yasnippet) |
| 108 | |
| 109 | (require 'go-mode) |
| 110 | (require 'eglot) |
| 111 | (add-hook 'go-mode-hook 'eglot-ensure) |
| 112 | |
| 113 | ;; Optional: install eglot-format-buffer as a save hook. |
| 114 | ;; The depth of -10 places this before eglot's willSave notification, |
| 115 | ;; so that notification reports the actual contents that will be saved. |
| 116 | (defun eglot-format-buffer-before-save () |
| 117 | (add-hook 'before-save-hook #'eglot-format-buffer -10 t)) |
| 118 | (add-hook 'go-mode-hook #'eglot-format-buffer-before-save) |
| 119 | ``` |
| 120 | |
| 121 | Use `M-x eglot-upgrade-eglot` to upgrade to the latest version of |
| 122 | Eglot. |
| 123 | |
| 124 | ### Configuring `gopls` via Eglot |
| 125 | |
| 126 | See [settings](../settings) for information about available gopls settings. |
| 127 | |
| 128 | LSP server settings are controlled by the `eglot-workspace-configuration` |
| 129 | variable, which can be set either globally in `.emacs` or in a `.dir-locals.el` file in the project root. |
| 130 | |
| 131 | `.emacs`: |
| 132 | ```elisp |
| 133 | (setq-default eglot-workspace-configuration |
| 134 | '((:gopls . |
| 135 | ((staticcheck . t) |
| 136 | (matcher . "CaseSensitive"))))) |
| 137 | ``` |
| 138 | |
| 139 | `.dir-locals.el`: |
| 140 | ```elisp |
| 141 | ((nil (eglot-workspace-configuration . ((gopls . ((staticcheck . t) |
| 142 | (matcher . "CaseSensitive"))))))) |
| 143 | ``` |
| 144 | |
| 145 | ### Organizing imports with Eglot |
| 146 | |
| 147 | `gopls` provides the import-organizing functionality of `goimports` as an LSP |
| 148 | code action, which you can invoke as needed by running `M-x eglot-code-actions` |
| 149 | (or a key of your choice bound to the `eglot-code-actions` function) and |
| 150 | selecting `Organize Imports` at the prompt. |
| 151 | |
| 152 | To automatically organize imports before saving, add a hook: |
| 153 | |
| 154 | ```elisp |
| 155 | (add-hook 'before-save-hook |
| 156 | (lambda () |
| 157 | (call-interactively 'eglot-code-action-organize-imports)) |
| 158 | nil t) |
| 159 | ``` |
| 160 | |
| 161 | ## Troubleshooting |
| 162 | |
| 163 | Common errors: |
| 164 | |
| 165 | * When prompted by Emacs for your project folder, if you are using modules you |
| 166 | must select the module's root folder (i.e. the directory with the "go.mod"). |
| 167 | If you are using GOPATH, select your $GOPATH as your folder. |
| 168 | * Emacs must have your environment set properly (PATH, GOPATH, etc). You can |
| 169 | run `M-x getenv <RET> PATH <RET>` to see if your PATH is set in Emacs. If |
| 170 | not, you can try starting Emacs from your terminal, using [this |
| 171 | package][exec-path-from-shell], or moving your shell config from `.bashrc` |
| 172 | into `.profile` and logging out and back in. |
| 173 | * Make sure only one LSP client mode is installed. (For example, if using |
| 174 | `lsp-mode`, ensure that you are not _also_ enabling `eglot`.) |
| 175 | * Look for errors in the `*lsp-log*` buffer or run `M-x eglot-events-buffer`. |
| 176 | * Ask for help in the `#emacs` channel on the [Gophers slack]. |
| 177 | |
| 178 | [LSP Mode]: https://emacs-lsp.github.io/lsp-mode/ |
| 179 | [Eglot]: https://github.com/joaotavora/eglot/blob/master/README.md |
| 180 | [`xref`]: https://www.gnu.org/software/emacs/manual/html_node/emacs/Xref.html |
| 181 | [Flymake]: https://www.gnu.org/software/emacs/manual/html_node/flymake/Using-Flymake.html#Using-Flymake |
| 182 | [Company]: https://company-mode.github.io/ |
| 183 | [`completion-at-point`]: https://www.gnu.org/software/emacs/manual/html_node/elisp/Completion-in-Buffers.html |
| 184 | [ElDoc]: https://elpa.gnu.org/packages/eldoc.html |
| 185 | [`lsp-ui`]: https://emacs-lsp.github.io/lsp-ui/ |
| 186 | [`lsp-go`]: https://github.com/emacs-lsp/lsp-mode/blob/master/clients/lsp-go.el |
| 187 | [`use-package`]: https://github.com/jwiegley/use-package |
| 188 | [`exec-path-from-shell`]: https://github.com/purcell/exec-path-from-shell |
| 189 | [settings]: settings.md |
| 190 | [Gophers slack]: https://invite.slack.golangbridge.org/ |