Skip to content

Commit

Permalink
goLanguageServer: add an opt-in "always update" setting for gopls
Browse files Browse the repository at this point in the history
Most users always accept gopls update notifications, and if we release
multiple gopls versions in rapid succession, they can be annoying. Allow
users to opt-in to an automatic update mechanism.

For automatic updates, don't show any output channels (except on
failure) to avoid bothering the user.

Fixes golang#1095

Change-Id: I9f29598ee94803babc616df061ab298ca448cd95
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/287852
Trust: Rebecca Stambler <[email protected]>
Run-TryBot: Rebecca Stambler <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
stamblerre committed Jan 29, 2021
1 parent 17e80f5 commit aac9542
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,11 @@ Environment variables that will be passed to the tools that run the Go tools (e.
### `go.toolsGopath`

Location to install the Go tools that the extension depends on if you don't want them in your GOPATH. When specified as a workspace setting, the setting is used only when the workspace is marked trusted with "Go: Toggle Workspace Trust Flag".
### `go.toolsManagement.autoUpdate`

Automatically update the tools used by the extension, without prompting the user.

Default: `false`
### `go.toolsManagement.checkForUpdates`

Specify whether to prompt about new versions of Go and the Go tools (currently, only `gopls`) the extension depends on<br/>
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1730,6 +1730,12 @@
],
"markdownDescription": "Specify whether to prompt about new versions of Go and the Go tools (currently, only `gopls`) the extension depends on"
},
"go.toolsManagement.autoUpdate": {
"type": "boolean",
"default": false,
"description": "Automatically update the tools used by the extension, without prompting the user.",
"scope": "resource"
},
"go.useGoProxyToCheckForToolUpdates": {
"type": "boolean",
"default": true,
Expand Down
33 changes: 29 additions & 4 deletions src/goInstallTools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import fs = require('fs');
import path = require('path');
import { SemVer } from 'semver';
import util = require('util');
import { ConfigurationTarget } from 'vscode';
import { workspace } from 'vscode';
import vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment, toolInstallationEnvironment } from './goEnv';
Expand Down Expand Up @@ -98,13 +100,16 @@ export async function installAllTools(updateExistingToolsOnly: boolean = false)
*/
export async function installTools(
missing: ToolAtVersion[],
goVersion: GoVersion
goVersion: GoVersion,
silent?: boolean,
): Promise<{ tool: ToolAtVersion, reason: string }[]> {
if (!missing) {
return [];
}

outputChannel.show();
if (!silent) {
outputChannel.show();
}
outputChannel.clear();

const envForTools = toolInstallationEnvironment();
Expand Down Expand Up @@ -172,6 +177,11 @@ export async function installTools(
if (failures.length === 0) {
outputChannel.appendLine('All tools successfully installed. You are ready to Go :).');
} else {
// Show the output channel on failures, even if the installation should
// be silent.
if (silent) {
outputChannel.show();
}
outputChannel.appendLine(failures.length + ' tools failed to install.\n');
for (const failure of failures) {
outputChannel.appendLine(`${failure.tool.name}: ${failure.reason} `);
Expand Down Expand Up @@ -345,15 +355,30 @@ export async function promptForUpdatingTool(

let choices: string[] = ['Update'];
if (toolName === `gopls`) {
choices.push('Release Notes');
choices = ['Always Update', 'Update Once', 'Release Notes'];
}

const goVersion = await getGoVersion();

while (choices.length > 0) {
const selected = await vscode.window.showInformationMessage(updateMsg, ...choices);
switch (selected) {
case 'Always Update':
// Update the user's settings to enable auto updates. They can
// always opt-out in their settings.
const goConfig = getGoConfig();
await goConfig.update('toolsManagement.autoUpdate', true, ConfigurationTarget.Global);

// And then install the tool.
choices = [];
await installTools([toolVersion], goVersion);
break;
case 'Update Once':
choices = [];
await installTools([toolVersion], goVersion);
break;
case 'Update':
choices = [];
const goVersion = await getGoVersion();
await installTools([toolVersion], goVersion);
break;
case 'Release Notes':
Expand Down
13 changes: 12 additions & 1 deletion src/goLanguageServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
getBinPath,
getCheckForToolsUpdatesConfig,
getCurrentGoPath,
getGoVersion,
getWorkspaceFolderPath,
removeDuplicateDiagnostics
} from './util';
Expand Down Expand Up @@ -171,7 +172,17 @@ function scheduleGoplsSuggestions(tool: Tool) {
return;
}
const versionToUpdate = await shouldUpdateLanguageServer(tool, cfg);
if (versionToUpdate) {
if (!versionToUpdate) {
return;
}
// If the user has opted in to automatic tool updates, we can update
// without prompting.
const toolsManagementConfig = getGoConfig()['toolsManagement'];
if (toolsManagementConfig && toolsManagementConfig['autoUpdate'] === true) {
const goVersion = await getGoVersion();
const toolVersion = { ...tool, version: versionToUpdate }; // ToolWithVersion
await installTools([toolVersion], goVersion, true);
} else {
promptForUpdatingTool(tool.name, versionToUpdate);
}
};
Expand Down

0 comments on commit aac9542

Please sign in to comment.