| 1 | --- |
| 2 | title: "Gopls: Contributing" |
| 3 | --- |
| 4 | |
| 5 | Contributions are welcome! However, development is fast moving, |
| 6 | and we are limited in our capacity to review contributions. |
| 7 | So, before sending a CL, please please please: |
| 8 | |
| 9 | - **file an issue** for a bug or feature request, if one does not |
| 10 | exist already. This allows us to identify redundant requests, or to |
| 11 | merge a specific problem into a more general one, and to assess the |
| 12 | importance of the problem. |
| 13 | - **claim it for yourself** by commenting on the issue or, if you are |
| 14 | able, by assigning the issue to yourself. This helps us avoid two |
| 15 | people working on the same problem. |
| 16 | - **propose an implementation plan** in the issue tracker for CLs of |
| 17 | any complexity. It is much more efficient to discuss the plan at a |
| 18 | high level before we start getting bogged down in the details of |
| 19 | a code review. |
| 20 | |
| 21 | When you send a CL, it should include: |
| 22 | |
| 23 | - a **CL description** that summarizes the change, |
| 24 | motivates why it is necessary, |
| 25 | explains it at a high level, |
| 26 | contrasts it with more obvious or simpler approaches, and |
| 27 | links to relevant issues; |
| 28 | - **tests** (integration tests or marker tests); |
| 29 | - **documentation**, for new or modified features; and |
| 30 | - **release notes**, for new features or significant changes. |
| 31 | |
| 32 | During code review, please address all reviewer comments. |
| 33 | Some comments result in straightforward code changes; |
| 34 | others demand a more complex response. |
| 35 | When a reviewer asks a question, the best response is |
| 36 | often not to respond to it directly, but to change the |
| 37 | code to avoid raising the question, |
| 38 | for example by making the code self-explanatory. |
| 39 | It's fine to disagree with a comment, |
| 40 | point out a reviewer's mistake, |
| 41 | or offer to address a comment in a follow-up change, |
| 42 | leaving a `TODO` comment in the current CL. |
| 43 | But please don't dismiss or quietly ignore a comment without action, |
| 44 | as it may lead reviewers to repeat themselves, |
| 45 | or to serious problems being neglected. |
| 46 | |
| 47 | For more detail, see the Go project's |
| 48 | [contribution guidelines](https://golang.org/doc/contribute.html). |
| 49 | |
| 50 | |
| 51 | ## Finding issues |
| 52 | |
| 53 | All `gopls` issues are labeled as such (see the [`gopls` label][issue-gopls]). |
| 54 | Issues that are suitable for contributors are additionally tagged with the |
| 55 | [`help-wanted` label][issue-wanted]. |
| 56 | |
| 57 | Before you begin working on an issue, please leave a comment that you are |
| 58 | claiming it. |
| 59 | |
| 60 | ## Getting started |
| 61 | |
| 62 | [](https://pkg.go.dev/golang.org/x/tools/gopls/internal) |
| 63 | |
| 64 | Most of the `gopls` logic is in the `golang.org/x/tools/gopls/internal` directory. |
| 65 | See [design/implementation.md](./design/implementation.md) for an overview of the code organization. |
| 66 | |
| 67 | ### Repository structure |
| 68 | |
| 69 | This repository provides two modules: |
| 70 | - The root directory defines the `golang.org/x/tools` module, which provides importable packages. |
| 71 | - The `gopls` subdirectory defines the `golang.org/x/tools/gopls` module, which provides the gopls application as its main package. |
| 72 | |
| 73 | The gopls/go.mod file contains a `replace` directive pointing to the parent directory to ensure that gopls uses the version of x/tools at the exact same git commit. |
| 74 | |
| 75 | We recommend creating a go.work file such as this in the root directory: |
| 76 | |
| 77 | ```go |
| 78 | go 1.25 |
| 79 | |
| 80 | use . |
| 81 | use ./gopls |
| 82 | ``` |
| 83 | |
| 84 | so that the go command will allow you to specify gopls packages even when working outside the gopls directory. For example: |
| 85 | |
| 86 | ``` |
| 87 | tools$ go test -short ./gopls/... |
| 88 | ``` |
| 89 | |
| 90 | For more details on how to use workspaces, see the [Go workspace documentation](https://go.dev/doc/tutorial/workspaces) or run `go help work`. |
| 91 | |
| 92 | With this setup, you can run tests for all modules in the workspace from the root directory using: |
| 93 | |
| 94 | ```bash |
| 95 | go test work |
| 96 | ``` |
| 97 | |
| 98 | This will run tests for both `golang.org/x/tools` and `golang.org/x/tools/gopls`. |
| 99 | |
| 100 | ## Build |
| 101 | |
| 102 | To build a version of `gopls` with your changes applied: |
| 103 | |
| 104 | ```bash |
| 105 | cd /path/to/tools/gopls |
| 106 | go install |
| 107 | ``` |
| 108 | |
| 109 | To confirm that you are testing with the correct `gopls` version, check that |
| 110 | your `gopls` version looks like this: |
| 111 | |
| 112 | ```bash |
| 113 | $ gopls version |
| 114 | golang.org/x/tools/gopls master |
| 115 | golang.org/x/tools/gopls@(devel) |
| 116 | ``` |
| 117 | |
| 118 | ## Getting help |
| 119 | |
| 120 | The best way to contact the gopls team directly is via the |
| 121 | [#gopls-dev](https://app.slack.com/client/T029RQSE6/CRWSN9NCD) channel on the |
| 122 | gophers slack. Please feel free to ask any questions about your contribution or |
| 123 | about contributing in general. |
| 124 | |
| 125 | |
| 126 | ## Error handling |
| 127 | |
| 128 | It is important for the user experience that, whenever practical, |
| 129 | minor logic errors in a particular feature don't cause the server to |
| 130 | crash. |
| 131 | |
| 132 | The representation of a Go program is complex. The import graph of |
| 133 | package metadata, the syntax trees of parsed files, and their |
| 134 | associated type information together form a huge API surface area. |
| 135 | Even when the input is valid, there are many edge cases to consider, |
| 136 | and this grows by an order of magnitude when you consider missing |
| 137 | imports, parse errors, and type errors. |
| 138 | |
| 139 | What should you do when your logic must handle an error that you |
| 140 | believe "can't happen"? |
| 141 | |
| 142 | - If it's possible to return an error, then use the `bug.Errorf` |
| 143 | function to return an error to the user, but also record the bug in |
| 144 | gopls' cache so that it is less likely to be ignored. |
| 145 | |
| 146 | - If it's safe to proceed, you can call `bug.Reportf` to record the |
| 147 | error and continue as normal. |
| 148 | |
| 149 | - If there's no way to proceed, call `bug.Fatalf` to record the error |
| 150 | and then stop the program with `log.Fatalf`. You can also use |
| 151 | `bug.Panicf` if there's a chance that a recover handler might save |
| 152 | the situation. |
| 153 | |
| 154 | - Only if you can prove locally that an error is impossible should you |
| 155 | call `log.Fatal`. If the error may happen for some input, however |
| 156 | unlikely, then you should use one of the approaches above. Also, if |
| 157 | the proof of safety depends on invariants broadly distributed across |
| 158 | the code base, then you should instead use `bug.Panicf`. |
| 159 | |
| 160 | Note also that panicking is preferable to `log.Fatal` because it |
| 161 | allows VS Code's crash reporting to recognize and capture the stack. |
| 162 | |
| 163 | ## Testing |
| 164 | |
| 165 | The normal command you should use to run the tests after a change is: |
| 166 | |
| 167 | ```bash |
| 168 | gopls$ go test -short ./... |
| 169 | ``` |
| 170 | |
| 171 | (The `-short` flag skips some slow-running ones. The trybot builders |
| 172 | run the complete set, on a wide range of platforms.) |
| 173 | |
| 174 | Gopls tests are a mix of two kinds. |
| 175 | |
| 176 | - [Marker tests](https://golang.org/x/tools/gopls/internal/test/marker) express each test scenario |
| 177 | in a standalone text file that contains the target .go, go.mod, and |
| 178 | go.work files, in which special annotations embedded in comments |
| 179 | drive the test. These tests are generally easy to write and fast |
| 180 | to iterate, but have limitations on what they can express. |
| 181 | |
| 182 | - [Integration tests](https://golang.org/x/tools/gopls/internal/test/integration) are regular Go |
| 183 | `func Test(*testing.T)` functions that make a series of calls to an |
| 184 | API for a fake LSP-enabled client editor. The API allows you to open |
| 185 | and edit a file, navigate to a definition, invoke other LSP |
| 186 | operations, and assert properties about the state. |
| 187 | |
| 188 | Due to the asynchronous nature of the LSP, integration tests make |
| 189 | assertions about states that the editor must achieve eventually, |
| 190 | even when the program goes wrong quickly, it may take a while before |
| 191 | the error is reported as a failure to achieve the desired state |
| 192 | within several minutes. We recommend that you set |
| 193 | `GOPLS_INTEGRATION_TEST_TIMEOUT=10s` to reduce the timeout for |
| 194 | integration tests when debugging. |
| 195 | |
| 196 | When they fail, the integration tests print the log of the LSP |
| 197 | session between client and server. Though verbose, they are very |
| 198 | helpful for debugging once you know how to read them. |
| 199 | |
| 200 | Don't hesitate to [reach out](#getting-help) to the gopls team if you |
| 201 | need help. |
| 202 | |
| 203 | ### CI |
| 204 | |
| 205 | When you mail your CL and you or a fellow contributor assigns the |
| 206 | `Run-TryBot=1` label in Gerrit, the |
| 207 | [TryBots](https://golang.org/doc/contribute.html#trybots) will run tests in |
| 208 | both the `golang.org/x/tools` and `golang.org/x/tools/gopls` modules, as |
| 209 | described above. |
| 210 | |
| 211 | Furthermore, an additional "gopls-CI" pass will be run by _Kokoro_, which is a |
| 212 | Jenkins-like Google infrastructure for running Dockerized tests. This allows us |
| 213 | to run gopls tests in various environments that would be difficult to add to |
| 214 | the TryBots. Notably, Kokoro runs tests on |
| 215 | [older Go versions](index.md#supported-go-versions) that are no longer supported |
| 216 | by the TryBots. Per that policy, support for these older Go versions is |
| 217 | best-effort, and test failures may be skipped rather than fixed. |
| 218 | |
| 219 | Kokoro runs are triggered by the `Run-TryBot=1` label, just like TryBots, but |
| 220 | unlike TryBots they do not automatically re-run if the "gopls-CI" result is |
| 221 | removed in Gerrit. To force a re-run of the Kokoro CI on a CL containing the |
| 222 | `Run-TryBot=1` label, you can reply in Gerrit with the comment "kokoro rerun". |
| 223 | |
| 224 | ## Debugging |
| 225 | |
| 226 | The easiest way to debug your change is to run a single `gopls` test with a |
| 227 | debugger. |
| 228 | |
| 229 | See also [Troubleshooting](troubleshooting.md#troubleshooting). |
| 230 | |
| 231 | ### Debug Server |
| 232 | |
| 233 | Gopls includes a built-in debug server that exposes metrics, traces, and |
| 234 | profiling information. Start it by passing the `-debug` flag, e.q. `gopls serve -debug=localhost:6060`. When using `:0`, gopls logs the assigned address to stderr. |
| 235 | |
| 236 | You can also start the server in a running gopls process by executing a StartDebugging LSP command. In VS Code this is bound to the "Go: Start language server maintainer's interface" command. |
| 237 | |
| 238 | **Key endpoints:** |
| 239 | |
| 240 | | Endpoint | Description | |
| 241 | |----------|-------------| |
| 242 | | `/` | Overview of caches, sessions and clients | |
| 243 | | `/rpc/` | RPC statistics with latency and status codes | |
| 244 | | `/trace/` | Recent spans and operation traces | |
| 245 | | `/metrics/` | Prometheus-compatible metrics | |
| 246 | | `/memory` | Memory usage statistics | |
| 247 | | `/debug/pprof/` | Go pprof profiling | |
| 248 | |
| 249 | ### OpenTelemetry |
| 250 | |
| 251 | Gopls supports periodical export of traces and metrics by POSTing JSON messages to an OpenTelemetry collector process such as Jaeger/Grafana. |
| 252 | Use the `-otel` flag to specify the collector endpoint to enable exporting: |
| 253 | |
| 254 | ```bash |
| 255 | gopls serve -otel=http://localhost:4318 |
| 256 | ``` |
| 257 | |
| 258 | The data are discarded if no collector is listening at that address. |
| 259 | |
| 260 | For example, to view traces locally with Jaeger: |
| 261 | ```sh |
| 262 | $ podman run --rm --name jaeger \ |
| 263 | -p 16686:16686 \ |
| 264 | -p 4318:4318 \ |
| 265 | jaegertracing/all-in-one:1.76.0 |
| 266 | ``` |
| 267 | |
| 268 | Then open http://localhost:16686 to view traces. |
| 269 | |
| 270 | |
| 271 | [issue-gopls]: https://github.com/golang/go/issues?utf8=%E2%9C%93&q=is%3Aissue+is%3Aopen+label%3Agopls "gopls issues" |
| 272 | [issue-wanted]: https://github.com/golang/go/issues?utf8=✓&q=is%3Aissue+is%3Aopen+label%3Agopls+label%3A"help+wanted" "help wanted" |
| 273 | |
| 274 | ## Documentation |
| 275 | |
| 276 | Each CL that adds or changes a feature should include, in addition to |
| 277 | a test that exercises the new behavior: |
| 278 | |
| 279 | - a **release note** that briefly explains the change, and |
| 280 | - **comprehensive documentation** in the [index of features](features/). |
| 281 | |
| 282 | The release note should go in the file named for the forthcoming |
| 283 | release, for example [release/v0.16.0.md](release/v0.16.0.md). (Create |
| 284 | the file if your feature is the first to be added after a release.) |
| 285 | |
| 286 | ## Design documentation |
| 287 | |
| 288 | * [Integrating `gopls` with an editor](design/integrating.md) |
| 289 | * [Design requirements and decisions](design/design.md) |
| 290 | * [Implementation overview](design/implementation.md) |