Rebecca Stambler | 1bdb73f | 2021-01-13 01:05:13 -0500 | [diff] [blame] | 1 | # Setting up your workspace |
| 2 | |
| 3 | `gopls` supports both Go module and GOPATH modes. However, it needs a defined |
| 4 | scope in which language features like references, rename, and implementation |
| 5 | should operate. |
| 6 | |
| 7 | The following options are available for configuring this scope: |
| 8 | |
| 9 | ## Module mode |
| 10 | |
| 11 | ### One module |
| 12 | |
| 13 | If you are working with a single module, you can open the module root (the |
| 14 | directory containing the `go.mod` file), a subdirectory within the module, |
| 15 | or a parent directory containing the module. |
| 16 | |
| 17 | **Note**: If you open a parent directory containing a module, it must **only** |
| 18 | contain that single module. Otherwise, you are working with multiple modules. |
| 19 | |
| 20 | ### Multiple modules |
| 21 | |
Robert Findley | abbbcaf | 2022-03-01 17:36:13 -0500 | [diff] [blame] | 22 | Gopls has several alternatives for working on multiple modules simultaneously, |
| 23 | described below. Starting with Go 1.18, Go workspaces are the preferred solution. |
| 24 | |
| 25 | #### Go workspaces (Go 1.18+) |
| 26 | |
| 27 | Starting with Go 1.18, the `go` command has native support for multi-module |
| 28 | workspaces, via [`go.work`](https://go.dev/ref/mod#workspaces) files. These |
| 29 | files are recognized by gopls starting with `gopls@v0.8.0`. |
| 30 | |
| 31 | The easiest way to work on multiple modules in Go 1.18 and later is therefore |
| 32 | to create a `go.work` file containing the modules you wish to work on, and set |
| 33 | your workspace root to the directory containing the `go.work` file. |
| 34 | |
Robert Findley | afc5fce | 2022-03-02 12:04:43 -0500 | [diff] [blame] | 35 | For example, suppose this repo is checked out into the `$WORK/tools` directory. |
| 36 | We can work on both `golang.org/x/tools` and `golang.org/x/tools/gopls` |
| 37 | simultaneously by creating a `go.work` file: |
Robert Findley | abbbcaf | 2022-03-01 17:36:13 -0500 | [diff] [blame] | 38 | |
| 39 | ``` |
Robert Findley | afc5fce | 2022-03-02 12:04:43 -0500 | [diff] [blame] | 40 | cd $WORK |
Robert Findley | abbbcaf | 2022-03-01 17:36:13 -0500 | [diff] [blame] | 41 | go work init |
Robert Findley | afc5fce | 2022-03-02 12:04:43 -0500 | [diff] [blame] | 42 | go work use tools tools/gopls |
Robert Findley | abbbcaf | 2022-03-01 17:36:13 -0500 | [diff] [blame] | 43 | ``` |
| 44 | |
Robert Findley | afc5fce | 2022-03-02 12:04:43 -0500 | [diff] [blame] | 45 | ...followed by opening the `$WORK` directory in our editor. |
Robert Findley | abbbcaf | 2022-03-01 17:36:13 -0500 | [diff] [blame] | 46 | |
| 47 | #### Experimental workspace module (Go 1.17 and earlier) |
| 48 | |
| 49 | With earlier versions of Go, `gopls` can simulate multi-module workspaces by |
| 50 | creating a synthetic module requiring the the modules in the workspace root. |
| 51 | See [the design document](https://github.com/golang/proposal/blob/master/design/37720-gopls-workspaces.md) |
| 52 | for more information. |
| 53 | |
| 54 | This feature is experimental, and will eventually be removed once `go.work` |
| 55 | files are accepted by all supported Go versions. |
| 56 | |
| 57 | You can enable this feature by configuring the |
| 58 | [experimentalWorkspaceModule](settings.md#experimentalworkspacemodule-bool) |
| 59 | setting. |
| 60 | |
| 61 | #### Multiple workspace folders |
| 62 | |
| 63 | If neither of the above solutions work, and your editor allows configuring the |
| 64 | set of |
| 65 | ["workspace folders"](https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#workspaceFolder) |
| 66 | used during your LSP session, you can still work on multiple modules by adding |
| 67 | a workspace folder at each module root (the locations of `go.mod` files). This |
| 68 | means that each module has its own scope, and features will not work across |
| 69 | modules. |
Rebecca Stambler | 1bdb73f | 2021-01-13 01:05:13 -0500 | [diff] [blame] | 70 | |
| 71 | In VS Code, you can create a workspace folder by setting up a |
| 72 | [multi-root workspace](https://code.visualstudio.com/docs/editor/multi-root-workspaces). |
Rebecca Stambler | bec622c | 2021-01-14 02:05:25 -0500 | [diff] [blame] | 73 | View the [documentation for your editor plugin](../README.md#editor) to learn how to |
Rebecca Stambler | 1bdb73f | 2021-01-13 01:05:13 -0500 | [diff] [blame] | 74 | configure a workspace folder in your editor. |
| 75 | |
Rebecca Stambler | 1bdb73f | 2021-01-13 01:05:13 -0500 | [diff] [blame] | 76 | ### GOPATH mode |
| 77 | |
| 78 | When opening a directory within your GOPATH, the workspace scope will be just |
| 79 | that directory. |
| 80 | |
| 81 | ### At your own risk |
| 82 | |
| 83 | Some users or companies may have projects that encompass one `$GOPATH`. If you |
| 84 | open your entire `$GOPATH` or `$GOPATH/src` folder, the workspace scope will be |
| 85 | your entire `GOPATH`. If your GOPATH is large, `gopls` to be very slow to start |
| 86 | because it will try to find all of the Go files in the directory you have |
| 87 | opened. It will then load all of the files it has found. |
| 88 | |
| 89 | To work around this case, you can create a new `$GOPATH` that contains only the |
| 90 | packages you want to work on. |
| 91 | |
| 92 | --- |
| 93 | |
| 94 | If you have additional use cases that are not mentioned above, please |
| 95 | [file a new issue](https://github.com/golang/go/issues/new). |