Skip to content

Refactor handling for go-langserver installation #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,8 +277,8 @@ export function installTools(missing: Tool[], goVersion: GoVersion): Promise<voi
outputChannel.appendLine(''); // Blank line for spacing
const failures = res.filter((x) => x != null);
if (failures.length === 0) {
if (containsString(missing, 'go-langserver') || containsString(missing, 'gopls')) {
outputChannel.appendLine('Reload VS Code window to use the Go language server');
if (containsString(missing, 'gopls')) {
outputChannel.appendLine('Reload VS Code window to use the Go language server.');
}
outputChannel.appendLine('All tools successfully installed. You are ready to Go :).');
return;
Expand Down Expand Up @@ -320,7 +320,6 @@ export async function promptForMissingTool(toolName: string) {
return;
}
}

const installOptions = ['Install'];
let missing = await getMissingTools(goVersion);
if (!containsTool(missing, tool)) {
Expand Down
64 changes: 27 additions & 37 deletions src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,10 @@ import {
FormattingOptions,
HandleDiagnosticsSignature,
LanguageClient,
ProvideCompletionItemsSignature,
ProvideDefinitionSignature,
ProvideDocumentFormattingEditsSignature,
ProvideDocumentHighlightsSignature,
ProvideDocumentLinksSignature,
ProvideDocumentSymbolsSignature,
ProvideHoverSignature,
ProvideReferencesSignature,
ProvideRenameEditsSignature,
ProvideSignatureHelpSignature,
ProvideWorkspaceSymbolsSignature,
RevealOutputChannelOn
} from 'vscode-languageclient';
import { ProvideImplementationSignature } from 'vscode-languageclient/lib/implementation';
import { ProvideTypeDefinitionSignature } from 'vscode-languageclient/lib/typeDefinition';
import WebRequest = require('web-request');
import { GoDefinitionProvider } from './goDeclaration';
import { GoHoverProvider } from './goExtraInfo';
Expand Down Expand Up @@ -291,13 +280,11 @@ export function parseLanguageServerConfig(): LanguageServerConfig {
}

/**
* Get the absolute path to the language server to be used.
* If the required tool is not available, then user is prompted to install it.
* This supports the language servers from both Google and Sourcegraph with the
* former getting a precedence over the latter
* If the user has enabled the language server, return the absolute path to the
* correct binary. If the required tool is not available, prompt the user to
* install it. Only gopls is officially supported.
*/
export function getLanguageServerToolPath(): string {
// If language server is not enabled, return
const goConfig = getGoConfig();
if (!goConfig['useLanguageServer']) {
return;
Expand All @@ -311,39 +298,42 @@ export function getLanguageServerToolPath(): string {
return;
}

// Get the path to gopls or any alternative that the user might have set for gopls.
const goplsBinaryPath = getBinPath('gopls');
if (path.isAbsolute(goplsBinaryPath)) {
return goplsBinaryPath;
}

// Get the path to go-langserver or any alternative that the user might have set for go-langserver.
const golangserverBinaryPath = getBinPath('go-langserver');
if (path.isAbsolute(golangserverBinaryPath)) {
return golangserverBinaryPath;
}

// If no language server path has been found, notify the user.
// Determine which language server the user has selected.
// gopls is the default choice.
let languageServerOfChoice = 'gopls';
if (goConfig['alternateTools']) {
const goplsAlternate = goConfig['alternateTools']['gopls'];
const golangserverAlternate = goConfig['alternateTools']['go-langserver'];
if (typeof goplsAlternate === 'string') {

// Check if the user has set the deprecated "go-langserver" setting.
if (goConfig['alternateTools']['go-langserver']) {
vscode.window.showErrorMessage(`The "go.alternateTools" setting for "go-langserver" has been deprecated.
Please set "gopls" instead, and then reload the VS Code window.`);
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the user had gopls installed in this case, then shouldn't we gracefully switch to that?
And also perhaps add a button to update the settings to remove go-langserver from the alternate tools setting?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And if the user didn't had gopls installed, have a button to install gopls?

}
if (goplsAlternate) {
if (typeof goplsAlternate !== 'string') {
vscode.window.showErrorMessage(`Unexpected type for "go.alternateTools" setting for "gopls": ${typeof goplsAlternate}.`);
return;
}
languageServerOfChoice = getToolFromToolPath(goplsAlternate);
} else if (typeof golangserverAlternate === 'string') {
languageServerOfChoice = getToolFromToolPath(golangserverAlternate);
}
}
// Only gopls and go-langserver are supported.
if (languageServerOfChoice !== 'gopls' && languageServerOfChoice !== 'go-langserver') {
// Get the path to the language server binary.
const languageServerBinPath = getBinPath(languageServerOfChoice);
if (path.isAbsolute(languageServerBinPath)) {
return languageServerBinPath;
}

// Installation of gopls is supported. Other language servers must be installed manually.
if (languageServerOfChoice !== 'gopls') {
vscode.window.showErrorMessage(
`Cannot find the language server ${languageServerOfChoice}. Please install it and reload this VS Code window`
`Cannot find the language server ${languageServerOfChoice}. Please install it and reload this VS Code window.`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about updating this to add or use gopls and add a button that will update the alternate tool settings and install gopls if it doesnt exist?

);
return;
}

// Otherwise, prompt the user to install the language server.
promptForMissingTool(languageServerOfChoice);
vscode.window.showInformationMessage('Reload VS Code window after installing the Go language server.');
}

function allFoldersHaveSameGopath(): boolean {
Expand Down
9 changes: 2 additions & 7 deletions src/goTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ export function getConfiguredTools(goVersion: GoVersion): Tool[] {
// Add the linter that was chosen by the user.
maybeAddTool(goConfig['lintTool']);

// Add the language server for Go versions > 1.10 if user has choosen to do so
// Add the language server for Go versions > 1.10 if user has choosen to do so.
// Respect the go.alternateTools setting.
if (goConfig['useLanguageServer'] && goVersion.gt('1.10')) {
maybeAddTool('gopls');
}
Expand Down Expand Up @@ -260,12 +261,6 @@ const allToolsInformation: { [key: string]: Tool } = {
isImportant: true,
description: 'Linter'
},
'go-langserver': {
name: 'go-langserver',
importPath: 'github.com/sourcegraph/go-langserver',
isImportant: false,
description: 'Language Server from Sourcegraph'
},
'gopls': {
name: 'gopls',
importPath: 'golang.org/x/tools/gopls',
Expand Down