From e64e50ff5350d69ae823d6a878136ba4872e23e5 Mon Sep 17 00:00:00 2001 From: sphawk Date: Wed, 20 Jun 2018 02:32:00 +0900 Subject: [PATCH 1/5] go.gopath and go.toolsGopath now expand ${env:XXX} --- src/util.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/util.ts b/src/util.ts index 70564b6d1..9819ec9bd 100644 --- a/src/util.ts +++ b/src/util.ts @@ -308,7 +308,7 @@ export function getToolsGopath(useCache: boolean = true): string { function resolveToolsGopath(): string { - let toolsGopathForWorkspace = vscode.workspace.getConfiguration('go')['toolsGopath'] || ''; + let toolsGopathForWorkspace = substituteEnv(vscode.workspace.getConfiguration('go')['toolsGopath'] || ''); // In case of single root, use resolvePath to resolve ~ and ${workspaceRoot} if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 1) { @@ -368,6 +368,12 @@ export function getToolsEnvVars(): any { return envVars; } +export function substituteEnv(input: string): string { + return input.replace(/\${env:([^}]+)}/mg, function (match, capture) { + return process.env[capture.trim()] || ''; + }); +} + export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { let currentFilePath: string; if (vscode.window.activeTextEditor && vscode.workspace.getWorkspaceFolder(vscode.window.activeTextEditor.document.uri)) { @@ -403,7 +409,7 @@ export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { } } - const configGopath = config['gopath'] ? resolvePath(config['gopath'], currentRoot) : ''; + let configGopath = config['gopath'] ? resolvePath(substituteEnv(config['gopath']), currentRoot) : ''; return inferredGopath ? inferredGopath : (configGopath || process.env['GOPATH']); } From 1d46c56b8a567538443d695c054a7300b29ed882 Mon Sep 17 00:00:00 2001 From: Sewon Park Date: Wed, 20 Jun 2018 03:00:00 +0900 Subject: [PATCH 2/5] Fix linting errors --- src/util.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/util.ts b/src/util.ts index 9819ec9bd..25374977d 100644 --- a/src/util.ts +++ b/src/util.ts @@ -369,9 +369,9 @@ export function getToolsEnvVars(): any { } export function substituteEnv(input: string): string { - return input.replace(/\${env:([^}]+)}/mg, function (match, capture) { - return process.env[capture.trim()] || ''; - }); + return input.replace(/\${env:([^}]+)}/mg, function (match, capture) { + return process.env[capture.trim()] || ''; + }); } export function getCurrentGoPath(workspaceUri?: vscode.Uri): string { From 4d5ceb3fee0769359a0cf246b72e08ea08b7724a Mon Sep 17 00:00:00 2001 From: sphawk Date: Wed, 17 Oct 2018 23:27:16 +0900 Subject: [PATCH 3/5] remove 'm' flag from substituteEnv --- src/util.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util.ts b/src/util.ts index 8b75aa9bc..66702126f 100644 --- a/src/util.ts +++ b/src/util.ts @@ -409,7 +409,7 @@ export function getToolsEnvVars(): any { } export function substituteEnv(input: string): string { - return input.replace(/\${env:([^}]+)}/mg, function (match, capture) { + return input.replace(/\${env:([^}]+)}/g, function (match, capture) { return process.env[capture.trim()] || ''; }); } From b2d2d9a49f7087ecf7d530a93fd72fbdd57597a6 Mon Sep 17 00:00:00 2001 From: sphawk Date: Wed, 17 Oct 2018 23:27:33 +0900 Subject: [PATCH 4/5] add test for substituteEnv --- test/unit/utils.test.ts | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 test/unit/utils.test.ts diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts new file mode 100644 index 000000000..96d837318 --- /dev/null +++ b/test/unit/utils.test.ts @@ -0,0 +1,25 @@ +/*--------------------------------------------------------- + * Copyright (C) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------*/ + +import * as assert from 'assert'; +import { substituteEnv } from '../../src/util'; + +suite('utils Tests', () => { + test('substituteEnv: default', () => { + + // prepare test + const env = Object.assign({}, process.env); + process.env['test1'] = 'abcd'; + process.env['test2'] = 'defg'; + + let actual = substituteEnv(' ${env:test1} \r\n ${env:test2}\r\n${env:test1}'); + let expected = ' abcd \r\n defg\r\nabcd'; + + assert.equal(actual, expected); + + // test completed + process.env = env; + }); +}); \ No newline at end of file From 499d6b186232dd76bb723101d45f90e0453fa2c1 Mon Sep 17 00:00:00 2001 From: sphawk Date: Wed, 17 Oct 2018 23:33:40 +0900 Subject: [PATCH 5/5] add newline on end of utils.test.ts --- test/unit/utils.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/unit/utils.test.ts b/test/unit/utils.test.ts index 96d837318..9daffd9d1 100644 --- a/test/unit/utils.test.ts +++ b/test/unit/utils.test.ts @@ -22,4 +22,4 @@ suite('utils Tests', () => { // test completed process.env = env; }); -}); \ No newline at end of file +});