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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add build package and workspace cmds
- Loading branch information
1 parent
951a72b
commit 97ffefc
Showing
4 changed files
with
123 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
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'; | ||
|
||
/** | ||
* Builds current package or workspace. | ||
*/ | ||
export function buildCode(buildWorkspace?: boolean) { | ||
let editor = vscode.window.activeTextEditor; | ||
if (!editor && !buildWorkspace) { | ||
vscode.window.showInformationMessage('No editor is active, cant find current package to build'); | ||
return; | ||
} | ||
|
||
let documentUri = editor ? editor.document.uri : null; | ||
let goConfig = vscode.workspace.getConfiguration('go', documentUri); | ||
outputChannel.clear(); | ||
goBuild(documentUri, goConfig, buildWorkspace) | ||
.then(errors => handleDiagnosticErrors(editor ? editor.document : null, errors, vscode.DiagnosticSeverity.Error)) | ||
.catch(err => { | ||
vscode.window.showInformationMessage('Error: ' + err); | ||
}); | ||
} | ||
|
||
/** | ||
* 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 buildEnv = Object.assign({}, getToolsEnvVars()); | ||
const currentWorkspace = getWorkspaceFolderPath(fileUri); | ||
const cwd = path.dirname(fileUri.fsPath); | ||
const tmpPath = path.normalize(path.join(os.tmpdir(), 'go-code-check')); | ||
const isTestFile = fileUri.fsPath.endsWith('_test.go'); | ||
|
||
let buildFlags = isTestFile ? getTestFlags(goConfig, null) : (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; | ||
This comment has been minimized.
Sorry, something went wrong. |
||
}) === 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 | ||
); | ||
|
||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This could be rather inefficient if there are a large number of results since it's going to be O(N^2).
It seems like the duplicates here are likely a result of it calling
go build
individually for each package, so compilation errors in shared dependencies will be repeated.As I noted in #1890 it seems like you could pass the list of packages to a single call of
go build
instead. Then you should only get one set of results back for the whole list of packages that shouldn't contain the duplication.