Skip to content

Commit 7bc14ec

Browse files
committed
src/goStatus: add go.work to status bar quickpick
This adds an item to go to the go.work file if available. Updates #2032 Change-Id: Ib521cc0ea648d84ac34223d55ebcd881b43800cd Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/386214 Trust: Suzy Mueller <[email protected]> Run-TryBot: Suzy Mueller <[email protected]> Trust: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> TryBot-Result: kokoro <[email protected]>
1 parent 16eadfa commit 7bc14ec

File tree

2 files changed

+31
-21
lines changed

2 files changed

+31
-21
lines changed

src/goModules.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,24 @@ import { getBinPath, getGoVersion, getModuleCache, getWorkspaceFolderPath } from
1919
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/pathUtils';
2020
export let GO111MODULE: string;
2121

22-
async function runGoModEnv(folderPath: string): Promise<string> {
22+
export async function runGoEnv(folderPath: string, envvars: string[]): Promise<any> {
2323
const goExecutable = getBinPath('go');
2424
if (!goExecutable) {
2525
console.warn(
2626
`Failed to run "go env GOMOD" to find mod file as the "go" binary cannot be found in either GOROOT(${getCurrentGoRoot()}) or PATH(${envPath})`
2727
);
28-
return;
28+
return {};
2929
}
3030
const env = toolExecutionEnvironment();
3131
GO111MODULE = env['GO111MODULE'];
3232
return new Promise((resolve) => {
33-
cp.execFile(goExecutable, ['env', 'GOMOD'], { cwd: folderPath, env }, (err, stdout) => {
33+
const args = ['env', '-json'].concat(envvars);
34+
cp.execFile(goExecutable, args, { cwd: folderPath, env }, (err, stdout) => {
3435
if (err) {
35-
console.warn(`Error when running go env GOMOD: ${err}`);
36-
return resolve('');
36+
console.warn(`Error when running go env ${args}: ${err}`);
37+
return resolve({});
3738
}
38-
const [goMod] = stdout.split('\n');
39-
if (goMod === '/dev/null' || goMod === 'NUL') resolve('');
40-
else resolve(goMod);
39+
resolve(JSON.parse(stdout));
4140
});
4241
});
4342
}
@@ -65,7 +64,9 @@ export async function getModFolderPath(fileuri: vscode.Uri, isDir?: boolean): Pr
6564
return;
6665
}
6766

68-
let goModEnvResult = await runGoModEnv(pkgPath);
67+
const goModEnvJSON = await runGoEnv(pkgPath, ['GOMOD']);
68+
let goModEnvResult =
69+
goModEnvJSON['GOMOD'] === '/dev/null' || goModEnvJSON['GOMOD'] === 'NUL' ? '' : goModEnvJSON['GOMOD'];
6970
if (goModEnvResult) {
7071
goModEnvResult = path.dirname(goModEnvResult);
7172
const goConfig = getGoConfig(fileuri);

src/goStatus.ts

+21-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
serverOutputChannel
1919
} from './goLanguageServer';
2020
import { isGoFile } from './goMode';
21-
import { getModFolderPath, isModSupported } from './goModules';
21+
import { isModSupported, runGoEnv } from './goModules';
2222
import { allToolsInformation } from './goToolsInformation';
2323
import { getGoVersion } from './util';
2424

@@ -34,22 +34,26 @@ diagnosticsStatusBarItem.name = STATUS_BAR_ITEM_NAME;
3434
// statusbar item for switching the Go environment
3535
export let goEnvStatusbarItem: vscode.StatusBarItem;
3636

37-
let modulePath: string;
37+
let gomod: string;
38+
let gowork: string;
3839
export const languageServerIcon = '$(zap)';
3940
export const languageServerErrorIcon = '$(warning)';
4041

41-
export function updateGoStatusBar(editor: vscode.TextEditor) {
42+
export async function updateGoStatusBar(editor: vscode.TextEditor) {
4243
// Only update the module path if we are in a Go file.
4344
// This allows the user to open output windows without losing
4445
// the go.mod information in the status bar.
4546
if (!!editor && isGoFile(editor.document)) {
46-
isModSupported(editor.document.uri).then((isMod) => {
47-
if (isMod) {
48-
getModFolderPath(editor.document.uri).then((p) => (modulePath = p));
49-
} else {
50-
modulePath = '';
51-
}
52-
});
47+
const isMod = await isModSupported(editor.document.uri);
48+
if (isMod) {
49+
runGoEnv(path.dirname(editor.document.uri.fsPath), ['GOMOD', 'GOWORK']).then((p) => {
50+
gomod = p['GOMOD'] === '/dev/null' || p['GOMOD'] === 'NUL' ? '' : p['GOMOD'];
51+
gowork = p['GOWORK'];
52+
});
53+
} else {
54+
gomod = '';
55+
gowork = '';
56+
}
5357
}
5458
}
5559

@@ -74,8 +78,12 @@ export async function expandGoStatusBar() {
7478
}
7579

7680
// If modules is enabled, add link to mod file
77-
if (modulePath) {
78-
options.push({ label: "Open 'go.mod'", description: path.join(modulePath, 'go.mod') });
81+
if (gomod) {
82+
options.push({ label: "Open 'go.mod'", description: gomod });
83+
}
84+
85+
if (gowork) {
86+
options.push({ label: "Open 'go.work'", description: gowork });
7987
}
8088

8189
vscode.window.showQuickPick(options).then((item) => {
@@ -95,6 +103,7 @@ export async function expandGoStatusBar() {
95103
case 'Install Go Language Server':
96104
vscode.commands.executeCommand('go.tools.install', [allToolsInformation['gopls']]);
97105
break;
106+
case "Open 'go.work'":
98107
case "Open 'go.mod'":
99108
const openPath = vscode.Uri.file(item.description);
100109
vscode.workspace.openTextDocument(openPath).then((doc) => {

0 commit comments

Comments
 (0)