11# Setting up your workspace
22
3- ** In general, ` gopls ` should work when you open a Go file contained in your
4- workspace folder** . If it isn't working for you, or if you want to better
5- understand how gopls models your workspace, please read on.
3+ In the language server protocol, a "workspace" consists of a folder along with
4+ per-folder configuration. Some LSP clients such as VS Code allow configuring
5+ workspaces explicitly, while others do so automatically by looking for special
6+ files defining a workspace root (such as a ` .git ` directory or ` go.mod ` file).
7+
8+ In order to function, gopls needs a defined scope in which language features
9+ like references, rename, and implementation should operate. Put differently,
10+ gopls needs to infer from the LSP workspace which ` go build ` invocations you
11+ would use to build your workspace, including the working directory,
12+ environment, and build flags.
13+
14+ In the past, it could be tricky to set up your workspace so that gopls would
15+ infer the correct build information. It required opening the correct directory
16+ or using a ` go.work ` file to tell gopls about the modules you're working on,
17+ and configuring the correct operating system and architecture in advance.
18+ When this didn't work as expected, gopls would often fail in mysterious
19+ ways--the dreaded "No packages found" error.
20+
21+ Starting with gopls v0.15.0, workspace configuration is much simpler, and gopls
22+ will typically work when you open a Go file anywhere in your workspace. If it
23+ isn't working for you, or if you want to better understand how gopls models
24+ your workspace, please read on.
625
726## Workspace builds
827
9- ` gopls ` supports both Go module and GOPATH modes. However, it needs a defined
10- scope in which language features like references, rename, and implementation
11- should operate. Put differently, gopls needs to infer which ` go build `
12- invocations you would use to build your workspace, including the working
13- directory, environment, and build flags.
14-
15- Starting with
` [email protected] ` , gopls will try to guess the builds you are
16- working on based on the set of open files. When you open a file in a workspace
17- folder, gopls will check whether the file is contained in a module, ` go.work `
18- workspace, or GOPATH directory, and configure the build accordingly.
19- Additionally, if you open a file that is constrained to a different operating
20- system or architecture, for example opening ` foo_windows.go ` when working on
21- Linux, gopls will create a scope with ` GOOS ` and ` GOARCH ` set to a value that
22- matches the file.
28+ Starting with gopls v0.15.0, gopls will guess the builds you are working on
29+ based on the set of open files. When you open a file in a workspace folder,
30+ gopls checks whether the file is contained in a module, ` go.work ` workspace, or
31+ GOPATH directory, and configures the build accordingly. Additionally, if you
32+ open a file that is constrained to a different operating system or
33+ architecture, for example opening ` foo_windows.go ` when working on Linux, gopls
34+ will create a scope with ` GOOS ` and ` GOARCH ` set to a value that matches the
35+ file.
2336
2437For example, suppose we had a repository with three modules: ` moda ` , ` modb ` ,
2538and ` modc ` , and a ` go.work ` file using modules ` moda ` and ` modb ` . If we open
@@ -28,69 +41,65 @@ will automatically create three builds:
2841
2942![ Zero Config gopls] ( zeroconfig.png )
3043
31- This allows ` gopls ` to _ just work_ when you open a Go file, but it does come with
44+ This allows gopls to _ just work_ when you open a Go file, but it does come with
3245several caveats:
3346
34- - This causes gopls to do more work, since it is now tracking three builds
47+ - It causes gopls to do more work, since it is now tracking three builds
3548 instead of one. However, the recent
3649 [ scalability redesign] ( https://go.dev/blog/gopls-scalability )
3750 allows much of this work to be avoided through efficient caching.
38- - In some cases this may cause gopls to do more work, since gopls is now
39- tracking three builds instead of one. However, the recent
40- [ scalability redesign] ( https://go.dev/blog/gopls-scalability ) allows us
41- to avoid most of this work by efficient caching.
42- - For operations originating from a given file, including finding references
43- and implementations, gopls executes the operation in
51+ - For operations invoked from a given file, such as "References"
52+ or "Implementations", gopls executes the operation in
4453 _ the default build for that file_ . For example, finding references to
4554 a symbol ` S ` from ` foo_linux.go ` will return references from the Linux build,
4655 and finding references to the same symbol ` S ` from ` foo_windows.go ` will
47- return references from the Windows build. This is done for performance
48- reasons, as in the common case one build is sufficient, but may lead to
49- surprising results. Issues [ #65757 ] ( https://go.dev/issue/65757 ) and
56+ return references from the Windows build. Gopls searches the default build
57+ for the file, but it doesn't search all the other possible builds (even
58+ though that would be nice) because it is liable to be too expensive.
59+ Issues [ #65757 ] ( https://go.dev/issue/65757 ) and
5060 [ #65755 ] ( https://go.dev/issue/65755 ) propose improvements to this behavior.
5161- When selecting a ` GOOS/GOARCH ` combination to match a build-constrained file,
52- ` gopls ` will choose the first matching combination from
62+ gopls will choose the first matching combination from
5363 [ this list] ( https://cs.opensource.google/go/x/tools/+/master:gopls/internal/cache/port.go;l=30;drc=f872b3d6f05822d290bc7bdd29db090fd9d89f5c ) .
5464 In some cases, that may be surprising.
5565- When working in a ` GOOS/GOARCH ` constrained file that does not match your
56- default toolchain, ` CGO_ENABLED=0 ` is implicitly set. This means that ` gopls `
57- will not work in files including ` import "C" ` . Issue
66+ default toolchain, ` CGO_ENABLED=0 ` is implicitly set, since a C toolchain for
67+ that target is unlikely to be available. This means that gopls will not
68+ work in files including ` import "C" ` . Issue
5869 [ #65758 ] ( https://go.dev/issue/65758 ) may lead to improvements in this
5970 behavior.
60- - ` gopls ` is not able to guess build flags that include arbitrary user-defined
61- build constraints. For example, if you are trying to work on a file that is
62- constrained by the build directive ` //go:build special ` , gopls will not guess
63- that it needs to create a build with ` "buildFlags": ["-tags=special"] ` . Issue
64- [ #65089 ] ( https://go.dev/issue/65089 ) proposes a heuristic by which gopls
65- could handle this automatically.
71+ - Gopls is currently unable to guess build flags that include arbitrary
72+ user-defined build constraints, such as a file with the build directive
73+ ` //go:build mytag ` . Issue [ #65089 ] ( https://go.dev/issue/65089 ) proposes
74+ a heuristic by which gopls could handle this automatically.
6675
67- We hope that you provide feedback on this behavior by upvoting or commenting
68- the issues mentioned above, or opening a [ new issue] ( https://go.dev/issue/new )
69- for other improvements you'd like to see.
76+ Please provide feedback on this behavior by upvoting or commenting the issues
77+ mentioned above, or opening a [ new issue] ( https://go.dev/issue/new ) for other
78+ improvements you'd like to see.
7079
7180## When to use a ` go.work ` file for development
7281
73- Starting with Go 1.18, the ` go ` command has native support for multi-module
74- workspaces, via [ ` go.work ` ] ( https://go.dev/ref/mod#workspaces ) files. ` gopls `
75- will recognize these files if they are present in your workspace.
82+ Starting with Go 1.18, the ` go ` command has built-in support for multi-module
83+ workspaces specified by [ ` go.work ` ] ( https://go.dev/ref/mod#workspaces ) files.
84+ Gopls will recognize these files if they are present in your workspace.
7685
7786Use a ` go.work ` file when:
7887
79- - You want to work on multiple modules simultaneously in a single logical
88+ - you want to work on multiple modules simultaneously in a single logical
8089 build, for example if you want changes to one module to be reflected in
8190 another.
82- - You want to improve ` gopls' ` memory usage or performance by reducing the number
91+ - you want to improve gopls' memory usage or performance by reducing the number
8392 of builds it must track.
84- - You want ` gopls ` to know which modules you are working on in a multi-module
85- workspace, without opening any files. For example, if you want to use
93+ - you want gopls to know which modules you are working on in a multi-module
94+ workspace, without opening any files. For example, it may be convenient to use
8695 ` workspace/symbol ` queries before any files are open.
87- - You are using ` gopls@ v0.14.2 ` or earlier, and want to work on multiple
96+ - you are using gopls v0.14.2 or earlier, and want to work on multiple
8897 modules.
8998
9099For example, suppose this repo is checked out into the ` $WORK/tools ` directory,
91100and [ ` x/mod ` ] ( https://pkg.go.dev/golang.org/x/mod ) is checked out into
92101` $WORK/mod ` , and you are working on a new ` x/mod ` API for editing ` go.mod `
93- files that you want to simultaneously integrate into ` gopls ` .
102+ files that you want to simultaneously integrate into gopls.
94103
95104You can work on both ` golang.org/x/tools/gopls ` and ` golang.org/x/mod `
96105simultaneously by creating a ` go.work ` file:
@@ -101,30 +110,30 @@ go work init
101110go work use tools/gopls mod
102111```
103112
104- ...followed by opening the ` $WORK ` directory in your editor.
113+ then opening the ` $WORK ` directory in your editor.
105114
106115## When to manually configure ` GOOS ` , ` GOARCH ` , or ` -tags `
107116
108- As described in the first section, ` gopls@ v0.15.0 ` and later will try to
117+ As described in the first section, gopls v0.15.0 and later will try to
109118configure a new build scope automatically when you open a file that doesn't
110119match the system default operating system (` GOOS ` ) or architecture (` GOARCH ` ).
111120
112121However, per the caveats listed in that section, this automatic behavior comes
113- with limitations. Customize your ` gopls ` environment by setting ` GOOS ` or
122+ with limitations. Customize your gopls environment by setting ` GOOS ` or
114123` GOARCH ` in your
115124[ ` "build.env" ` ] ( https://github.com/golang/tools/blob/master/gopls/doc/settings.md#env-mapstringstring )
116125or ` -tags=... ` in your"
117126[ "build.buildFlags"] ( https://github.com/golang/tools/blob/master/gopls/doc/settings.md#buildflags-string )
118127when:
119128
120129- You want to modify the default build environment.
121- - ` gopls ` is not guessing the ` GOOS/GOARCH ` combination you want to use for
130+ - Gopls is not guessing the ` GOOS/GOARCH ` combination you want to use for
122131 cross platform development.
123132- You need to work on a file that is constrained by a user-defined build tags,
124- such as the build directive ` //go:build special ` .
133+ such as the build directive ` //go:build mytag ` .
125134
126135## GOPATH mode
127136
128- When opening a directory within your ` GOPATH ` , the workspace scope will be just
129- that directory and all directories contained within it. Note that opening
130- a large GOPATH directory can make gopls very slow to start.
137+ When opening a directory within a ` GOPATH ` directory , the workspace scope will
138+ be just that directory and all directories contained within it. Note that
139+ opening a large GOPATH directory can make gopls very slow to start.
0 commit comments