Skip to content

Commit 66a896f

Browse files
committed
all: convert the "Go" output channel to 'log' type
The Go outputChannel was used as a place to record various commands' progress, error, and output. Go vet, lint, build and tool installation commands used this channel to record their progress. They are now more suitable in log format. Use the 'log' format output channel API (that was added a while ago). "Go: Configured Go Tools" printed the result to the output channel and this timestamped log format doesn't work well. Change the command to created an unnamed: file and output the result there. This change also helps users to audit/edit the result before sharing in their bug reports. Change-Id: I990f5bffd9ce22ec804170898e900be0de7717d4 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/559738 TryBot-Result: kokoro <[email protected]> Reviewed-by: Suzy Mueller <[email protected]> Commit-Queue: Hyang-Ah Hana Kim <[email protected]>
1 parent a8b52c1 commit 66a896f

17 files changed

+91
-91
lines changed

extension/src/commands/getConfiguredGoTools.ts

Lines changed: 53 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,14 @@ import path from 'path';
99
import { CommandFactory } from '.';
1010
import { getGoConfig, getGoplsConfig } from '../config';
1111
import { inspectGoToolVersion } from '../goInstallTools';
12-
import { outputChannel } from '../goStatus';
1312
import { getConfiguredTools } from '../goTools';
1413
import { getBinPath, getCurrentGoPath, getGoEnv, getGoVersion, getToolsGopath } from '../util';
1514
import { getEnvPath, initialEnvPath, getCurrentGoRoot } from '../utils/pathUtils';
1615

