This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 645
/
Copy pathgoBuild.ts
119 lines (104 loc) · 4.26 KB
/
goBuild.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
import path = require('path');
import vscode = require('vscode');
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath } from './util';
import { outputChannel } from './goStatus';
import os = require('os');
import { getNonVendorPackages } from './goPackages';
import { getTestFlags } from './testUtils';
import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
import { diagnosticsStatusBarItem } from './goStatus';
/**
* Builds current package or workspace.
*/
export function buildCode(buildWorkspace?: boolean) {
let editor = vscode.window.activeTextEditor;
if (!buildWorkspace) {
if (!editor) {
vscode.window.showInformationMessage('No editor is active, cannot find current package to build');
return;
}
if (editor.document.languageId !== 'go') {
vscode.window.showInformationMessage('File in the active editor is not a Go file, cannot find current package to build');
return;
}
}
let documentUri = editor ? editor.document.uri : null;
let goConfig = vscode.workspace.getConfiguration('go', documentUri);
outputChannel.clear(); // Ensures stale output from build on save is cleared
diagnosticsStatusBarItem.show();
diagnosticsStatusBarItem.text = 'Building...';
goBuild(documentUri, goConfig, buildWorkspace)
.then(errors => {
handleDiagnosticErrors(editor ? editor.document : null, errors, vscode.DiagnosticSeverity.Error);
diagnosticsStatusBarItem.hide();
})
.catch(err => {
vscode.window.showInformationMessage('Error: ' + err);
diagnosticsStatusBarItem.text = 'Build Failed';
});
}
/**
* Runs go build -i or go test -i 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 buildWorkspace If true builds code in all workspace.
*/
export function goBuild(fileUri: vscode.Uri, goConfig: vscode.WorkspaceConfiguration, buildWorkspace?: boolean): Promise<ICheckResult[]> {
const currentWorkspace = getWorkspaceFolderPath(fileUri);
const cwd = (buildWorkspace && currentWorkspace) ? currentWorkspace : path.dirname(fileUri.fsPath);
if (!path.isAbsolute(cwd)) {
return Promise.resolve([]);
}
const buildEnv = Object.assign({}, getToolsEnvVars());
const tmpPath = path.normalize(path.join(os.tmpdir(), 'go-code-check'));
const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go');
const buildFlags: string[] = isTestFile ? getTestFlags(goConfig, null) : (Array.isArray(goConfig['buildFlags']) ? [...goConfig['buildFlags']] : []);
// Remove the -i flag as it will be added later anyway
if (buildFlags.indexOf('-i') > -1) {
buildFlags.splice(buildFlags.indexOf('-i'), 1);
}
// If current file is a test file, then use `go test -c` instead of `go build` to find build errors
let buildArgs: string[] = isTestFile ? ['test', '-c'] : ['build'];
buildArgs.push('-i', '-o', tmpPath, ...buildFlags);
if (goConfig['buildTags'] && buildFlags.indexOf('-tags') === -1) {
buildArgs.push('-tags');
buildArgs.push(goConfig['buildTags']);
}
if (buildWorkspace && currentWorkspace && !isTestFile) {
return getNonVendorPackages(currentWorkspace).then(pkgs => {
let buildPromises = [];
buildPromises = pkgs.map(pkgPath => {
return runTool(
buildArgs.concat(pkgPath),
currentWorkspace,
'error',
true,
null,
buildEnv,
true
);
});
return Promise.all(buildPromises).then((resultSets) => {
let results: ICheckResult[] = [].concat.apply([], resultSets);
// Filter duplicates
return results.filter((results, index, self) =>
self.findIndex((t) => {
return t.file === results.file && t.line === results.line && t.msg === results.msg && t.severity === results.severity;
}) === index);
});
});
}
// Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846
let currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd);
let importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.';
return runTool(
buildArgs.concat(importPath),
cwd,
'error',
true,
null,
buildEnv,
true
);
}