Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 0b3eb4b

Browse files
authored
Docs: restore docs from Sourcegraph.com (#366)
1 parent 8437933 commit 0b3eb4b

File tree

2 files changed

+85
-2
lines changed

2 files changed

+85
-2
lines changed

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,89 @@ interface GoInitializationOptions {
100100
}
101101
```
102102

103+
## Debugging Go code intelligence
104+
105+
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 that in 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 for the `example.io` domain in 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+
103186
## Profiling
104187
105188
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:

buildserver/environment.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ type sourcegraphConfig struct {
278278
// This is to support monorepos such as the ones described in
279279
// https://blog.gopheracademy.com/advent-2015/go-in-a-monorepo/
280280
//
281-
// See https://docs.sourcegraph.com/extensions/language_servers/go#custom-gopaths-go-monorepos.
281+
// See https://github.com/sourcegraph/go-langserver#custom-gopaths-go-monorepos.
282282
GOPATH []string
283283

284284
// RootImportPath defines what Go import path corresponds to the
@@ -289,7 +289,7 @@ type sourcegraphConfig struct {
289289
// information (from glide.yml or canonical import path comments) and
290290
// gives you an opportunity to specify it directly.
291291
//
292-
// See https://docs.sourcegraph.com/extensions/language_servers/go#vanity-import-paths.
292+
// See https://github.com/sourcegraph/go-langserver#vanity-import-paths.
293293
RootImportPath string
294294
} `json:"go"`
295295
}

0 commit comments

Comments
 (0)