forked from golang/vscode-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goVet.ts
125 lines (111 loc) · 3.76 KB
/
goVet.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See LICENSE in the project root for license information.
*--------------------------------------------------------*/
import path = require('path');
import vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import { vetDiagnosticCollection } from './goMain';
import { diagnosticsStatusBarItem, outputChannel } from './goStatus';
import {
getGoVersion,
getWorkspaceFolderPath,
handleDiagnosticErrors,
ICheckResult,
resolvePath,
runTool
} from './util';
/**
* Runs go vet in the current package or workspace.
*/
export function vetCode(vetWorkspace?: boolean) {
const editor = vscode.window.activeTextEditor;
if (!editor && !vetWorkspace) {
vscode.window.showInformationMessage('No editor is active, cannot find current package to vet');
return;
}
if (editor.document.languageId !== 'go' && !vetWorkspace) {
vscode.window.showInformationMessage(
'File in the active editor is not a Go file, cannot find current package to vet'
);
return;
}
const documentUri = editor ? editor.document.uri : null;
const goConfig = getGoConfig(documentUri);
outputChannel.clear(); // Ensures stale output from vet on save is cleared
diagnosticsStatusBarItem.show();
diagnosticsStatusBarItem.text = 'Vetting...';
goVet(documentUri, goConfig, vetWorkspace)
.then((warnings) => {
handleDiagnosticErrors(editor ? editor.document : null, warnings, vetDiagnosticCollection);
diagnosticsStatusBarItem.hide();
})
.catch((err) => {
vscode.window.showInformationMessage('Error: ' + err);
diagnosticsStatusBarItem.text = 'Vetting Failed';
});
}
/**
* Runs go vet or go tool vet and presents the output in the 'Go' channel and in the diagnostic collections.
*
* @param fileUri Document uri.
* @param goConfig Configuration for the Go extension.
* @param vetWorkspace If true vets code in all workspace.
*/
export async function goVet(
fileUri: vscode.Uri,
goConfig: vscode.WorkspaceConfiguration,
vetWorkspace?: boolean
): Promise<ICheckResult[]> {
epoch++;
const closureEpoch = epoch;
if (tokenSource) {
if (running) {
tokenSource.cancel();
}
tokenSource.dispose();
}
tokenSource = new vscode.CancellationTokenSource();
const currentWorkspace = getWorkspaceFolderPath(fileUri);
const cwd = vetWorkspace && currentWorkspace ? currentWorkspace : path.dirname(fileUri.fsPath);
if (!path.isAbsolute(cwd)) {
return Promise.resolve([]);
}
const vetFlags: string[] = goConfig['vetFlags'] || [];
const vetEnv = toolExecutionEnvironment();
const args: string[] = [];
vetFlags.forEach((flag) => {
if (flag.startsWith('--vettool=') || flag.startsWith('-vettool=')) {
let vetToolPath = flag.substr(flag.indexOf('=') + 1).trim();
if (!vetToolPath) {
return;
}
vetToolPath = resolvePath(vetToolPath);
args.push(`${flag.substr(0, flag.indexOf('=') + 1)}${vetToolPath}`);
return;
}
args.push(flag);
});
const goVersion = await getGoVersion();
const tagsArg = [];
if (goConfig['buildTags'] && vetFlags.indexOf('-tags') === -1) {
tagsArg.push('-tags');
tagsArg.push(goConfig['buildTags']);
}
let vetArgs = ['vet', ...args, ...tagsArg, vetWorkspace ? './...' : '.'];
if (goVersion && goVersion.lt('1.10') && args.length) {
vetArgs = ['tool', 'vet', ...args, ...tagsArg, '.'];
}
outputChannel.appendLine(`Starting "go vet" under the folder ${cwd}`);
running = true;
return runTool(vetArgs, cwd, 'warning', true, null, vetEnv, false, tokenSource.token).then((result) => {
if (closureEpoch === epoch) {
running = false;
}
return result;
});
}
let epoch = 0;
let tokenSource: vscode.CancellationTokenSource;
let running = false;