Skip to content

Commit 62de799

Browse files
committed
[release] docs: update GOPATH, modules, and gopls documentation
This change finishes the general work of updating the documentation. The GOPATH, modules, and gopls pages are finalized. I expect that I still have 3 CLs left in this set of changes: (1) Generating settings and commands docs from the package.json (2) A pass for proofreading, checking links, and other improvements (3) A pass to remove and file issues for all TODOs Let me know if there's anything else I'm missing here. Change-Id: I0ba9c2be9cc92e32dc7bae4526745306ef49a503 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/236840 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> (cherry picked from commit 2ce0e4f) Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/236977
1 parent 2300467 commit 62de799

File tree

9 files changed

+329
-151
lines changed

9 files changed

+329
-151
lines changed

docs/commands.md

Lines changed: 69 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Finally, you can also see a full list by using a meta command: `Go: Show All Com
2121

2222
## Detailed list
2323

24+
<!--TODO(rstambler): Automatically generate this list using the package.json.-->
25+
2426
Below is a detailed list of commands. They are categorized into [code editing and generation](#code-editing-and-generation), [testing and benchmarking](#testing-and-benchmarking), [build, lint, and vet](#build-lint-and-vet), [miscellaneous](#miscellaneous), and [troubleshooting](#troubleshooting). You will find the [troubleshooting](#troubleshooting) commands helpful when diagnosing an issue with the extension (learn more in the [Troubleshooting documentation](troubleshooting.md)).
2527

2628
### Code editing and generation
@@ -32,7 +34,11 @@ Below is a detailed list of commands. They are categorized into [code editing an
3234
<!--TODO(rstambler): Confirm exactly how this works.-->
3335
Manually add an import to your file. See [Add import](features.md#add-import).
3436

35-
#### [`Go: Add Package to Workspace`]()
37+
#### [`Go: Add Package to Workspace`]
38+
39+
Add a package to the current workspace.
40+
41+
<!--TODO(rstambler): Figure out how this command works, its use cases, and how it fits into modules.-->
3642

3743
#### [`Go: Add Tags to Struct Fields`](features.md#add-struct-tags)
3844

@@ -62,33 +68,47 @@ Generate unit tests for the current file. See [Generate unit tests](features.md#
6268

6369
Generate unit tests for the current package. See [Generate unit tests](features.md#generate-unit-tests).
6470

65-
#### [`Go: Extract to function`]()
71+
#### [`Go: Extract to function`](features.md#refactor)
6672

67-
#### [`Go: Extract to variable`]()
73+
Extract the highlighted code to a function. Provided by the [`godoctor`](tools.md#godoctor) tool. Learn more about [refactoring](features.md#refactoring).
74+
75+
#### [`Go: Extract to variable`](features.md#refactor)
76+
77+
Extract the highlighted code to a local variable. Provided by the [`godoctor`](tools.md#godoctor) tool. Learn more about [refactoring](features.md#refactoring).
6878

6979
### Testing and benchmarking
7080

7181
#### [`Go: Test Function at Cursor`](features.md#test-and-benchmark-in-the-editor)
7282

7383
Run the test function at the current cursor position in the file.
7484

75-
#### [`Go: Subtest at Cursor`]()
85+
#### [`Go: Subtest at Cursor`](features.md#test-and-benchmark-in-the-editor)
86+
87+
Run the subtest (`t.Run`) at the current cursor position in the file.
7688

77-
#### [`Go: Benchmark Function At Cursor`]()
89+
#### [`Go: Benchmark Function At Cursor`](features.md#test-and-benchmark-in-the-editor)
7890

79-
#### [`Go: Debug Test At Cursor`]()
91+
Run the benchmark at the current cursor position in the file.
92+
93+
#### [`Go: Debug Test At Cursor`](features.md#debugging)
94+
95+
Debug the test at the current cursor position.
8096

8197
#### [`Go: Test File`](features.md#test-and-benchmark-in-the-editor)
8298

8399
Run all of the tests in the current file.
84100

85-
#### [`Go: Benchmark File`]()
101+
#### [`Go: Benchmark File`](features.md#test-and-benchmark-in-the-editor)
102+
103+
Run all of the benchmarks in the current file.
86104

87105
#### [`Go: Test Package`](features.md#test-and-benchmark-in-the-editor)
88106

89-
Run all of tests in the current package.
107+
Run all of the tests in the current package.
90108

91-
#### [`Go: Benchmark Package`]()
109+
#### [`Go: Benchmark Package`](features.md#test-and-benchmark-in-the-editor)
110+
111+
Run all of the benchmarks in the current package.
92112

93113
#### [`Go: Test Previous`](features.md#test-and-benchmark-in-the-editor)
94114

@@ -98,33 +118,57 @@ Re-run the most recently executed test command.
98118

99119
Run all of the tests in the current workspace.
100120

101-
#### [`Go: Cancel Running Tests]()
121+
#### [`Go: Cancel Running Tests`](features.md#test-and-benchmark-in-the-editor)
122+
123+
Cancel currently running tests.
124+
125+
#### [`Go: Toggle Test File`](features.md#toggle-between-code-and-tests)
102126

