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