Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Run on-save tools for entire workspace #1023

Merged
merged 7 commits into from
Jun 8, 2017
Merged
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
35 changes: 25 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,14 @@
"title": "Go configuration",
"properties": {
"go.buildOnSave": {
"type": "boolean",
"default": true,
"description": "Run 'go build'/'go test -c' on save."
"type": "string",
"enum": [
"package",
"workspace",
"off"
],
"default": "package",
"description": "Run 'go build'/'go test -c' on save on the current workspace, current package or nothing."
},
"go.buildFlags": {
"type": "array",
Expand All @@ -371,9 +376,14 @@
"description": "The Go build tags to use for all commands that support a `-tags '...'` argument"
},
"go.lintOnSave": {
"type": "boolean",
"default": true,
"description": "Run Lint tool on save."
"type": "string",
"enum": [
"package",
"workspace",
"off"
],
"default": "package",
"description": "Run Lint tool on save on current workspace, current package or nothing."
},
"go.lintTool": {
"type": "string",
Expand All @@ -393,17 +403,22 @@
"description": "Flags to pass to Lint tool (e.g. [\"-min_confidence=.8\"])"
},
"go.vetOnSave": {
"type": "boolean",
"default": true,
"description": "Run 'go tool vet' on save."
"type": "string",
"enum": [
"package",
"workspace",
"off"
],
"default": "package",
"description": "Run 'go vet' on save on current workspace, current package or nothing."
},
"go.vetFlags": {
"type": "array",
"items": {
"type": "string"
},
"default": [],
"description": "Flags to pass to `go tool vet` (e.g. ['-all', '-shadow'])"
"description": "Flags to pass to `go vet` (e.g. ['-all', '-shadow'])"
},
"go.formatOnSave": {
"type": "boolean",
Expand Down
50 changes: 36 additions & 14 deletions src/goCheck.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,20 +142,27 @@ export function check(filename: string, goConfig: vscode.WorkspaceConfiguration)
return testPromise;
};

if (!!goConfig['buildOnSave']) {
if (!!goConfig['buildOnSave'] && goConfig['buildOnSave'] !== 'off') {
let buildFlags = goConfig['buildFlags'] || [];
let buildTags = '"' + goConfig['buildTags'] + '"';
let tmppath = path.normalize(path.join(os.tmpdir(), 'go-code-check'));
let currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(cwd);
let importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.';
let args = ['build', '-i', '-o', tmppath, '-tags', buildTags, ...buildFlags, importPath];
if (filename.match(/_test.go$/i)) {
args = ['test', '-i', '-copybinary', '-o', tmppath, '-c', '-tags', buildTags, ...buildFlags, importPath];

let tmpPath = path.normalize(path.join(os.tmpdir(), 'go-code-check'));

let buildWorkDir = cwd;
let buildArgs: string[];

if (goConfig['buildOnSave'] === 'workspace') {
buildWorkDir = vscode.workspace.rootPath;
buildArgs = ['test', '-run=^$', '-tags', buildTags, ...buildFlags, './...'];
} else if (filename.match(/_test.go$/i)) {
buildArgs = ['test', '-c', '-copybinary', '-o', tmpPath, '-tags', buildTags, ...buildFlags];
} else {
buildArgs = ['build', '-i', '-o', tmpPath, '-tags', buildTags, ...buildFlags];
}

runningToolsPromises.push(runTool(
args,
cwd,
buildArgs,
buildWorkDir,
'error',
true,
null,
Expand All @@ -179,7 +186,7 @@ export function check(filename: string, goConfig: vscode.WorkspaceConfiguration)
});
}

if (!!goConfig['lintOnSave']) {
if (!!goConfig['lintOnSave'] && goConfig['lintOnSave'] !== 'off') {
let lintTool = goConfig['lintTool'] || 'golint';
let lintFlags: string[] = goConfig['lintFlags'] || [];

Expand All @@ -202,21 +209,36 @@ export function check(filename: string, goConfig: vscode.WorkspaceConfiguration)
args.push('--aggregate');
}

let lintWorkDir = cwd;

if (goConfig['lintOnSave'] === 'workspace') {
args.push('./...');
lintWorkDir = vscode.workspace.rootPath;
}

runningToolsPromises.push(runTool(
args,
cwd,
lintWorkDir,
'warning',
false,
lintTool,
env
));
}

if (!!goConfig['vetOnSave']) {
if (!!goConfig['vetOnSave'] && goConfig['vetOnSave'] !== 'off') {
let vetFlags = goConfig['vetFlags'] || [];
let vetArgs = ['vet', ...vetFlags];
let vetWorkDir = cwd;

if (goConfig['vetOnSave'] === 'workspace') {
vetArgs.push('./...');
vetWorkDir = vscode.workspace.rootPath;
}

runningToolsPromises.push(runTool(
['tool', 'vet', ...vetFlags, filename],
cwd,
vetArgs,
vetWorkDir,
'warning',
true,
null,
Expand Down