From 2b59636518cf0f9eddf234fe69af04870d5f3cd3 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Sun, 5 Feb 2017 12:05:36 -0800 Subject: [PATCH 1/4] Fixes #623 Expand ~ to os.homedir() in gopath --- src/debugAdapter/goDebug.ts | 4 ++-- src/goInstallTools.ts | 4 ++-- src/goPath.ts | 10 ++++++++++ src/util.ts | 4 ++-- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts index 826f1d7f7..d4d11879b 100644 --- a/src/debugAdapter/goDebug.ts +++ b/src/debugAdapter/goDebug.ts @@ -11,7 +11,7 @@ import { readFileSync, existsSync, lstatSync } from 'fs'; import { basename, dirname } from 'path'; import { spawn, ChildProcess } from 'child_process'; import { Client, RPCConnection } from 'json-rpc2'; -import { getBinPathWithPreferredGopath } from '../goPath'; +import { getBinPathWithPreferredGopath, resolvePath } from '../goPath'; import * as logger from 'vscode-debug-logger'; require('console-stamp')(console); @@ -187,7 +187,7 @@ class Delve { connectClient(port, host); return; } - let dlv = getBinPathWithPreferredGopath('dlv', env['GOPATH']); + let dlv = getBinPathWithPreferredGopath('dlv', resolvePath(env['GOPATH'])); if (!existsSync(dlv)) { verbose(`Couldnt find dlv at ${process.env['GOPATH']}${env['GOPATH'] ? ', ' + env['GOPATH'] : ''} or ${process.env['PATH']}`); diff --git a/src/goInstallTools.ts b/src/goInstallTools.ts index 2f0d208cb..f901a0304 100644 --- a/src/goInstallTools.ts +++ b/src/goInstallTools.ts @@ -11,7 +11,7 @@ import path = require('path'); import os = require('os'); import cp = require('child_process'); import { showGoStatus, hideGoStatus } from './goStatus'; -import { getGoRuntimePath } from './goPath'; +import { getGoRuntimePath, resolvePath } from './goPath'; import { outputChannel } from './goStatus'; import { getBinPath, getToolsGopath, getGoVersion, SemVersion, isVendorSupported } from './util'; @@ -207,7 +207,7 @@ export function updateGoPathGoRootFromConfig() { let gopath = vscode.workspace.getConfiguration('go')['gopath']; if (gopath) { - process.env['GOPATH'] = gopath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath); + process.env['GOPATH'] = resolvePath(gopath, vscode.workspace.rootPath); } let inferGoPath = vscode.workspace.getConfiguration('go')['inferGopath']; diff --git a/src/goPath.ts b/src/goPath.ts index 3cfe27282..8a3ebd8b4 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -95,4 +95,14 @@ function fileExists(filePath: string): boolean { export function clearCacheForTools() { binPathCache = {}; +} + +/** + * Exapnds ~ to homedir in non-Windows platform and replaces ${workspaceRoot} token with given workspaceroot + */ +export function resolvePath(inputPath: string, workspaceRoot?: string): string { + if (workspaceRoot) { + inputPath = inputPath.replace(/\${workspaceRoot}/g, workspaceRoot); + } + return (process.platform === 'win32' || !inputPath.startsWith('~')) ? inputPath : path.join(os.homedir(), inputPath.substr(1)); } \ No newline at end of file diff --git a/src/util.ts b/src/util.ts index a06e3b24f..b27dbbcde 100644 --- a/src/util.ts +++ b/src/util.ts @@ -5,7 +5,7 @@ import vscode = require('vscode'); import path = require('path'); -import { getGoRuntimePath, getBinPathWithPreferredGopath} from './goPath'; +import { getGoRuntimePath, getBinPathWithPreferredGopath, resolvePath} from './goPath'; import cp = require('child_process'); import TelemetryReporter from 'vscode-extension-telemetry'; @@ -253,7 +253,7 @@ export function getToolsGopath(): string { let goConfig = vscode.workspace.getConfiguration('go'); let toolsGopath = goConfig['toolsGopath']; if (toolsGopath) { - toolsGopath = toolsGopath.replace(/\${workspaceRoot}/g, vscode.workspace.rootPath); + toolsGopath = resolvePath(toolsGopath, vscode.workspace.rootPath); } return toolsGopath; } From 7b7f008f028f3d013bc4a8b7981cde04f2f428f0 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Sun, 5 Feb 2017 12:10:10 -0800 Subject: [PATCH 2/4] Add null/empty string check --- src/goPath.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/goPath.ts b/src/goPath.ts index 8a3ebd8b4..7f2c0f450 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -101,6 +101,7 @@ export function clearCacheForTools() { * Exapnds ~ to homedir in non-Windows platform and replaces ${workspaceRoot} token with given workspaceroot */ export function resolvePath(inputPath: string, workspaceRoot?: string): string { + if (!inputPath || !inputPath.trim()) return inputPath; if (workspaceRoot) { inputPath = inputPath.replace(/\${workspaceRoot}/g, workspaceRoot); } From 64a77a0898fde0317ae46ddb5007dcf5cf2ec2df Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Sun, 5 Feb 2017 22:08:12 -0800 Subject: [PATCH 3/4] Support ~in Windows as well --- src/goPath.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/goPath.ts b/src/goPath.ts index 7f2c0f450..b86638d62 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -105,5 +105,5 @@ export function resolvePath(inputPath: string, workspaceRoot?: string): string { if (workspaceRoot) { inputPath = inputPath.replace(/\${workspaceRoot}/g, workspaceRoot); } - return (process.platform === 'win32' || !inputPath.startsWith('~')) ? inputPath : path.join(os.homedir(), inputPath.substr(1)); + return !inputPath.startsWith('~') ? inputPath : path.join(os.homedir(), inputPath.substr(1)); } \ No newline at end of file From 8043a5cfdaae5d034c1910519ad8525bf5a92992 Mon Sep 17 00:00:00 2001 From: Ramya Achutha Rao Date: Mon, 6 Feb 2017 11:32:51 -0800 Subject: [PATCH 4/4] Switch the condition --- src/goPath.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/goPath.ts b/src/goPath.ts index b86638d62..f925b95ee 100644 --- a/src/goPath.ts +++ b/src/goPath.ts @@ -105,5 +105,5 @@ export function resolvePath(inputPath: string, workspaceRoot?: string): string { if (workspaceRoot) { inputPath = inputPath.replace(/\${workspaceRoot}/g, workspaceRoot); } - return !inputPath.startsWith('~') ? inputPath : path.join(os.homedir(), inputPath.substr(1)); + return inputPath.startsWith('~') ? path.join(os.homedir(), inputPath.substr(1)) : inputPath; } \ No newline at end of file