From 15f571e490e35a4c531aca6585aae7d07dfaae93 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Mon, 11 Mar 2019 19:05:32 -0700 Subject: [PATCH] Build, Install, Debug when using modules under GOPATH #2238 --- src/debugAdapter/goDebug.ts | 2 +- src/goBuild.ts | 12 ++++++++---- src/goInstall.ts | 14 +++++++++++--- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts index 30cb5b078..1de8a7b72 100644 --- a/src/debugAdapter/goDebug.ts +++ b/src/debugAdapter/goDebug.ts @@ -395,7 +395,7 @@ class Delve { let dlvArgs = [mode || 'debug']; if (mode === 'exec') { dlvArgs = dlvArgs.concat([program]); - } else if (currentGOWorkspace) { + } else if (currentGOWorkspace && env['GO111MODULE'] !== 'on') { dlvArgs = dlvArgs.concat([dirname.substr(currentGOWorkspace.length + 1)]); } dlvArgs = dlvArgs.concat(['--headless=true', '--listen=' + host + ':' + port.toString()]); diff --git a/src/goBuild.ts b/src/goBuild.ts index 1605c2405..530db03ca 100644 --- a/src/goBuild.ts +++ b/src/goBuild.ts @@ -1,8 +1,7 @@ import path = require('path'); import vscode = require('vscode'); -import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getTempFilePath } from './util'; +import { getToolsEnvVars, runTool, ICheckResult, handleDiagnosticErrors, getWorkspaceFolderPath, getCurrentGoPath, getTempFilePath, getModuleCache } from './util'; import { outputChannel } from './goStatus'; -import os = require('os'); import { getNonVendorPackages } from './goPackages'; import { getTestFlags } from './testUtils'; import { getCurrentGoWorkspaceFromGOPATH } from './goPath'; @@ -53,7 +52,7 @@ export function buildCode(buildWorkspace?: boolean) { * @param goConfig Configuration for the Go extension. * @param buildWorkspace If true builds code in all workspace. */ -export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.WorkspaceConfiguration, buildWorkspace?: boolean): Promise { +export async function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.WorkspaceConfiguration, buildWorkspace?: boolean): Promise { epoch++; let closureEpoch = epoch; if (tokenSource) { @@ -74,6 +73,11 @@ export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.Wo return Promise.resolve([]); } + // Skip building if cwd is in the module cache + if (isMod && cwd.startsWith(getModuleCache())) { + return []; + } + const buildEnv = Object.assign({}, getToolsEnvVars()); const tmpPath = getTempFilePath('go-code-check'); const isTestFile = fileUri && fileUri.fsPath.endsWith('_test.go'); @@ -115,7 +119,7 @@ export function goBuild(fileUri: vscode.Uri, isMod: boolean, goConfig: vscode.Wo // Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846 let currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd); - let importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.'; + let importPath = (currentGoWorkspace && !isMod) ? cwd.substr(currentGoWorkspace.length + 1) : '.'; running = true; outputChannel.appendLine(`Starting building the current package at ${cwd}`); return runTool( diff --git a/src/goInstall.ts b/src/goInstall.ts index 0799fc16c..d1a375e16 100644 --- a/src/goInstall.ts +++ b/src/goInstall.ts @@ -1,11 +1,12 @@ import path = require('path'); import vscode = require('vscode'); -import { getToolsEnvVars, getCurrentGoPath, getBinPath } from './util'; +import { getToolsEnvVars, getCurrentGoPath, getBinPath, getModuleCache } from './util'; import { outputChannel } from './goStatus'; import { getCurrentGoWorkspaceFromGOPATH } from './goPath'; import cp = require('child_process'); +import { isModSupported } from './goModules'; -export function installCurrentPackage() { +export async function installCurrentPackage(): Promise { let editor = vscode.window.activeTextEditor; if (!editor) { vscode.window.showInformationMessage('No editor is active, cannot find current package to install'); @@ -24,6 +25,13 @@ export function installCurrentPackage() { const env = Object.assign({}, getToolsEnvVars()); const cwd = path.dirname(editor.document.uri.fsPath); + const isMod = await isModSupported(editor.document.uri); + + // Skip installing if cwd is in the module cache + if (isMod && cwd.startsWith(getModuleCache())) { + return; + } + const goConfig = vscode.workspace.getConfiguration('go', editor.document.uri); const buildFlags = goConfig['buildFlags'] || []; const args = ['install', ...buildFlags]; @@ -34,7 +42,7 @@ export function installCurrentPackage() { // Find the right importPath instead of directly using `.`. Fixes https://github.com/Microsoft/vscode-go/issues/846 const currentGoWorkspace = getCurrentGoWorkspaceFromGOPATH(getCurrentGoPath(), cwd); - const importPath = currentGoWorkspace ? cwd.substr(currentGoWorkspace.length + 1) : '.'; + let importPath = (currentGoWorkspace && !isMod) ? cwd.substr(currentGoWorkspace.length + 1) : '.'; args.push(importPath); outputChannel.clear();