You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Oct 12, 2022. It is now read-only.
Additional configuration for Go code intelligence may be required in some cases:
106
+
107
+
-[Custom GOPATHs / Go monorepos](#custom-gopaths--go-monorepos)
108
+
-[Vanity import paths](#vanity-import-paths)
109
+
110
+
### Custom GOPATHs / Go monorepos
111
+
112
+
By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under `$GOPATH/src/...`. That
113
+
is, a Go repository is assumed to only contain Go packages.
114
+
115
+
For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) `$GOPA
116
+
TH` directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi
117
+
s.
118
+
119
+
To inform Sourcegraph's Go language server that your repository contains an entire `$GOPATH` directory, you can use one of three option
120
+
s:
121
+
122
+
1.**Auto-detection via `.vscode/settings.json`**
123
+
124
+
Sourcegraph will automatically detect a Visual Studio Code `settings.json` file with a GOPATH configuration. You may already have o
125
+
ne of these files if you are using Visual Studio Code with the Go extension. The file `.vscode/settings.json` would look like:
126
+
127
+
```json
128
+
{
129
+
"go.gopath": "${workspaceRoot}/YOUR_GOPATH"
130
+
}
131
+
```
132
+
133
+
In this case, Sourcegraph would look for a folder named `YOUR_GOPATH` in the root of the repository.
134
+
135
+
2.**Auto-detection via `.envrc`**
136
+
137
+
Sourcegraph will also automatically detect a GOPATH from an `.envrc` file in the root of the repository. You may already have one of these if you are using direnv. For example a file such as:
138
+
139
+
```bash
140
+
export GOPATH=${PWD}/third_party
141
+
GOPATH_add code:code2
142
+
GOPATH_add /absolute
143
+
```
144
+
145
+
Would lead to Sourcegraph using a final `GOPATH` of `third_party:code:code2`. Note that we will ignore any `/absolute` path, and that we do not execute `.envrc` files but rather scan them for simple syntax such as the above. If you use a more complex `.envrc` file to build your `GOPATH`, this auto-detection may not work for you.
146
+
147
+
3. **Manual configuration via `.sourcegraph/config.json`**
148
+
149
+
If you add a `.sourcegraph/config.json` file in the root directory of your repository, Sourcegraph will use this configuration to determine the `GOPATH` instead of the auto-detection methods described above. An example configuration is:
150
+
151
+
```json
152
+
{
153
+
"go": {
154
+
"GOPATH": ["/third_party", "code"]
155
+
}
156
+
}
157
+
```
158
+
159
+
Sourcegraph will use a final `GOPATH` of `third_party:code`. That is, it will assume the `third_party` and `code` directories in the root of the repository are to be used as `$GOPATH` directories.
160
+
161
+
### Vanity import paths
162
+
163
+
When the Go language server encounters a vanity import path, it must be able to locate the source code for it or else code intelligence will not work for code related to that dependency.
164
+
165
+
For example, consider a repository `github.com/example/server` which contains Go code with an `import "example.io/pkg/logger"` statement.
166
+
167
+
1. If the source code for`example.io/pkg/logger` is located under a vendor directory, Sourcegraph will use thatin the same manner that the `go` tool would.
168
+
2. If the source code for`example.io/pkg/logger` is inside of the current repository at e.g. `github.com/example/pkg/logger`, Sourcegraph will look for it by scanning the repository for a [canonical import path comment](https://golang.org/doc/go1.4#canonicalimports) using some heuristics.
169
+
170
+
- For example, if Sourcegraph finds a canonical import path comment such as `package logger // import "example.io/pkg/logger"`in the `pkg/logger` directory of the repository, Sourcegraph will assume that the code in the `pkg/logger` directory is what should be used when a `import "example.io/pkg/logger"` statement is seen.
171
+
- Note that Sourcegraph only needs to find one such comment forthe `example.io` domainin order to resolve all other vanity imports. That is, placing this comment in`pkg/logger/logger.go` is enough for Sourcegraph to know how to import any package under `example.io/...`.
172
+
- Sometimes the Go language server's heuristics are not able to locate a canonical import path comment in a repository, in which case you can specify the root import path of your repository directly by placing a `.sourcegraph/config.json` file in the root of your repository, e.g.:
173
+
174
+
```json
175
+
{
176
+
"go": {
177
+
"RootImportPath": "example.io/pkg"
178
+
}
179
+
}
180
+
```
181
+
182
+
Which would tell the Go language server to clone `github.com/example/pkg` into `$GOPATH/src/example.io/pkg`.
183
+
184
+
3. Otherwise, Sourcegraph will attempt to fetch `example.io/pkg/logger` via the network using `go get example.io/pkg/logger`.
185
+
103
186
## Profiling
104
187
105
188
If you run into performance issues while using the language server, it can be very helpful to attach a CPU or memory profile with the issue report. To capture one, first [install Go](https://golang.org/doc/install), start `go-langserver` with the pprof flag (e.g. `$GOPATH/bin/go-langserver -pprof :6060`) and then:
0 commit comments