Skip to content

Commit 5b8a911

Browse files
sapenmsoftgopherbot
authored andcommitted
extension/src/utils: Support exported environment variables with go.testEnvFile
This changes adds support for exported environment variables in environment files with the go.testEnvFile configuration option Fixes #3879 Change-Id: Idd7073fb8efaf93a15d51a2bfab4c361911efa4f GitHub-Last-Rev: 5bfe028 GitHub-Pull-Request: #3880 Reviewed-on: https://go-review.googlesource.com/c/vscode-go/+/706135 Reviewed-by: Hongxiang Jiang <[email protected]> Reviewed-by: Junyang Shao <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Auto-Submit: Hongxiang Jiang <[email protected]>
1 parent ddc8268 commit 5b8a911

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

extension/src/utils/envUtils.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ function stripBOM(s: string): string {
1717
}
1818

1919
/**
20-
* returns the environment variable collection created by parsing the given .env file.
20+
* Returns the environment variable collection created by parsing the given .env file.
21+
* Each line in the .env file is expected to be in the format `KEY=VALUE` or `export KEY=VALUE`.
22+
* Keys can contain alphanumeric characters, periods, and hyphens.
23+
* Values can be optionally enclosed in single or double quotes. Double-quoted values support `\n` for newlines.
24+
* Environment variable substitution using `${VAR}` syntax is also supported within values.
2125
*/
2226
export function parseEnvFile(envFilePath: string, globalVars?: NodeJS.Dict<string>): { [key: string]: string } {
2327
const env: { [key: string]: string } = {};
@@ -31,14 +35,14 @@ export function parseEnvFile(envFilePath: string, globalVars?: NodeJS.Dict<strin
3135
try {
3236
const buffer = stripBOM(fs.readFileSync(envFilePath, 'utf8'));
3337
buffer.split('\n').forEach((line) => {
34-
const r = line.match(/^\s*([\w\.\-]+)\s*=\s*(.*)?\s*$/);
38+
const r = line.match(/^\s*(export\s+)?([\w\.\-]+)\s*=\s*(.*)?\s*$/);
3539
if (r !== null) {
36-
let value = r[2] || '';
40+
let value = r[3] || '';
3741
if (value.length > 0 && value.charAt(0) === '"' && value.charAt(value.length - 1) === '"') {
3842
value = value.replace(/\\n/gm, '\n');
3943
}
4044
const v = value.replace(/(^['"]|['"]$)/g, '');
41-
env[r[1]] = substituteEnvVars(v, env, globalVars!);
45+
env[r[2]] = substituteEnvVars(v, env, globalVars!);
4246
}
4347
});
4448
return env;

extension/test/integration/goDebugConfiguration.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ suite('Debug Environment Variable Merge Test', () => {
130130
test('launchArgs.env overwrites launchArgs.envFile', () => {
131131
const env = { SOMEVAR1: 'valueFromEnv' };
132132
const envFile = path.join(tmpDir, 'env');
133-
fs.writeFileSync(envFile, ['SOMEVAR1=valueFromEnvFile1', 'SOMEVAR2=valueFromEnvFile2'].join('\n'));
133+
fs.writeFileSync(envFile, ['SOMEVAR1=valueFromEnvFile1', 'export SOMEVAR2=valueFromEnvFile2'].join('\n'));
134134

135135
runTest(
136136
{ env, envFile },

0 commit comments

Comments
 (0)