103-
#### [`Go: Toggle Test File`]()
127+
Toggle between a file and its corresponding test file.
104128

105-
#### [`Go: Apply Cover Profile]()
129+
#### [`Go: Apply Cover Profile`](features.md#code-coverage)
106130

107-
#### [`Go: Toggle Test Coverage In Current Package`]()
131+
Apply a given [cover profile](https://blog.golang.org/cover) to the current file.
132+
133+
#### [`Go: Toggle Test Coverage In Current Package`](features.md#code-coverage)
134+
135+
Show [code coverage](features.md#code-coverage) in the current file.
108136

109137
### Build, lint, and vet
110138

111-
#### [`Go: Build Current Package`]()
139+
#### [`Go: Build Current Package`](features.md#build-errors)
140+
141+
Build the current package and show build errors.
142+
143+
#### [`Go: Vet Current Package`](features.md#vet-errors)
144+
145+
Show vet errors for the current package.
146+
147+
#### [`Go: Lint Current Package`](features.md#lint-errors)
112148

113-
#### [`Go: Lint Current Package`]()
149+
Show lint errors for the current package.
114150

115-
#### [`Go: Vet Current Package`]()
151+
#### [`Go: Build Workspace`](features.md#build-errors)
116152

117-
#### [`Go: Build Workspace`]()
153+
Build all of the packages in the current workspace and show build errors.
118154

119-
#### [`Go: Lint Workspace`]()
155+
#### [`Go: Vet Workspace`](features.md#vet-errors)
120156

121-
#### [`Go: Vet Workspace`]()
157+
Show vet errors for all of the packages in the current workspace.
122158

123-
#### [`Go: Install Current Package`]()
159+
#### [`Go: Lint Workspace`](features.md#lint-errors)
160+
161+
Show lint errors for all of the packages in the current workspace.
162+
163+
#### `Go: Install Current Package`
164+
165+
Install the current package and its dependencies.
124166

125167
### Miscellaneous
126168

127-
#### [`Go: Restart Language Server`]()
169+
#### [`Go: Restart Language Server`](gopls.md)
170+
171+
Use this command to restart the [language server](gopls.md) without reloading the VS Code window. This can be helpful if something seems goes wrong with the language server (for example, if you see incorrect error messages).
128172

129173
#### [`Go: Run on Go Playground`](features.md#go-playground)
130174

@@ -134,13 +178,15 @@ Upload the current selection or file to the Go Playground ([play.golang.org](htt
134178

135179
#### `Go: Current GOPATH`
136180

137-
See the current value of GOPATH. This is not equivalent to `go env GOPATH`, as your VS Code settings may have altered the value of `GOPATH` used by the extension.
181+
See the current value of GOPATH. This is not equivalent to `go env GOPATH`, as your VS Code settings may have altered the value of `GOPATH` used by the extension. This command is helpful when [troubleshooting](troubleshooting.md) the extension.
138182

139183
#### [`Go: Install/Update Tools`](tools.md)
140184

141185
Install or update the Go tools on which the extension depends. Tools can be installed or updated all at once, or individual tools can be selected.
142186

143-
#### [`Go: Locate Configured Go Tools`]()
187+
#### [`Go: Locate Configured Go Tools`](troubleshooting.md#check-your-set-up)
188+
189+
This command is helpful when [troubleshooting](troubleshooting.md) the extension. It prints out the environment variables and paths to the tools used by the extension. See an example of the command's output in the [troubleshooting documentation](troubleshooting.md#check-your-set-up).
144190

145191
[`golint`]: https://pkg.go.dev/golang.org/x/lint/golint?tab=overview
146192
[`staticcheck`]: https://pkg.go.dev/honnef.co/go/tools/staticcheck?tab=overview

docs/features.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Please see the [Tools documentation](tools.md) for details on how to troubleshoo
2626
* [Format and organize imports](#format-and-organize-imports)
2727
* [Add import](#add-import)
2828
* [Rename symbol](#rename-symbol)
29+
* [Refactor](#refactor)
2930
* [Code Generation](#code-generation)
3031
* [Add or remove struct tags](#add-or-remove-struct-tags)
3132
* [Generate interface implementation](#generate-interface-implementation)
@@ -127,6 +128,10 @@ Rename all occurrences of a symbol in your workspace. This feature is provided b
127128

128129
This feature is not available if you are using Go modules without `gopls`, the Go language server.
129130

131+
### Refactor
132+
133+
Extract a piece of code to a local variable or to its own function using the [`Go: Extract to variable`](commands.md#go-extract-to-variable) and [`Go: Extract to function`](commands.md#go-extract-to-function) commands. These features are provided by the [`godoctor`](tools.md#godoctor) tool. These features do not work with Go modules, but they will be supported by [`gopls`] in the future (see [golang/go#37170](https://github.com/golang/go/issues/37170)).
134+
130135
## Code Generation
131136

132137
### Add or remove struct tags
@@ -194,3 +199,5 @@ This extension offers debugging of Go programs. See the [debugging documentation
194199
### Go Playground
195200

196201
Export your current file to the [Go Playground](https://play.golang.org) via the `Go: Run On Go Playground` command. This is useful for quickly exporting code for an example.
202+
203+
[`gopls`]: gopls.md

docs/gopath.md

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,80 @@
11
# `GOPATH`
22

3-
At any point in time, you can run the command `Go: Current GOPATH` to see the GOPATH being used by the extension.
3+
The `GOPATH` environment variable is a fundamental part of writing Go code before the introduction of [Go modules]. It specifies the location of your workspace, and it defaults to `$HOME/go`. A `GOPATH` directory contains `src`, `bin`, and `pkg` directories. Your code is typically located in the `$GOPATH/src` directory.
44

5-
### GOPATH from the environment variable
6-
Out of the box, the extension uses the value of the environment variable `GOPATH`. From Go 1.8 onwards, if no such environment variable is set, then the default GOPATH as deciphered from the command `go env` is used.
5+
If you are not familiar with Go and `GOPATH`, please first read about [writing Go code with `GOPATH`](https://golang.org/doc/gopath_code.html#GOPATH).
76

8-
### GOPATH from `go.gopath` setting
9-
Setting `go.gopath` in User settings overrides the GOPATH that was derived from the above logic.
10-
Setting `go.gopath` in Workspace settings overrides the one from User settings.
11-
You can set multiple folders as GOPATH in this setting. Note that they should be `;` separated in Windows and `:` separated otherwise.
7+
## Overview
128

13-
### GOPATH from `go.inferGopath` setting
14-
Setting `go.inferGopath` overrides the value set in `go.gopath` setting. If `go.inferGopath` is set to true, the extension will try to infer the `GOPATH` from the path of the workspace i.e. the directory opened in `vscode`. It searches upwards in the path for the `src` directory, and sets `GOPATH` to one level above that. It will also include the global GOPATH. Run `go env GOPATH` to find out what your global GOPATH is.
9+
* [Check the value of `GOPATH`](#check-the-value-of-gopath)
10+
* [Setting `GOPATH`](#setting-gopath)
11+
* [Different `GOPATH`s for different projects](#different-gopaths-for-different-projects)
12+
* [Automatically inferring your `GOPATH`](#automatically-inferring-your-gopath)
13+
* [Install tools to a separate `GOPATH`](#install-tools-to-a-separate-gopath)
1514

16-
For example, if your project looks like `/aaa/bbb/ccc/src/...`, then opening the directory `/aaa/bbb/ccc/src` (or anything below that) will cause the extension to search upwards, find the `src` component in the path, and set the `GOPATH` to one level above that i.e. `GOPATH=/aaa/bbb/ccc`.
15+
## Check the value of `GOPATH`
1716

18-
This setting is useful when you are working on different Go projects which have different GOPATHs. Instead of setting the GOPATH in the workspace settings of each project or setting all the paths as `;`/`:` separated string, you can just set `go.inferGopath` to `true`and the extension uses the right GOPATH automatically.
17+
First, it's useful to quickly check that you are using the right `GOPATH`. Two commands report the `GOPATH` value used by the VS Code Go extension: (1) `Go: Current GOPATH`, or (2) `Go: Locate Configured Go Tools`. Use either of these commands to check which `GOPATH` the extension is using.
1918

20-
### GOPATH for installing the Go tools using `go.toolsGopath`
19+
If the `GOPATH` value is incorrect, see the details below on how to configure it.
2120

22-
The `go get` command installs Go tools in your GOPATH. To prevent the Go tools from cluttering your GOPATH, use the `go.toolsGopath` setting to provide a separate GOPATH to use just for the Go tools.
21+
## Setting `GOPATH`
2322

24-
The first time you set `go.toolsGopath`, you will have to run `Go: Install/Update Tools` command so that the Go tools get installed in the provided location.
23+
If you have chosen not to use [Go modules], you will need to configure your `GOPATH`. Modules have largely eliminated the need for a `GOPATH`, so if you're interested in using them, taking a look at the [modules documentation](modules.md) for the VS Code Go extension.
2524

26-
If `go.toolsGopath` is not set or if the Go tools are not found there, then the Go tools from the GOPATH derived from the logic described in the previous section are used. If not found in there as well, then they are looked for in the paths that are part of the PATH environment variable.
25+
Setting `GOPATH` is typically as simple as setting the environment variable once in your system's configuration. Take a look at the [Setting `GOPATH`](https://github.com/golang/go/wiki/SettingGOPATH) Wiki page if you're unsure how to do this.
26+
27+
By default, the extension uses the value of the environment variable `GOPATH`. If no such environment variable is set, the extension runs `go env` and uses the `GOPATH` provided in that output.
28+
29+
Note that, much like a `PATH` variable, `GOPATH` can contain multiple directory paths, separated by `:` or `;`. This allows you to set different `GOPATH`s for different projects.
30+
31+
Still, there are a number of cases in which you might want a more complicated `GOPATH` set-up. This document will explain how to configure and manage your `GOPATH` within the VS Code Go extension.
32+
33+
## Different `GOPATH`s for different projects
34+
35+
Setting [`go.gopath`](settings.md#gopath) in your [user settings](https://vscode.readthedocs.io/en/latest/getstarted/settings/) overrides the environment's `GOPATH` value.
36+
37+
[Workspace settings](https://vscode.readthedocs.io/en/latest/getstarted/settings/) override user settings, so you can use the [`go.gopath`](settings.md#gopath) setting to set different `GOPATH`s for different projects. A `GOPATH` can also contain multiple directories, so this setting is not necessary to achieve this behavior.
38+
39+
## Automatically inferring your `GOPATH`
40+
41+
**NOTE: This feature only works in `GOPATH` mode, not in module mode.**
42+
43+
The [`go.inferGopath`](settings.md#inferGopath) setting overrides the [`go.gopath`](settings.md#gopath) setting. If you set [`go.inferGopath`](settings.md#inferGopath) to `true`, the extension will try to infer your `GOPATH` based on the workspace opened in VS Code. This is done by searching for a `src` directory in your workspace. The parent of this `src` directory is then added to your [global `GOPATH`](#setting-gopath) (`go env GOPATH`).
44+
45+
For example, say your global `GOPATH` is `$HOME/go`, but you are working in a repository with the following structure.
46+
47+
```bash
48+
foo/
49+
└── bar
50+
└── src
51+
└── main.go
52+
```
53+
54+
If you open the `foo` directory as your workspace root in VS Code, [`"go.inferGopath"`](settings.md#inferGopath) will set your `GOPATH` to `$HOME/go:/path/to/foo/bar`.
55+
56+
This setting is useful because it allows you to avoid setting the `GOPATH` in the workspace settings of each of your projects.
57+
58+
## Install tools to a separate `GOBIN`
59+
60+
If you find yourself frequently switching between `GOPATH`s, you may have found that the extension prompts you to install tools for every `GOPATH`. You can resolve this by making sure your tool installations are on your `PATH`, or you can configure a separate directory for tool installation: `GOBIN`. This environment variable tells the `go` command where to install all binaries. Configure it by setting:
61+
62+
```json5
63+
"go.toolsEnvVars": {
64+
"GOBIN": "path/to/gobin"
65+
}
66+
```
67+
68+
## Install tools to a separate `GOPATH`
69+
70+
**NOTE: The following is only relevant if you are using a Go version that does not support [Go modules], that is, any version of Go before 1.11.**
71+
72+
Before Go 1.11, the `go get` command installs tools and their source code to your `GOPATH`. Because this extension uses a lot of different tools, this can clutter up your `GOPATH`. If you wish to reduce this clutter, you can have the extension install tools do a different location. This also addresses the issue described above, when switching `GOPATHs` forces you to reinstall Go tools.
73+
74+
This can be done via the [`"go.toolsGopath"`](settings.md#toolsGopath) setting. Use it to specify an alternate directory that the extension can use to install tools. After you set this value, be sure to run the `Go: Install/Update Tools` command so that the Go tools get installed to the provided location.
75+
76+
The extension will fall back to your existing `GOPATH` if tools are not found in the [`go.toolsGopath`](settings.md#toolsGopath) directory.
77+
78+
<!--TODO(rstambler): Should we deprecate this setting for Go > 1.11?-->
79+
80+
[Go modules]: https://blog.golang.org/using-go-modules

0 commit comments

Comments
 (0)