1716
export const getConfiguredGoTools: CommandFactory = () => {
1817
return async () => {
19-
outputChannel.show();
20-
outputChannel.clear();
21-
outputChannel.appendLine('Checking configured tools....');
18+
// create an untitled markdown document.
19+
const buf = [];
2220
// Tool's path search is done by getBinPathWithPreferredGopath
2321
// which searches places in the following order
2422
// 1) absolute path for the alternateTool
@@ -27,45 +25,51 @@ export const getConfiguredGoTools: CommandFactory = () => {
2725
// 4) gopath
2826
// 5) GOROOT
2927
// 6) PATH
30-
outputChannel.appendLine('GOBIN: ' + process.env['GOBIN']);
31-
outputChannel.appendLine('toolsGopath: ' + getToolsGopath());
32-
outputChannel.appendLine('gopath: ' + getCurrentGoPath());
33-
outputChannel.appendLine('GOROOT: ' + getCurrentGoRoot());
28+
buf.push('# Tools Configuration\n');
29+
buf.push('\n## Environment\n');
30+
buf.push('GOBIN: ' + process.env['GOBIN']);
31+
buf.push('toolsGopath: ' + getToolsGopath());
32+
buf.push('gopath: ' + getCurrentGoPath());
33+
buf.push('GOROOT: ' + getCurrentGoRoot());
3434
const currentEnvPath = getEnvPath();
35-
outputChannel.appendLine('PATH: ' + currentEnvPath);
35+
buf.push('PATH: ' + currentEnvPath);
3636
if (currentEnvPath !== initialEnvPath) {
37-
outputChannel.appendLine(`PATH (vscode launched with): ${initialEnvPath}`);
37+
buf.push(`PATH (vscode launched with): ${initialEnvPath}`);
3838
}
39-
outputChannel.appendLine('');
4039

41-
const goVersion = await getGoVersion();
42-
const allTools = getConfiguredTools(getGoConfig(), getGoplsConfig());
43-
const goVersionTooOld = goVersion?.lt('1.12') || false;
40+
buf.push('\n## Tools\n');
41+
try {
42+
const goVersion = await getGoVersion();
43+
const allTools = getConfiguredTools(getGoConfig(), getGoplsConfig());
44+
const goVersionTooOld = goVersion?.lt('1.18') || false;
4445

45-
outputChannel.appendLine(`\tgo:\t${goVersion?.binaryPath}: ${goVersion?.version}`);
46-
const toolsInfo = await Promise.all(
47-
allTools.map(async (tool) => {
48-
const toolPath = getBinPath(tool.name);
49-
// TODO(hyangah): print alternate tool info if set.
50-
if (!path.isAbsolute(toolPath)) {
51-
// getBinPath returns the absolute path is the tool exists.
52-
// (See getBinPathWithPreferredGopath which is called underneath)
53-
return `\t${tool.name}:\tnot installed`;
54-
}
55-
if (goVersionTooOld) {
56-
return `\t${tool.name}:\t${toolPath}: unknown version`;
57-
}
58-
const { goVersion, moduleVersion, debugInfo } = await inspectGoToolVersion(toolPath);
59-
if (goVersion || moduleVersion) {
60-
return `\t${tool.name}:\t${toolPath}\t(version: ${moduleVersion} built with go: ${goVersion})`;
61-
} else {
62-
return `\t${tool.name}:\t${toolPath}\t(version: unknown - ${debugInfo})`;
63-
}
64-
})
65-
);
66-
toolsInfo.forEach((info) => {
67-
outputChannel.appendLine(info);
68-
});
46+
buf.push(`\tgo:\t${goVersion?.binaryPath}: ${goVersion?.version}`);
47+
const toolsInfo = await Promise.all(
48+
allTools.map(async (tool) => {
49+
const toolPath = getBinPath(tool.name);
50+
// TODO(hyangah): print alternate tool info if set.
51+
if (!path.isAbsolute(toolPath)) {
52+
// getBinPath returns the absolute path is the tool exists.
53+
// (See getBinPathWithPreferredGopath which is called underneath)
54+
return `\t${tool.name}:\tnot installed`;
55+
}
56+
if (goVersionTooOld) {
57+
return `\t${tool.name}:\t${toolPath}: unknown version`;
58+
}
59+
const { goVersion, moduleVersion, debugInfo } = await inspectGoToolVersion(toolPath);
60+
if (goVersion || moduleVersion) {
61+
return `\t${tool.name}:\t${toolPath}\t(version: ${moduleVersion} built with go: ${goVersion})`;
62+
} else {
63+
return `\t${tool.name}:\t${toolPath}\t(version: unknown - ${debugInfo})`;
64+
}
65+
})
66+
);
67+
toolsInfo.forEach((info) => {
68+
buf.push(info);
69+
});
70+
} catch (e) {
71+
buf.push(`failed to get tools info: ${e}`);
72+
}
6973

7074
let folders = vscode.workspace.workspaceFolders?.map<{ name: string; path?: string }>((folder) => {
7175
return { name: folder.name, path: folder.uri.fsPath };
@@ -74,18 +78,24 @@ export const getConfiguredGoTools: CommandFactory = () => {
7478
folders = [{ name: 'no folder', path: undefined }];
7579
}
7680

77-
outputChannel.appendLine('');
78-
outputChannel.appendLine('go env');
81+
buf.push('\n## Go env\n');
7982
for (const folder of folders) {
80-
outputChannel.appendLine(`Workspace Folder (${folder.name}): ${folder.path}`);
83+
buf.push(`Workspace Folder (${folder.name}): ${folder.path}\n`);
8184
try {
8285
const out = await getGoEnv(folder.path);
8386
// Append '\t' to the beginning of every line (^) of 'out'.
8487
// 'g' = 'global matching', and 'm' = 'multi-line matching'
85-
outputChannel.appendLine(out.replace(/^/gm, '\t'));
88+
buf.push(out.replace(/^/gm, '\t'));
8689
} catch (e) {
87-
outputChannel.appendLine(`failed to run 'go env': ${e}`);
90+
buf.push(`failed to run 'go env': ${e}`);
8891
}
8992
}
93+
94+
// create a new untitled document
95+
const doc = await vscode.workspace.openTextDocument({
96+
content: buf.join('\n'),
97+
language: 'markdown'
98+
});
99+
await vscode.window.showTextDocument(doc);
90100
};
91101
};

extension/src/commands/startLanguageServer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,19 +110,19 @@ function shouldActivateLanguageFeatures() {
110110
for (const folder of vscode.workspace.workspaceFolders || []) {
111111
switch (folder.uri.scheme) {
112112
case 'vsls':
113-
outputChannel.appendLine(
113+
outputChannel.error(
114114
'Language service on the guest side is disabled. ' +
115115
'The server-side language service will provide the language features.'
116116
);
117117
return;
118118
case 'ssh':
119-
outputChannel.appendLine('The language server is not supported for SSH. Disabling it.');
119+
outputChannel.error('The language server is not supported for SSH. Disabling it.');
120120
return;
121121
}
122122
}
123123
const schemes = vscode.workspace.workspaceFolders?.map((folder) => folder.uri.scheme);
124124
if (schemes && schemes.length > 0 && !schemes.includes('file') && !schemes.includes('untitled')) {
125-
outputChannel.appendLine(
125+
outputChannel.error(
126126
`None of the folders in this workspace ${schemes.join(
127127
','
128128
)} are the types the language server recognizes. Disabling the language features.`

extension/src/goBuild.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function buildCode(buildWorkspace?: boolean): CommandFactory {
4545
const documentUri = editor?.document.uri;
4646
const goConfig = getGoConfig(documentUri);
4747

48-
outputChannel.clear(); // Ensures stale output from build on save is cleared
4948
diagnosticsStatusBarItem.show();
5049
diagnosticsStatusBarItem.text = 'Building...';
5150

@@ -156,7 +155,7 @@ export async function goBuild(
156155
if (currentGoWorkspace && !isMod) {
157156
importPath = cwd.substr(currentGoWorkspace.length + 1);
158157
} else {
159-
outputChannel.appendLine(
158+
outputChannel.error(
160159
`Not able to determine import path of current package by using cwd: ${cwd} and Go workspace: ${currentGoWorkspace}`
161160
);
162161
}

extension/src/goCheck.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export function check(
6161
goConfig: vscode.WorkspaceConfiguration
6262
): Promise<IToolCheckResults[]> {
6363
diagnosticsStatusBarItem.hide();
64-
outputChannel.clear();
64+
outputChannel.appendLine('Running checks...');
6565
const runningToolsPromises = [];
6666
const cwd = path.dirname(fileUri.fsPath);
6767

extension/src/goEnvironmentStatus.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,6 @@ async function downloadGo(goOption: GoEnvironmentOption): Promise<GoEnvironmentO
218218
location: vscode.ProgressLocation.Notification
219219
},
220220
async () => {
221-
outputChannel.clear();
222-
outputChannel.show();
223221
outputChannel.appendLine(`go install ${goOption.binpath}@latest`);
224222
const result = await installTool({
225223
name: newExecutableName,
@@ -229,7 +227,7 @@ async function downloadGo(goOption: GoEnvironmentOption): Promise<GoEnvironmentO
229227
isImportant: false
230228
});
231229
if (result) {
232-
outputChannel.appendLine(`Error installing ${goOption.binpath}: ${result}`);
230+
outputChannel.error(`Error installing ${goOption.binpath}: ${result}`);
233231
throw new Error('Could not install ${goOption.binpath}');
234232
}
235233
// run `goX.X download`
@@ -238,7 +236,7 @@ async function downloadGo(goOption: GoEnvironmentOption): Promise<GoEnvironmentO
238236
try {
239237
await execFile(goXExecutable, ['download']);
240238
} catch (downloadErr) {
241-
outputChannel.appendLine(`Error finishing installation: ${downloadErr}`);
239+
outputChannel.error(`Error finishing installation: ${downloadErr}`);
242240
throw new Error('Could not download Go version.');
243241
}
244242

@@ -253,11 +251,11 @@ async function downloadGo(goOption: GoEnvironmentOption): Promise<GoEnvironmentO
253251
sdkPath = stdout.trim();
254252
}
255253
} catch (downloadErr) {
256-
outputChannel.appendLine(`Error finishing installation: ${downloadErr}`);
254+
outputChannel.error(`Error finishing installation: ${downloadErr}`);
257255
throw new Error('Could not download Go version.');
258256
}
259257
if (!sdkPath || !(await dirExists(sdkPath))) {
260-
outputChannel.appendLine(`SDK path does not exist: ${sdkPath}`);
258+
outputChannel.error(`SDK path does not exist: ${sdkPath}`);
261259
throw new Error(`SDK path does not exist: ${sdkPath}`);
262260
}
263261

extension/src/goGenerateTests.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ function generateTests(
202202
return resolve(false);
203203
}
204204
if (err) {
205-
console.log(err);
206-
outputChannel.appendLine(err.message);
205+
outputChannel.error(err.message);
207206
return reject('Cannot generate test due to errors');
208207
}
209208

extension/src/goGetPackage.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ export const goGetPackage: CommandFactory = (ctx, goCtx) => () => {
3535
cp.execFile(goRuntimePath, ['get', '-v', importPath], { env }, (err, stdout, stderr) => {
3636
// go get -v uses stderr to write output regardless of success or failure
3737
if (stderr !== '') {
38-
outputChannel.show();
39-
outputChannel.clear();
40-
outputChannel.appendLine(stderr);
38+
outputChannel.error(stderr);
4139
buildCode(false)(ctx, goCtx)();
4240
return;
4341
}

extension/src/goInstall.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ export const installCurrentPackage: CommandFactory = () => async () => {
5858
const importPath = currentGoWorkspace && !isMod ? cwd.substr(currentGoWorkspace.length + 1) : '.';
5959
args.push(importPath);
6060

61-
outputChannel.clear();
62-
outputChannel.show();
6361
outputChannel.appendLine(`Installing ${importPath === '.' ? 'current package' : importPath}`);
6462

6563
cp.execFile(goRuntimePath, args, { env, cwd }, (err, stdout, stderr) => {
66-
outputChannel.appendLine(err ? `Installation failed: ${stderr}` : 'Installation successful');
64+
if (err) {
65+
outputChannel.error(`Installation failed: ${stderr}`);
66+
} else {
67+
outputChannel.appendLine('Installation successful');
68+
}
6769
});
6870
};

extension/src/goInstallTools.ts

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ async function getGoForInstall(goVersion: GoVersion, silent?: boolean): Promise<
119119
logError(`getGoForInstall failed to run 'go version' with the configured go for tool install: ${e}`);
120120
} finally {
121121
if (!silent) {
122-
outputChannel.appendLine(
122+
outputChannel.error(
123123
`Ignoring misconfigured 'go.toolsManagement.go' (${configured}). Provide a valid path to the Go command.`
124124
);
125125
}
@@ -152,10 +152,7 @@ export async function installTools(
152152
return [];
153153
}
154154
const { silent, skipRestartGopls } = options || {};
155-
if (!silent) {
156-
outputChannel.show();
157-
}
158-
outputChannel.clear();
155+
outputChannel.appendLine('Installing tools...');
159156

160157
const goForInstall = await getGoForInstall(goVersion);
161158

@@ -222,9 +219,9 @@ export async function installTools(
222219
if (silent) {
223220
outputChannel.show();
224221
}
225-
outputChannel.appendLine(failures.length + ' tools failed to install.\n');
222+
outputChannel.error(failures.length + ' tools failed to install.\n');
226223
for (const failure of failures) {
227-
outputChannel.appendLine(`${failure.tool.name}: ${failure.reason} `);
224+
outputChannel.error(`${failure.tool.name}: ${failure.reason} `);
228225
}
229226
}
230227
if (missing.some((tool) => tool.isImportant)) {
@@ -280,8 +277,8 @@ async function installToolWithGo(
280277
const toolInstallPath = getBinPath(tool.name);
281278
outputChannel.appendLine(`Installing ${importPath} (${toolInstallPath}) SUCCEEDED`);
282279
} catch (e) {
283-
outputChannel.appendLine(`Installing ${importPath} FAILED`);
284-
outputChannel.appendLine(`${JSON.stringify(e, null, 1)}`);
280+
outputChannel.error(`Installing ${importPath} FAILED`);
281+
outputChannel.error(`${JSON.stringify(e, null, 1)}`);
285282
return `failed to install ${tool.name}(${importPath}): ${e}`;
286283
}
287284
}
@@ -484,7 +481,7 @@ export function updateGoVarsFromConfig(goCtx: GoExtensionContext): Promise<void>
484481
if (stderr) {
485482
// 'go env' may output warnings about potential misconfiguration.
486483
// Show the messages to users but keep processing the stdout.
487-
outputChannel.append(`'${goRuntimePath} env': ${stderr}`);
484+
outputChannel.error(`'${goRuntimePath} env': ${stderr}`);
488485
outputChannel.show();
489486
}
490487
logVerbose(`${goRuntimePath} env ...:\n${stdout}`);

extension/src/goLint.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function lintCode(scope?: string): CommandFactory {
3535
const goConfig = getGoConfig(documentUri);
3636
const goplsConfig = getGoplsConfig(documentUri);
3737

38-
outputChannel.clear(); // Ensures stale output from lint on save is cleared
38+
outputChannel.appendLine('Linting...');
3939
diagnosticsStatusBarItem.show();
4040
diagnosticsStatusBarItem.text = 'Linting...';
4141

0 commit comments

Comments
 (0)