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

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Add copy buttons to all
 blocks
(function() {
function addCopyButtons() {
document.querySelectorAll('pre code').forEach(function(codeBlock) {
if (codeBlock.parentElement.hasAttribute('data-copy-added')) return;
codeBlock.parentElement.setAttribute('data-copy-added', 'true');
var btn = document.createElement('button');
btn.textContent = 'Copy';
btn.style.cssText = 'position:absolute;top:4px;right:4px;padding:2px 8px;font-size:11px;background:#4ecdc4;border:none;border-radius:4px;color:#1a1a2e;cursor:pointer;opacity:0.7;transition:opacity 0.2s;';
btn.onmouseover = function() { this.style.opacity = '1'; };
btn.onmouseout = function() { this.style.opacity = '0.7'; };
btn.onclick = function() {
navigator.clipboard.writeText(codeBlock.textContent).then(function() {
btn.textContent = 'Copied!';
setTimeout(function() { btn.textContent = 'Copy'; }, 1500);
});
};
codeBlock.parentElement.style.position = 'relative';
codeBlock.parentElement.appendChild(btn);
});
}
addCopyButtons();
// Re-run on dynamic content
var observer = new MutationObserver(addCopyButtons);
observer.observe(document.body, { childList: true, subtree: true });
})();
}
} catch(__e) { console.warn('[Userscript:Add Copy Buttons to Code Blocks]', __e); }
})();
(function(){
try {
var __m = "github.com";
var __re = new RegExp('^' + "github\\.com" + '
GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Force GitHub README to respect dark mode (function() { var style = document.createElement('style'); style.textContent = ' .markdown-body { color-scheme: dark light; } .markdown-body pre { background: #161b22 !important; } .markdown-body code { background: rgba(110, 118, 129, 0.4) !important; } .markdown-body table th, .markdown-body table td { border-color: #30363d !important; } .markdown-body img { background: #0d1117; } .markdown-body blockquote { border-left-color: #8b949e; } .markdown-body hr { border-color: #30363d; } '; document.head.appendChild(style); })(); } } catch(__e) { console.warn('[Userscript:GitHub Dark Mode README Fix]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Highlight search terms from Google/DuckDuckGo/Bing referrer (function() { var ref = document.referrer; var terms = []; if (ref.includes('google.com') || ref.includes('duckduckgo.com') || ref.includes('bing.com')) { var url = new URL(ref); var q = url.searchParams.get('q') || url.searchParams.get('p'); if (q) { terms = q.split(/\s+/).filter(function(t) { return t.length > 2; }); } } if (terms.length === 0) return; var style = document.createElement('style'); style.textContent = '.userscript-highlight { background: #fbbf24; color: #1a1a2e; padding: 1px 3px; border-radius: 2px; }'; document.head.appendChild(style); function highlight(node) { if (node.nodeType === 3) { // text node var text = node.textContent; var found = false; terms.forEach(function(term) { var regex = new RegExp('(' + term.replace(/[.*+?^${}()|[\]\\]/g, '\\') + ')', 'gi'); if (regex.test(text)) { found = true; var frag = document.createDocumentFragment(); var parts = text.split(regex); parts.forEach(function(part, i) { if (i % 2 === 0) { frag.appendChild(document.createTextNode(part)); } else { var span = document.createElement('span'); span.className = 'userscript-highlight'; span.textContent = part; frag.appendChild(span); } }); node.parentNode.replaceChild(frag, node); } }); } else if (node.nodeType === 1 && node.childNodes) { // element var skipTags = ['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA', 'INPUT', 'SELECT']; if (!skipTags.includes(node.tagName)) { Array.from(node.childNodes).forEach(highlight); } } } highlight(document.body); // Re-highlight on dynamic content var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1 || node.nodeType === 3) highlight(node); }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Highlight Search Terms]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Strip utm_, fbclid, gclid, etc. from all links on page (function() { var trackingParams = ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content', 'fbclid', 'gclid', 'dclid', 'msclkid', 'yclid', 'ref', 'ref_src', 'source', 'medium', 'campaign']; function cleanUrl(url) { try { var u = new URL(url, window.location.origin); var changed = false; trackingParams.forEach(function(p) { if (u.searchParams.has(p)) { u.searchParams.delete(p); changed = true; } }); return changed ? u.toString() : url; } catch (e) { return url; } } function cleanLinks() { document.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } cleanLinks(); var observer = new MutationObserver(function(mutations) { mutations.forEach(function(m) { m.addedNodes.forEach(function(node) { if (node.nodeType === 1) { if (node.tagName === 'A') cleanLinks(); node.querySelectorAll('a[href]').forEach(function(a) { var clean = cleanUrl(a.href); if (clean !== a.href) a.href = clean; }); } }); }); }); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:Remove Tracking Parameters from Links]', __e); } })(); (function(){ try { var __m = "youtube.com"; var __re = new RegExp('^' + "youtube\\.com" + ' GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Auto-enable theater mode on YouTube (function() { function tryTheater() { var btn = document.querySelector('button[aria-label="Theater mode"], ytd-player #player button[title="Theater mode"]'); if (btn && !btn.classList.contains('activated')) { btn.click(); } } // Try immediately tryTheater(); // Try after navigation (SPA) var lastUrl = location.href; setInterval(function() { if (location.href !== lastUrl) { lastUrl = location.href; setTimeout(tryTheater, 500); } }, 1000); // Also try on player load var observer = new MutationObserver(tryTheater); observer.observe(document.body, { childList: true, subtree: true }); })(); } } catch(__e) { console.warn('[Userscript:YouTube Theater Mode Default]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Remove or un-stick sticky/fixed headers that block content (function() { function unstick() { document.querySelectorAll('header, nav, [role="banner"], .header, .navbar, .sticky, .fixed-top, [style*="position: fixed"], [style*="position:sticky"]').forEach(function(el) { if (el.style.position === 'fixed' || el.style.position === 'sticky' || getComputedStyle(el).position === 'fixed' || getComputedStyle(el).position === 'sticky') { el.style.position = 'static'; el.style.top = 'auto'; el.style.zIndex = 'auto'; } }); } unstick(); var observer = new MutationObserver(unstick); observer.observe(document.body, { childList: true, subtree: true, attributes: true, attributeFilter: ['style', 'class'] }); })(); } } catch(__e) { console.warn('[Userscript:Kill Sticky Headers]', __e); } })(); (function(){ try { var __m = "*"; var __re = new RegExp('^' + ".*" + ' GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages

, 'i'); if (__m === '*' || __re.test(location.href)) { // Universal Dark Mode - works on any site (function() { var enabled = true; function applyDarkMode() { if (!enabled) return; // Create style element if it doesn't exist var style = document.getElementById('universal-dark-mode-style'); if (!style) { style = document.createElement('style'); style.id = 'universal-dark-mode-style'; document.head.appendChild(style); } // Dark mode CSS - inverts colors but preserves images/video style.textContent = ' /* Invert everything except media */ html { filter: invert(1) hue-rotate(180deg) !important; background: #1a1a2e !important; } /* Restore images, videos, iframes, canvas */ img, video, iframe, canvas, svg, picture, [style*="background-image"] { filter: invert(1) hue-rotate(180deg) !important; } /* Preserve specific elements that should not be inverted */ .no-dark-mode, .no-dark-mode *, [data-theme="light"], [data-theme="light"], .ace_editor, .ace_editor *, .CodeMirror, .CodeMirror *, .monaco-editor, .monaco-editor *, .markdown-body pre, .markdown-body pre *, .highlight, .highlight *, pre code, pre code * { filter: none !important; } /* Fix common UI elements */ .modal, .popup, .dropdown-menu, .tooltip, .popover { filter: invert(1) hue-rotate(180deg) !important; background: #2d2d44 !important; border-color: #444 !important; } /* Scrollbars */ ::-webkit-scrollbar { background: #1a1a2e !important; } ::-webkit-scrollbar-thumb { background: #444 !important; } ::-webkit-scrollbar-thumb:hover { background: #555 !important; } /* Selection */ ::selection { background: #4ecdc4 !important; color: #1a1a2e !important; } ::-moz-selection { background: #4ecdc4 !important; color: #1a1a2e !important; } '; } function removeDarkMode() { var style = document.getElementById('universal-dark-mode-style'); if (style) style.remove(); } // Toggle with Alt+Shift+D document.addEventListener('keydown', function(e) { if (e.altKey && e.shiftKey && e.key === 'D') { e.preventDefault(); enabled = !enabled; if (enabled) { applyDarkMode(); console.log('[Universal Dark Mode] Enabled'); } else { removeDarkMode(); console.log('[Universal Dark Mode] Disabled'); } } }); // Apply on load applyDarkMode(); // Re-apply on dynamic content var observer = new MutationObserver(function(mutations) { if (enabled && !document.getElementById('universal-dark-mode-style')) { applyDarkMode(); } }); observer.observe(document.head, { childList: true }); console.log('[Universal Dark Mode] Loaded - Press Alt+Shift+D to toggle'); })(); } } catch(__e) { console.warn('[Userscript:Universal Dark Mode]', __e); } })(); })(); GitHub - sourcegraph/go-langserver: Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP) · GitHub
Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Repository files navigation

Go Language Server Build Status

Note: We have deprioritized work on this language server for use in editors in favor of Google's Go language server, gopls. It is in the best interests of the community to only have a single language server.

go-langserver is a Go language server that speaks Language Server Protocol. It supports editor features such as go-to-definition, hover, and find-references for Go projects.

Open in Sourcegraph

To build and install the standalone go-langserver run

go get -u github.com/sourcegraph/go-langserver

Support

HoverJump to defFind referencesWorkspace symbolsVFS extensionIsolatedParallel
Go

InitializationOptions

If you are a client wanting to integrate go-langserver, you can use the following as initializationOptions in your initialize request to adjust the behaviour:

interfaceGoInitializationOptions{/** * funcSnippetEnabled enables the returning of argument snippets * on `func` completions, eg. func(foo string, arg2 bar). * Requires code completion to be enabled. * * Defaults to true if not specified. */funcSnippetEnabled?: boolean;/** * gocodeCompletionEnabled enables code completion feature (using gocode). * * Defaults to false if not specified. */gocodeCompletionEnabled?: boolean;/** * formatTool decides which tool is used to format documents. Supported: goimports and gofmt. * * Defaults to goimports if not specified. */formatTool?: "goimports"|"gofmt";/** * lintTool decides which tool is used for linting documents. Supported: none and golint * * Diagnostics must be enabled for linting to work. * * Defaults to none if not specified. */lintTool?: "none"|"golint";/** * goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use. * * Defaults to empty string if not specified. */goimportsLocalPrefix?: string;/** * MaxParallelism controls the maximum number of goroutines that should be used * to fulfill requests. This is useful in editor environments where users do * not want results ASAP, but rather just semi quickly without eating all of * their CPU. * * Defaults to half of your CPU cores if not specified. */maxParallelism?: number;/** * useBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should * be used. * * Defaults to true if not specified. */useBinaryPkgCache?: boolean;/** * DiagnosticsEnabled enables handling of diagnostics. * * Defaults to false if not specified. */diagnosticsEnabled?: boolean;}

Debugging Go code intelligence

Additional configuration for Go code intelligence may be required in some cases:

Custom GOPATHs / Go monorepos

By default, Sourcegraph assumes that Go code in a repository represents Go packages that would be placed under $GOPATH/src/.... That is, a Go repository is assumed to only contain Go packages.

For some repositories, such as Go monorepos, this may not be the case. These repositories typically have an entire (or multiple) $GOPA TH directories comitted to them, and the Go language server may not be able to provide code intelligence without being informed of thi s.

To inform Sourcegraph's Go language server that your repository contains an entire $GOPATH directory, you can use one of three option s:

  1. Auto-detection via .vscode/settings.json

    Sourcegraph will automatically detect a Visual Studio Code settings.json file with a GOPATH configuration. You may already have o ne of these files if you are using Visual Studio Code with the Go extension. The file .vscode/settings.json would look like:

    {
    "go.gopath": "${workspaceRoot}/YOUR_GOPATH"
    }

    In this case, Sourcegraph would look for a folder named YOUR_GOPATH in the root of the repository.

  2. Auto-detection via .envrc

    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:

    export GOPATH=${PWD}/third_party
    GOPATH_add code:code2
    GOPATH_add /absolute

    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.

  3. Manual configuration via .sourcegraph/config.json

    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:

    {
    "go": {
    "GOPATH": ["/third_party", "code"]
    }
    }

    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.

Vanity import paths

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.

For example, consider a repository github.com/example/server which contains Go code with an import "example.io/pkg/logger" statement.

  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.

  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 using some heuristics.

    • 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.
    • 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/....
    • 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.:
    {
    "go": {
    "RootImportPath": "example.io/pkg"
    }
    }

    Which would tell the Go language server to clone github.com/example/pkg into $GOPATH/src/example.io/pkg.

  3. Otherwise, Sourcegraph will attempt to fetch example.io/pkg/logger via the network using go get example.io/pkg/logger.

Profiling

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, start go-langserver with the pprof flag (e.g. $GOPATH/bin/go-langserver -pprof :6060) and then:

Capture a heap (memory) profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/heap > heap.svg

Capture a CPU profile:

go tool pprof -svg $GOPATH/bin/go-langserver http://localhost:6060/debug/pprof/profile > cpu.svg

Since these capture the active resource usage, it's best to run these commands while the issue is occurring (i.e. while memory or CPU is high).

About

Go language server to add Go support to editors and other tools that use the Language Server Protocol (LSP)

Topics

Resources

Stars

1.2k stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages