diff --git a/Extension/src/common.ts b/Extension/src/common.ts index c84f94290..60a8ff72e 100644 --- a/Extension/src/common.ts +++ b/Extension/src/common.ts @@ -399,14 +399,12 @@ export function resolveVariables(input: string | undefined, additionalEnvironmen const cycleCache = new Set(); while (!cycleCache.has(ret)) { cycleCache.add(ret); - ret = ret.replace(regexp(), (match: string, ignored1: string, varType: string, ignored2: string, name: string) => { - // Historically, if the variable didn't have anything before the "." or ":" - // it was assumed to be an environment variable - if (!varType) { - varType = "env"; - } + ret = ret.replace(regexp(), (match: string, ignored1: string | undefined, varType: string | undefined, ignored2: string | undefined, name: string) => { let newValue: string | undefined; switch (varType) { + // Historically, if the variable didn't have anything before the "." or ":" + // it was assumed to be an environment variable + case undefined: case "env": { if (additionalEnvironment) { const v: string | string[] | undefined = additionalEnvironment[name]; @@ -425,6 +423,12 @@ export function resolveVariables(input: string | undefined, additionalEnvironmen if (newValue === undefined) { newValue = process.env[name]; } + + // If the environment variable is not set, we return an empty string. Only do + // this for ${env:X} variables, not ${X} variables. + if (newValue === undefined && varType !== undefined) { + newValue = ""; + } break; } case "config": { diff --git a/Extension/test/scenarios/SingleRootProject/tests/common.test.ts b/Extension/test/scenarios/SingleRootProject/tests/common.test.ts index e04c88ea2..3c9e625a6 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/common.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/common.test.ts @@ -199,6 +199,23 @@ suite("resolveVariables", () => { .shouldLookupSymbol("Root"); }); + test("${env:X} expands to empty when unset", () => { + const processKey: string = `cpptoolstests_unset_${Date.now()}`; + delete process.env[processKey]; + resolveVariablesWithInput("${env:" + processKey + "}") + .withEnvironment({}) + .shouldResolveTo(""); + }); + + test("${X} left unexpanded when unset", () => { + const processKey: string = `cpptoolstests_unset_${Date.now()}`; + delete process.env[processKey]; + const token: string = "${" + processKey + "}"; + resolveVariablesWithInput(token) + .withEnvironment({}) + .shouldResolveTo(token); + }); + test("escapeForSquiggles:", () => { const testEscapeForSquigglesScenario: any = (input: string, expectedOutput: string) => { const result: string = escapeForSquiggles(input);