Skip to content

Commit 6b7a2b6

Browse files
h9jianggopherbot
authored andcommitted
extension/src: getFormatTool return "customFormatter" instead of path
Previously, getFormatTool returns the path specified in go.alternateTools, but the tool existence check is expecting a tool name not a tool path. Instead of returning a path, getFormatTool can return the "customFormatter" string. The getBinPath function will resolve the path. Validate config also throw error when formatTool is set to custom but formatter's path is not provided in the alternateTools. [bug fix] tool existence check should be triggered under all condition regardless of whether user is using language server. Fix #3906 Change-Id: I9d328c2c63064119d48cb42b8774e144c6281a36 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/717405 Reviewed-by: Madeline Kalil <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent 36d95e2 commit 6b7a2b6

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

extension/src/config.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ This will result in duplicate diagnostics.`;
103103

104104
// Format tool check.
105105
const formatTool = getFormatTool(goConfig);
106+
if (formatTool === 'customFormatter') {
107+
const alternateTools = goConfig.get<any>('alternateTools');
108+
if (alternateTools === undefined || alternateTools['customFormatter'] === undefined) {
109+
const message =
110+
"Error: formatter is configured to custom (go.formatTool=custom) but custom formatter path is not provided in go.alternateTools['customFormatter']";
111+
112+
const selected = await vscode.window.showWarningMessage(message, 'Open Settings');
113+
if (selected === 'Open Settings') {
114+
vscode.commands.executeCommand('workbench.action.openSettingsJson');
115+
}
116+
}
117+
}
118+
106119
if (formatTool !== '' && goplsConfig['formatting.gofumpt'] === true) {
107120
const message = `Warning: formatter is configured to run from both client side (go.formatTool=${formatTool}) and server side (gopls.formatting.gofumpt=true).
108121
This may result in double formatting.`;

extension/src/goMain.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,7 @@ export function addConfigChangeListener(ctx: vscode.ExtensionContext) {
312312
}
313313

314314
if (e.affectsConfiguration('go.formatTool')) {
315-
const tool = getFormatTool(goConfig);
316-
// When language server gopls is active (by default), existence
317-
// checks are skipped only for tools that gopls replaces. Other
318-
// tools are still checked.
319-
if (goConfig['useLanguageServer'] === false || !new Set(['gofmt', 'goimports', 'goformat']).has(tool)) {
320-
checkToolExists(tool);
321-
}
315+
checkToolExists(getFormatTool(goConfig));
322316
}
323317
if (e.affectsConfiguration('go.docsTool')) {
324318
checkToolExists(goConfig['docsTool']);

extension/src/language/legacy/goFormat.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import vscode = require('vscode');
1212
import { getGoConfig } from '../../config';
1313
import { toolExecutionEnvironment } from '../../goEnv';
1414
import { promptForMissingTool, promptForUpdatingTool } from '../../goInstallTools';
15-
import { getBinPath, resolvePath } from '../../util';
15+
import { getBinPath } from '../../util';
1616
import { killProcessTree } from '../../utils/processUtils';
1717

1818
/**
@@ -130,8 +130,8 @@ export class GoDocumentFormattingEditProvider implements vscode.DocumentFormatti
130130
* getFormatTool returns the formatter tool configured through the "go.formatTool"
131131
* setting.
132132
*
133-
* If "go.formatTool" is set to "custom", it returns the "customFormatter"
134-
* specified in "go.alternateTools".
133+
* If "go.formatTool" is set to "custom", it returns "customFormatter". User
134+
* should specify "customFormatter" in setting "go.alternateTools".
135135
*
136136
* If "go.formatTool" is not set, it returns an empty string, indicating that
137137
* no specific format tool is selected and gopls should be used.
@@ -143,7 +143,7 @@ export function getFormatTool(goConfig: vscode.WorkspaceConfiguration): string {
143143
return ''; // not specified, yield to gopls by return empty string.
144144
}
145145
if (formatTool === 'custom') {
146-
return resolvePath(goConfig['alternateTools']['customFormatter']);
146+
return 'customFormatter';
147147
}
148148
return formatTool;
149149
}

extension/test/gopls/formatting.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ fmt.Println("hello")
155155
const config = require('../../src/config');
156156
sandbox.stub(config, 'getGoConfig').returns(goConfig);
157157

158+
// Disable path resolver's cache.
159+
const util = require('../../src/util');
160+
sandbox.stub(util, 'getBinPath').callsFake((...args: any[]) => {
161+
const [tool] = args;
162+
return util.getBinPathWithExplanation(tool, false).binPath;
163+
});
164+
158165
// Create formatter script under tool dir and make it executable.
159166
if (tc.formatter && tc.formatterScript) {
160167
fs.writeFileSync(path.join(toolPath, tc.formatter), tc.formatterScript);

extension/test/integration/configuration.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,25 @@ suite('Configuration validation tests', () => {
107107
expectWarning: false
108108
},
109109
// Format tool setting tests.
110+
{
111+
name: 'Error: formatTool is custom but path not provided',
112+
goConfig: new Map<string, any>([
113+
['useLanguageServer', true],
114+
['formatTool', 'custom']
115+
]),
116+
expectWarning: true,
117+
warningMessage:
118+
"Error: formatter is configured to custom (go.formatTool=custom) but custom formatter path is not provided in go.alternateTools['customFormatter']"
119+
},
120+
{
121+
name: 'Happy path: formatTool is custom and path is provided',
122+
goConfig: new Map<string, any>([
123+
['useLanguageServer', true],
124+
['formatTool', 'custom'],
125+
['alternateTools', { customFormatter: 'foo' }]
126+
]),
127+
expectWarning: false
128+
},
110129
{
111130
name: 'Conflict: formatTool is gofumpt and gopls gofumpt setting is true',
112131
suggested: true,

0 commit comments

Comments
 (0)