Skip to content

Commit

Permalink
package.json: add command to run 'go mod init'
Browse files Browse the repository at this point in the history
When users start a new Go project, one of the first things they need
to do is initialize their go.mod. This is required for debugging and
other tools to work correctly. Currently users have to navigate to
the terminal and run the command there. This adds a command that runs
in the workspace root, allowing users to initialize go.mod from the
command prompt instead.

Fixes golang#1449

Change-Id: Ie3daf565770402fe79f8720dd3fca8cf4ab01fa8
Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/312092
Trust: Suzy Mueller <[email protected]>
Run-TryBot: Suzy Mueller <[email protected]>
TryBot-Result: kokoro <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
suzmue committed Apr 22, 2021
1 parent 5bcfcac commit 2875e05
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ Build the current workspace.

Install the current package.

### `Go: Initialize go.mod`

Run `go mod init` in the workspace folder.

### `Go: Cancel Running Tests`

Cancels running tests.
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"onCommand:go.tools.install",
"onCommand:go.locate.tools",
"onCommand:go.show.commands",
"onCommand:go.run.modinit",
"onDebugInitialConfigurations",
"onDebugResolve:go",
"onWebviewPanel:welcomeGo"
Expand Down Expand Up @@ -367,6 +368,11 @@
"title": "Go: Install Current Package",
"description": "Install the current package."
},
{
"command": "go.run.modinit",
"title": "Go: Initialize go.mod",
"description": "Run `go mod init` in the workspace folder."
},
{
"command": "go.test.cancel",
"title": "Go: Cancel Running Tests",
Expand Down
4 changes: 3 additions & 1 deletion src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import { lintCode } from './goLint';
import { logVerbose, setLogConfig } from './goLogging';
import { GO_MODE } from './goMode';
import { addTags, removeTags } from './goModifytags';
import { GO111MODULE, isModSupported } from './goModules';
import { GO111MODULE, goModInit, isModSupported } from './goModules';
import { playgroundCommand } from './goPlayground';
import { GoReferencesCodeLensProvider } from './goReferencesCodelens';
import { GoRunTestCodeLensProvider } from './goRunTestCodelens';
Expand Down Expand Up @@ -586,6 +586,8 @@ If you would like additional configuration for diagnostics from gopls, please se

ctx.subscriptions.push(vscode.commands.registerCommand('go.install.package', installCurrentPackage));

ctx.subscriptions.push(vscode.commands.registerCommand('go.run.modinit', goModInit));

ctx.subscriptions.push(
vscode.commands.registerCommand('go.extractServerChannel', () => {
showServerOutputChannel();
Expand Down
27 changes: 25 additions & 2 deletions src/goModules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@

import cp = require('child_process');
import path = require('path');
import util = require('util');
import vscode = require('vscode');
import { getGoConfig } from './config';
import { toolExecutionEnvironment } from './goEnv';
import { getFormatTool } from './goFormat';
import { installTools } from './goInstallTools';
import { outputChannel } from './goStatus';
import { getTool } from './goTools';
import { getFromGlobalState, updateGlobalState } from './stateUtils';
import { getBinPath, getGoVersion, getModuleCache } from './util';
import { getBinPath, getGoVersion, getModuleCache, getWorkspaceFolderPath } from './util';
import { envPath, fixDriveCasingInWindows, getCurrentGoRoot } from './utils/pathUtils';

export let GO111MODULE: string;

async function runGoModEnv(folderPath: string): Promise<string> {
Expand Down Expand Up @@ -184,3 +185,25 @@ export async function getCurrentPackage(cwd: string): Promise<string> {
});
});
}

export async function goModInit() {
outputChannel.clear();

const moduleName = await vscode.window.showInputBox({
prompt: 'Enter module name',
value: '',
placeHolder: 'example.com/m'
});

const goRuntimePath = getBinPath('go');
const execFile = util.promisify(cp.execFile);
try {
const env = toolExecutionEnvironment();
const cwd = getWorkspaceFolderPath();
await execFile(goRuntimePath, ['mod', 'init', moduleName], { env, cwd });
} catch (e) {
outputChannel.appendLine(e);
outputChannel.show();
vscode.window.showErrorMessage(`Error running 'go mod init ${moduleName}': See Go output channel for details`);
}
}

0 comments on commit 2875e05

Please sign in to comment.