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

Commit 15f571e

Browse files
committed
Build, Install, Debug when using modules under GOPATH #2238
1 parent 058eccf commit 15f571e

3 files changed

Lines changed: 20 additions & 8 deletions

File tree

src/debugAdapter/goDebug.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ class Delve {
395395
let dlvArgs = [mode || 'debug'];
396396
if (mode === 'exec') {
397397
dlvArgs = dlvArgs.concat([program]);
398-
} else if (currentGOWorkspace) {
398+
} else if (currentGOWorkspace && env['GO111MODULE'] !== 'on') {
399399
dlvArgs = dlvArgs.concat([dirname.substr(currentGOWorkspace.length + 1)]);
400400
}
401401
dlvArgs = dlvArgs.concat(['--headless=true', '--listen=' + host + ':' + port.toString()]);

src/goBuild.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import path = require('path');
22
import vscode = require('vscode');
3-
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getTempFilePath } from './util';
3+
import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getTempFilePath, getModuleCache } from './util';
44
import { outputChannel } from './goStatus';
5-
import os = require('os');
65
import { getNonVendorPackages } from './goPackages';
76
import { getTestFlags } from './testUtils';
87
import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
@@ -53,7 +52,7 @@ export function buildCode(buildWorkspace?: boolean) {
5352
* @param goConfig Configuration for the Go extension.
5453
* @param buildWorkspace If true builds code in all workspace.
5554
*/
56-
export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.WorkspaceConfiguration, buildWorkspace?: boolean): Promise<ICheckResult[]> {
55+
export async function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.WorkspaceConfiguration, buildWorkspace?: boolean): Promise<ICheckResult[]> {
5756
epoch++;
5857
let closureEpoch = epoch;
5958
if (tokenSource) {
@@ -74,6 +73,11 @@ export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.Wo
7473
return Promise.resolve([]);
7574
}
7675

76+
// Skip building if cwd is in the module cache
77+
if (isMod && cwd.startsWith(getModuleCache())) {
78+
return [];
79+
}
80+
7781
const buildEnv = Object.assign({}, getToolsEnvVars());
7882
const tmpPath = getTempFilePath('go-code-check');
7983
const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go');
@@ -115,7 +119,7 @@ export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.Wo
115119

116120
// Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846
117121
let currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd);
118-
let importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.';
122+
let importPath = (currentGoWorkspace && !isMod) ? cwd.substr(currentGoWorkspace.length + 1) : '.';
119123
running = true;
120124
outputChannel.appendLine(`Starting building the current package at ${cwd}`);
121125
return runTool(

src/goInstall.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import path = require('path');
22
import vscode = require('vscode');
3-
import { getToolsEnvVars, getCurrentGoPath, getBinPath } from './util';
3+
import { getToolsEnvVars, getCurrentGoPath, getBinPath, getModuleCache } from './util';
44
import { outputChannel } from './goStatus';
55
import { getCurrentGoWorkspaceFromGOPATH } from './goPath';
66
import cp = require('child_process');
7+
import { isModSupported } from './goModules';
78

8-
export function installCurrentPackage() {
9+
export async function installCurrentPackage(): Promise<void> {
910
let editor = vscode.window.activeTextEditor;
1011
if (!editor) {
1112
vscode.window.showInformationMessage('No editor is active, cannot find current package to install');
@@ -24,6 +25,13 @@ export function installCurrentPackage() {
2425

2526
const env = Object.assign({}, getToolsEnvVars());
2627
const cwd = path.dirname(editor.document.uri.fsPath);
28+
const isMod = await isModSupported(editor.document.uri);
29+
30+
// Skip installing if cwd is in the module cache
31+
if (isMod && cwd.startsWith(getModuleCache())) {
32+
return;
33+
}
34+
2735
const goConfig = vscode.workspace.getConfiguration('go', editor.document.uri);
2836
const buildFlags = goConfig['buildFlags'] || [];
2937
const args = ['install', ...buildFlags];
@@ -34,7 +42,7 @@ export function installCurrentPackage() {
3442

3543
// Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846
3644
const currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd);
37-
const importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.';
45+
let importPath = (currentGoWorkspace && !isMod) ? cwd.substr(currentGoWorkspace.length + 1) : '.';
3846
args.push(importPath);
3947

4048
outputChannel.clear();

0 commit comments

Comments
 (0)