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

go.gopath and go.toolsGopath now expand ${env:XXX} #1743

Merged
merged 6 commits into from
Oct 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,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
if (!vscode.workspace.workspaceFolders || vscode.workspace.workspaceFolders.length <= 1) {
Expand Down Expand Up @@ -408,6 +408,12 @@ export function getToolsEnvVars(): any {
return envVars;
}

export function substituteEnv(input: string): string {
return input.replace(/\${env:([^}]+)}/g, 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)) {
Expand Down Expand Up @@ -443,7 +449,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']);
}

Expand Down
25 changes: 25 additions & 0 deletions test/unit/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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;
});
});