| 1 | --- |
| 2 | title: "Gopls: Setting up your workspace" |
| 3 | --- |
| 4 | |
| 5 | In the language server protocol, a "workspace" consists of a folder along with |
| 6 | per-folder configuration. Some LSP clients such as VS Code allow configuring |
| 7 | workspaces explicitly, while others do so automatically by looking for special |
| 8 | files defining a workspace root (such as a `.git` directory or `go.mod` file). |
| 9 | |
| 10 | In order to function, gopls needs a defined scope in which language features |
| 11 | like references, rename, and implementation should operate. Put differently, |
| 12 | gopls needs to infer from the LSP workspace which `go build` invocations you |
| 13 | would use to build your workspace, including the working directory, |
| 14 | environment, and build flags. |
| 15 | |
| 16 | In the past, it could be tricky to set up your workspace so that gopls would |
| 17 | infer the correct build information. It required opening the correct directory |
| 18 | or using a `go.work` file to tell gopls about the modules you're working on, |
| 19 | and configuring the correct operating system and architecture in advance. |
| 20 | When this didn't work as expected, gopls would often fail in mysterious |
| 21 | ways--the dreaded "No packages found" error. |
| 22 | |
| 23 | Starting with gopls v0.15.0, workspace configuration is much simpler, and gopls |
| 24 | will typically work when you open a Go file anywhere in your workspace. If it |
| 25 | isn't working for you, or if you want to better understand how gopls models |
| 26 | your workspace, please read on. |
| 27 | |
| 28 | ## Workspace builds |
| 29 | |
| 30 | Starting with gopls v0.15.0, gopls will guess the builds you are working on |
| 31 | based on the set of open files. When you open a file in a workspace folder, |
| 32 | gopls checks whether the file is contained in a module, `go.work` workspace, or |
| 33 | GOPATH directory, and configures the build accordingly. Additionally, if you |
| 34 | open a file that is constrained to a different operating system or |
| 35 | architecture, for example opening `foo_windows.go` when working on Linux, gopls |
| 36 | will create a scope with `GOOS` and `GOARCH` set to a value that matches the |
| 37 | file. |
| 38 | |
| 39 | For example, suppose we had a repository with three modules: `moda`, `modb`, |
| 40 | and `modc`, and a `go.work` file using modules `moda` and `modb`. If we open |
| 41 | the files `moda/a.go`, `modb/b.go`, `moda/a_windows.go`, and `modc/c.go`, gopls |
| 42 | will automatically create three builds: |
| 43 | |
| 44 |  |
| 45 | |
| 46 | This allows gopls to _just work_ when you open a Go file, but it does come with |
| 47 | several caveats: |
| 48 | |
| 49 | - It causes gopls to do more work, since it is now tracking three builds |
| 50 | instead of one. However, the recent |
| 51 | [scalability redesign](https://go.dev/blog/gopls-scalability) |
| 52 | allows much of this work to be avoided through efficient caching. |
| 53 | - For operations invoked from a given file, such as "References" |
| 54 | or "Implementations", gopls executes the operation in |
| 55 | _the default build for that file_. For example, finding references to |
| 56 | a symbol `S` from `foo_linux.go` will return references from the Linux build, |
| 57 | and finding references to the same symbol `S` from `foo_windows.go` will |
| 58 | return references from the Windows build. Gopls searches the default build |
| 59 | for the file, but it doesn't search all the other possible builds (even |
| 60 | though that would be nice) because it is liable to be too expensive. |
| 61 | Issues [#65757](https://go.dev/issue/65757) and |
| 62 | [#65755](https://go.dev/issue/65755) propose improvements to this behavior. |
| 63 | - When selecting a `GOOS/GOARCH` combination to match a build-constrained file, |
| 64 | gopls will choose the first matching combination from |
| 65 | [this list](https://cs.opensource.google/go/x/tools/+/master:gopls/internal/cache/port.go;l=30;drc=f872b3d6f05822d290bc7bdd29db090fd9d89f5c). |
| 66 | In some cases, that may be surprising. |
| 67 | - When working in a `GOOS/GOARCH` constrained file that does not match your |
| 68 | default toolchain, `CGO_ENABLED=0` is implicitly set, since a C toolchain for |
| 69 | that target is unlikely to be available. This means that gopls will not |
| 70 | work in files including `import "C"`. Issue |
| 71 | [#65758](https://go.dev/issue/65758) may lead to improvements in this |
| 72 | behavior. |
| 73 | - Gopls is currently unable to guess build flags that include arbitrary |
| 74 | user-defined build constraints, such as a file with the build directive |
| 75 | `//go:build mytag`. Issue [#65089](https://go.dev/issue/65089) proposes |
| 76 | a heuristic by which gopls could handle this automatically. |
| 77 | |
| 78 | Please provide feedback on this behavior by upvoting or commenting the issues |
| 79 | mentioned above, or opening a [new issue](https://go.dev/issue/new) for other |
| 80 | improvements you'd like to see. |
| 81 | |
| 82 | ## When to use a `go.work` file for development |
| 83 | |
| 84 | Starting with Go 1.18, the `go` command has built-in support for multi-module |
| 85 | workspaces specified by [`go.work`](https://go.dev/ref/mod#workspaces) files. |
| 86 | Gopls will recognize these files if they are present in your workspace. |
| 87 | |
| 88 | Use a `go.work` file when: |
| 89 | |
| 90 | - you want to work on multiple modules simultaneously in a single logical |
| 91 | build, for example if you want changes to one module to be reflected in |
| 92 | another. |
| 93 | - you want to improve gopls' memory usage or performance by reducing the number |
| 94 | of builds it must track. |
| 95 | - you want gopls to know which modules you are working on in a multi-module |
| 96 | workspace, without opening any files. For example, it may be convenient to use |
| 97 | `workspace/symbol` queries before any files are open. |
| 98 | - you are using gopls v0.14.2 or earlier, and want to work on multiple |
| 99 | modules. |
| 100 | |
| 101 | For example, suppose this repo is checked out into the `$WORK/tools` directory, |
| 102 | and [`x/mod`](https://pkg.go.dev/golang.org/x/mod) is checked out into |
| 103 | `$WORK/mod`, and you are working on a new `x/mod` API for editing `go.mod` |
| 104 | files that you want to simultaneously integrate into gopls. |
| 105 | |
| 106 | You can work on both `golang.org/x/tools/gopls` and `golang.org/x/mod` |
| 107 | simultaneously by creating a `go.work` file: |
| 108 | |
| 109 | ```sh |
| 110 | cd $WORK |
| 111 | go work init |
| 112 | go work use tools/gopls mod |
| 113 | ``` |
| 114 | |
| 115 | then opening the `$WORK` directory in your editor. |
| 116 | |
| 117 | ## When to manually configure `GOOS`, `GOARCH`, or `-tags` |
| 118 | |
| 119 | As described in the first section, gopls v0.15.0 and later will try to |
| 120 | configure a new build scope automatically when you open a file that doesn't |
| 121 | match the system default operating system (`GOOS`) or architecture (`GOARCH`). |
| 122 | |
| 123 | However, per the caveats listed in that section, this automatic behavior comes |
| 124 | with limitations. Customize your gopls environment by setting `GOOS` or |
| 125 | `GOARCH` in your |
| 126 | [`"build.env"`](https://github.com/golang/tools/blob/master/gopls/doc/settings.md#env) |
| 127 | or `-tags=...` in your" |
| 128 | ["build.buildFlags"](https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags) |
| 129 | when: |
| 130 | |
| 131 | - You want to modify the default build environment. |
| 132 | - Gopls is not guessing the `GOOS/GOARCH` combination you want to use for |
| 133 | cross platform development. |
| 134 | - You need to work on a file that is constrained by a user-defined build tags, |
| 135 | such as the build directive `//go:build mytag`. |
| 136 | |
| 137 | ## GOPATH mode |
| 138 | |
| 139 | When opening a directory within a `GOPATH` directory, the workspace scope will |
| 140 | be just that directory and all directories contained within it. Note that |
| 141 | opening a large GOPATH directory can make gopls very slow to start. |