blob: 4ff9994f9393dd4b1e647362dd25568ca24a48c7 [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`
Angus Dippenaar9474ca32022-11-08 16:03:14 +000037simultaneously by creating a `go.work` file using `go work init`, followed by
38`go work use MODULE_DIRECTORIES...` to add directories containing `go.mod` files to the
39workspace:
Robert Findleyabbbcaf2022-03-01 17:36:13 -050040
Angus Dippenaar9474ca32022-11-08 16:03:14 +000041```sh
Robert Findleyafc5fce2022-03-02 12:04:43 -050042cd $WORK
Robert Findleyabbbcaf2022-03-01 17:36:13 -050043go work init
Angus Dippenaar9474ca32022-11-08 16:03:14 +000044go work use ./tools/ ./tools/gopls/
Robert Findleyabbbcaf2022-03-01 17:36:13 -050045```
46
Robert Findleyafc5fce2022-03-02 12:04:43 -050047...followed by opening the `$WORK` directory in our editor.
Robert Findleyabbbcaf2022-03-01 17:36:13 -050048
Robert Findleybddb3722022-09-26 18:17:59 -040049#### DEPRECATED: Experimental workspace module (Go 1.17 and earlier)
50
51**This feature is deprecated and will be removed in future versions of gopls.
52Please see [issue #52897](https://go.dev/issue/52897) for additional
53information.**
Robert Findleyabbbcaf2022-03-01 17:36:13 -050054
55With earlier versions of Go, `gopls` can simulate multi-module workspaces by
Leonardo Henrique35f806b2022-08-12 22:44:49 +000056creating a synthetic module requiring the modules in the workspace root.
Robert Findleyabbbcaf2022-03-01 17:36:13 -050057See [the design document](https://github.com/golang/proposal/blob/master/design/37720-gopls-workspaces.md)
58for more information.
59
60This feature is experimental, and will eventually be removed once `go.work`
61files are accepted by all supported Go versions.
62
63You can enable this feature by configuring the
64[experimentalWorkspaceModule](settings.md#experimentalworkspacemodule-bool)
65setting.
66
67#### Multiple workspace folders
68
69If neither of the above solutions work, and your editor allows configuring the
70set of
71["workspace folders"](https://microsoft.github.io/language-server-protocol/specifications/specification-3-17/#workspaceFolder)
72used during your LSP session, you can still work on multiple modules by adding
73a workspace folder at each module root (the locations of `go.mod` files). This
74means that each module has its own scope, and features will not work across
75modules.
Rebecca Stambler1bdb73f2021-01-13 01:05:13 -050076
77In VS Code, you can create a workspace folder by setting up a
78[multi-root workspace](https://code.visualstudio.com/docs/editor/multi-root-workspaces).
Rebecca Stamblerbec622c2021-01-14 02:05:25 -050079View the [documentation for your editor plugin](../README.md#editor) to learn how to
Rebecca Stambler1bdb73f2021-01-13 01:05:13 -050080configure a workspace folder in your editor.
81
Rebecca Stambler1bdb73f2021-01-13 01:05:13 -050082### GOPATH mode
83
84When opening a directory within your GOPATH, the workspace scope will be just
85that directory.
86
87### At your own risk
88
89Some users or companies may have projects that encompass one `$GOPATH`. If you
90open your entire `$GOPATH` or `$GOPATH/src` folder, the workspace scope will be
91your entire `GOPATH`. If your GOPATH is large, `gopls` to be very slow to start
92because it will try to find all of the Go files in the directory you have
93opened. It will then load all of the files it has found.
94
95To work around this case, you can create a new `$GOPATH` that contains only the
96packages you want to work on.
97
98---
99
100If you have additional use cases that are not mentioned above, please
101[file a new issue](https://github.com/golang/go/issues/new).