| 1 | --- |
| 2 | title: "Gopls: Implementation" |
| 3 | --- |
| 4 | |
| 5 | Last major update: Jan 16 2024 |
| 6 | |
| 7 | This doc presents a high-level overview of the structure of gopls to |
| 8 | help new contributors find their way. It is not intended to be a |
| 9 | complete description of the implementation, nor even of any key |
| 10 | components; for that, the package documentation (linked below) and |
| 11 | other comments within the code are a better guide. |
| 12 | |
| 13 | The diagram below shows selected components of the gopls module and |
| 14 | their relationship to each other according to the Go import graph. |
| 15 | Tests and test infrastructure are not shown, nor are utility packages, |
| 16 | nor packages from the [x/tools] module. For brevity, packages are |
| 17 | referred to by their last segment, which is usually unambiguous. |
| 18 | |
| 19 | The height of each blob corresponds loosely to its technical depth. |
| 20 | Some blocks are wide and shallow, such as [protocol], which declares |
| 21 | Go types for the entire LSP protocol. Others are deep, such as [cache] |
| 22 | and [golang], as they contain a lot of dense logic and algorithms. |
| 23 | |
| 24 | <!-- Source: https://docs.google.com/drawings/d/1CK6YSLt7G3svRoZf7skJI-lxRol2VI90YOxHcYS0DP4 --> |
| 25 |  |
| 26 | |
| 27 | Starting from the bottom, we'll describe the various components. |
| 28 | |
| 29 | The lowest layer defines the request and response types of the |
| 30 | Language Server Protocol: |
| 31 | |
| 32 | - The [protocol] package defines the standard protocol; it is mostly |
| 33 | generated mechanically from the schema definition provided by |
| 34 | Microsoft. |
| 35 | The most important type is DocumentURI, which represents a `file:` |
| 36 | URL that identifies a client editor document. It also provides |
| 37 | `Mapper`, which maps between the different coordinate systems used |
| 38 | for source positions: UTF-8, UTF-16, and token.Pos. |
| 39 | |
| 40 | - The [command] package defines Gopls's non-standard commands, which |
| 41 | are all invoked through the `workspace/executeCommand` extension |
| 42 | mechanism. These commands are typically returned by the server as |
| 43 | continuations of Code Actions or Code Lenses; most clients do not |
| 44 | construct calls to them directly. |
| 45 | |
| 46 | The next layer defines a number of important and very widely used data structures: |
| 47 | |
| 48 | - The [file] package defines the primary abstractions of a client |
| 49 | file: its `Identity` (URI and content hash), and its `Handle` (which |
| 50 | additionally provides the version and content of a particular |
| 51 | snapshot of the file. |
| 52 | |
| 53 | - The [parsego] package defines `File`, the parsed form of a Go source |
| 54 | file, including its content, syntax tree, and coordinary mappings |
| 55 | (Mapper and token.File). The package performs various kinds of tree |
| 56 | repair to work around error-recovery shortcomings of the Go parser. |
| 57 | |
| 58 | - The [metadata] package defines `Package`, an abstraction of the |
| 59 | metadata of a Go package, similar to the output of `go list -json`. |
| 60 | Metadata is produced from [go/packages], which takes |
| 61 | care of invoking `go list`. (Users report that it works to some extent |
| 62 | with a GOPACKAGESDRIVER for Bazel, though we maintain no tests for this |
| 63 | scenario.) |
| 64 | |
| 65 | The package also provides `Graph`, the complete import graph for a |
| 66 | workspace; each graph node is a `Package`. |
| 67 | |
| 68 | The [settings] layer defines the data structure (effectively a large |
| 69 | tree) for gopls configuration options, along with its JSON encoding. |
| 70 | |
| 71 | The [cache] layer is the largest and most complex component of gopls. |
| 72 | It is concerned with state management, dependency analysis, and invalidation: |
| 73 | the `Session` of communication with the client; |
| 74 | the `Folder`s that the client has opened; |
| 75 | the `View` of a particular workspace tree with particular build |
| 76 | options; |
| 77 | the `Snapshot` of the state of all files in the workspace after a |
| 78 | particular edit operation; |
| 79 | the contents of all files, whether saved to disk (`DiskFile`) or |
| 80 | edited and unsaved (`Overlay`); |
| 81 | the `Cache` of in-memory memoized computations, |
| 82 | such as parsing go.mod files or build the symbol index; |
| 83 | and the `Package`, which holds the results of type checking a package |
| 84 | from Go syntax. |
| 85 | |
| 86 | The cache layer depends on various auxiliary packages, including: |
| 87 | |
| 88 | - The [filecache] package, which manages gopls' persistent, transactional, |
| 89 | file-based key/value store. |
| 90 | |
| 91 | - The [xrefs], [methodsets], and [typerefs] packages define algorithms |
| 92 | for constructing indexes of information derived from type-checking, |
| 93 | and for encoding and decoding these serializable indexes in the file |
| 94 | cache. |
| 95 | |
| 96 | Together these packages enable the fast restart, reduced memory |
| 97 | consumption, and synergy across processes that were delivered by the |
| 98 | v0.12 redesign and described in ["Scaling gopls for the growing Go |
| 99 | ecosystem"](https://go.dev/blog/gopls-scalability). |
| 100 | |
| 101 | The cache also defines gopls's [go/analysis] driver, which runs |
| 102 | modular analysis (similar to `go vet`) across the workspace. |
| 103 | Gopls also includes a number of analysis passes that are not part of vet. |
| 104 | |
| 105 | The next layer defines four packages, each for handling files in a |
| 106 | particular language: |
| 107 | [mod] for go.mod files; |
| 108 | [work] for go.work files; |
| 109 | [template] for files in `text/template` syntax; and |
| 110 | [golang], for files in Go itself. |
| 111 | This package, by far the largest, provides the main features of gopls: |
| 112 | navigation, analysis, and refactoring of Go code. |
| 113 | As most users imagine it, this package _is_ gopls. |
| 114 | |
| 115 | The [server] package defines the LSP service implementation, with one |
| 116 | handler method per LSP request type. Each handler switches on the type |
| 117 | of the file and dispatches to one of the four language-specific |
| 118 | packages. |
| 119 | |
| 120 | The [lsprpc] package connects the service interface to our [jsonrpc2](https://www.jsonrpc.org/specification) server. |
| 121 | |
| 122 | Bear in mind that the diagram is a dependency graph, a "static" |
| 123 | viewpoint of the program's structure. A more dynamic viewpoint would |
| 124 | order the packages based on the sequence in which they are encountered |
| 125 | during processing of a particular request; in such a view, the bottom |
| 126 | layer would represent the "wire" (protocol and command), the next |
| 127 | layer up would hold the RPC-related packages (lsprpc and server), and |
| 128 | features (e.g. golang, mod, work, template) would be at the top. |
| 129 | |
| 130 | <!-- |
| 131 | A dynamic view would be an interesting topic for another article. |
| 132 | This slide deck [requires Google network] |
| 133 | The Life of a (gopls) Query (Oct 2021) |
| 134 | https://docs.google.com/presentation/d/1c8XJaIldzii-F3YvEOPWHK_MQJ_o8ua5Bct1yDa3ZlU |
| 135 | provides useful (if somewhat out of date) information. |
| 136 | --> |
| 137 | |
| 138 | The [cmd] package defines the command-line interface of the `gopls` |
| 139 | command, around which gopls's main package is just a trivial wrapper. |
| 140 | It is usually run without arguments, causing it to start a server and |
| 141 | listen indefinitely. |
| 142 | It also provides a number of subcommands that start a server, make a |
| 143 | single request to it, and exit, providing traditional batch-command |
| 144 | access to server functionality. These subcommands are primarily |
| 145 | provided as a debugging aid; but see https://go.dev/issue/63693. |
| 146 | |
| 147 | [cache]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache |
| 148 | [cmd]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cmd |
| 149 | [command]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/protocol/command |
| 150 | [debug]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/debug |
| 151 | [file]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/file |
| 152 | [filecache]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/filecache |
| 153 | [go/analysis]: https://pkg.go.dev/golang.org/x/tools@master/go/analysis |
| 154 | [go/packages]: https://pkg.go.dev/golang.org/x/tools@master/go/packages |
| 155 | [gopls]: https://pkg.go.dev/golang.org/x/tools/gopls@master |
| 156 | [jsonrpc]: https://pkg.go.dev/golang.org/x/tools@master/internal/jsonrpc |
| 157 | [jsonrpc2]: https://pkg.go.dev/golang.org/x/tools@master/internal/jsonrpc2 |
| 158 | [lsprpc]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/lsprpc |
| 159 | [memoize]: https://github.com/golang/tools/tree/master/internal/memoize |
| 160 | [metadata]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/metadata |
| 161 | [methodsets]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/methodsets |
| 162 | [mod]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/mod |
| 163 | [parsego]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/parsego |
| 164 | [protocol]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/protocol |
| 165 | [server]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/server |
| 166 | [settings]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/settings |
| 167 | [golang]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/golang |
| 168 | [template]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/template |
| 169 | [typerefs]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/typerefs |
| 170 | [work]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/work |
| 171 | [x/tools]: https://github.com/golang/tools@master |
| 172 | [xrefs]: https://pkg.go.dev/golang.org/x/tools/gopls@master/internal/cache/xrefs |