diff --git a/src/goBuild.ts b/src/goBuild.ts index 8a8860b41..ead55988d 100644 --- a/src/goBuild.ts +++ b/src/goBuild.ts @@ -126,6 +126,9 @@ 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) : '.'; + if (importPath === '') { + importPath = currentGoWorkspace; + } running = true; return runTool( buildArgs.concat('-o', tmpPath, importPath), diff --git a/src/goPath.ts b/src/goPath.ts index f0647b3f7..997d2c79d 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -179,9 +179,22 @@ export function getCurrentGoWorkspaceFromGOPATH(gopath: string, currentFileDirPa } } } + // use current root if the package is using go modules outside of GOPATH + if (isGoModule(currentFileDirPath)) { + currentWorkspace = currentFileDirPath; + } return currentWorkspace; } +export function isGoModule(modulePath: string): Boolean { + try { + return fs.statSync(path.join(modulePath, 'go.mod')).isFile(); + } + catch (e) { + return false; + } +} + // Workaround for issue in https://github.com/Microsoft/vscode/issues/9448#issuecomment-244804026 export function fixDriveCasingInWindows(pathToFix: string): string { return (process.platform === 'win32' && pathToFix) ? pathToFix.substr(0, 1).toUpperCase() + pathToFix.substr(1) : pathToFix; diff --git a/src/testUtils.ts b/src/testUtils.ts index 28aeef4ff..42ff13220 100644 --- a/src/testUtils.ts +++ b/src/testUtils.ts @@ -7,7 +7,7 @@ import cp = require('child_process'); import path = require('path'); import vscode = require('vscode'); import util = require('util'); -import { parseEnvFile, getCurrentGoWorkspaceFromGOPATH } from './goPath'; +import { parseEnvFile, getCurrentGoWorkspaceFromGOPATH, isGoModule } from './goPath'; import { getToolsEnvVars, getGoVersion, LineBuffer, SemVersion, resolvePath, getCurrentGoPath, getBinPath } from './util'; import { GoDocumentSymbolProvider } from './goOutline'; import { getNonVendorPackages } from './goPackages'; @@ -229,7 +229,8 @@ export function goTest(testconfig: TestConfig): Thenable { const result = line.match(packageResultLineRE); if (result && currentGoWorkspace) { const packageNameArr = result[2].split('/'); - const baseDir = path.join(currentGoWorkspace, ...packageNameArr); + + const baseDir = isGoModule(currentGoWorkspace) ? currentGoWorkspace : path.join(currentGoWorkspace, ...packageNameArr); testResultLines.forEach(line => outputChannel.appendLine(expandFilePathInOutput(line, baseDir))); testResultLines.splice(0); }