From 214642cd5dd8f14fd48edfa46f8baf28793772ce Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Sat, 10 Oct 2026 14:26:42 +0200 Subject: [PATCH 1/7] Fix variable interpolation in .env files loaded through envFile --- Extension/CHANGELOG.md | 1 + .../src/Debugger/ParsedEnvironmentFile.ts | 11 ++++++++ .../tests/ParsedEnvironmentFile.test.ts | 27 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/Extension/CHANGELOG.md b/Extension/CHANGELOG.md index e81081eab..b925bf3fd 100644 --- a/Extension/CHANGELOG.md +++ b/Extension/CHANGELOG.md @@ -113,6 +113,7 @@ * Fix a `cpptools` crash during shutdown on macOS. * Various localization updates. * Update dependencies. +* Fix `${VAR}` interpolation in `.env` files used by `envFile`: references to variables defined earlier in the same file (or in the OS environment) are now resolved. [#13781](https://github.com/microsoft/vscode-cpptools/issues/13781) ## Version 1.34.4: September 9, 2026 ### Enhancements diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 9018a540b..311fda21b 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -59,6 +59,17 @@ export class ParsedEnvironmentFile { value = value.replace(/(^['"]|['"]$)/g, ""); + // Resolve ${VAR} references against the variables defined so far in the file + // (and the process environment), following the dotenv interpolation rules. + // A reference to a name we don't know is left as-is instead of being dropped. + value = value.replace(/\$\{([\w\.\-]+)\}/g, (full: string, name: string): string => { + if (env.has(name)) { + return env.get(name); + } + const processValue: string | undefined = process.env[name]; + return processValue !== undefined ? processValue : full; + }); + env.set(key, value); } else { // Blank lines and lines starting with # are no parse errors diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index a107971ce..6e8abff01 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -100,4 +100,31 @@ MyName2=Value2 assertEnvironmentEqual(result.Env, "MyName1", "Value1"); assertEnvironmentEqual(result.Env, "MyName2", "Value2"); }); + + test("Resolve a reference to a variable defined earlier in the file", () => { + const content: string = `USER=admin +EMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "USER", "admin"); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); + }); + + test("Resolve a reference from the initial environment", () => { + const initialEnv: Environment[] = [{ name: "USER", value: "admin" }]; + const content: string = `EMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", initialEnv); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); + }); + + test("Leave an unknown reference untouched", () => { + const content: string = `EMAIL=\${UNKNOWN_USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${UNKNOWN_USER}@example.org"); + }); }); From c5946b7b4d0402ef919f14e15f46a943f2028528 Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Wed, 14 Oct 2026 00:31:15 +0200 Subject: [PATCH 2/7] Leave single-quoted and escaped references alone in .env interpolation The quotes come off before the references are resolved, so VALUE='${SECRET}' was expanded and a \${SECRET} reference kept its backslash while losing its meaning. Both are literal in dotenv, and both were literal before the interpolation existed. Adds the missing test for the process-environment fallback the changelog advertises. --- Extension/CHANGELOG.md | 2 +- .../src/Debugger/ParsedEnvironmentFile.ts | 31 ++++++++++------ .../tests/ParsedEnvironmentFile.test.ts | 35 +++++++++++++++++++ 3 files changed, 57 insertions(+), 11 deletions(-) diff --git a/Extension/CHANGELOG.md b/Extension/CHANGELOG.md index b925bf3fd..8a7104620 100644 --- a/Extension/CHANGELOG.md +++ b/Extension/CHANGELOG.md @@ -113,7 +113,7 @@ * Fix a `cpptools` crash during shutdown on macOS. * Various localization updates. * Update dependencies. -* Fix `${VAR}` interpolation in `.env` files used by `envFile`: references to variables defined earlier in the same file (or in the OS environment) are now resolved. [#13781](https://github.com/microsoft/vscode-cpptools/issues/13781) +* Fix `${VAR}` interpolation in `.env` files used by `envFile`: references to variables defined earlier in the same file (or in the OS environment) are now resolved. Single-quoted values and `\${VAR}` are left as written. [#13781](https://github.com/microsoft/vscode-cpptools/issues/13781) ## Version 1.34.4: September 9, 2026 ### Enhancements diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 311fda21b..30cd9e1a2 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -57,18 +57,29 @@ export class ParsedEnvironmentFile { value = value.replace(/\\n/gm, "\n"); } + // A single-quoted value is literal, so a reference inside it must not be + // expanded. The quotes come off on the next line, so this has to be read here. + const singleQuoted: boolean = (value.length > 1) && (value.charAt(0) === "'") && (value.charAt(value.length - 1) === "'"); + value = value.replace(/(^['"]|['"]$)/g, ""); - // Resolve ${VAR} references against the variables defined so far in the file - // (and the process environment), following the dotenv interpolation rules. - // A reference to a name we don't know is left as-is instead of being dropped. - value = value.replace(/\$\{([\w\.\-]+)\}/g, (full: string, name: string): string => { - if (env.has(name)) { - return env.get(name); - } - const processValue: string | undefined = process.env[name]; - return processValue !== undefined ? processValue : full; - }); + // Resolve ${VAR} references against the variables defined so far in the file, + // then the process environment. A single-quoted value and a reference written + // with a backslash both stay literal; the backslash itself is dropped, so + // \${VAR} ends up as the text ${VAR}. A name we don't know is left as-is + // instead of being dropped. + if (!singleQuoted) { + value = value.replace(/(\\)?\$\{([\w\.\-]+)\}/g, (full: string, escape: string | undefined, name: string): string => { + if (escape) { + return full.substring(1); + } + if (env.has(name)) { + return env.get(name); + } + const processValue: string | undefined = process.env[name]; + return processValue !== undefined ? processValue : full; + }); + } env.set(key, value); } else { diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index 6e8abff01..24d38d66a 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -127,4 +127,39 @@ EMAIL=\${USER}@example.org`; assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); assertEnvironmentEqual(result.Env, "EMAIL", "${UNKNOWN_USER}@example.org"); }); + + test("Resolve a reference from the process environment", () => { + const name: string = "CPPTOOLS_ENVFILE_INTERPOLATION_TEST"; + const previous: string | undefined = process.env[name]; + process.env[name] = "from-process"; + try { + const content: string = `EMAIL=\${${name}}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "from-process@example.org"); + } finally { + if (previous === undefined) { + delete process.env[name]; + } else { + process.env[name] = previous; + } + } + }); + + test("Leave a reference inside a single-quoted value alone", () => { + const content: string = `USER=admin\nEMAIL='\${USER}@example.org'`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${USER}@example.org"); + }); + + test("Leave an escaped reference alone, without the backslash", () => { + const content: string = `USER=admin\nEMAIL=\\\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${USER}@example.org"); + }); }); From e12e15af3a9d0fcd24274841a724f630851869bc Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Wed, 14 Oct 2026 12:09:21 +0200 Subject: [PATCH 3/7] Protect literal references from the launch expansion The parser's output goes through expandAllStrings straight after it (configurationProvider.expand), which substitutes every ${...} it knows and warns about the rest. Writing a literal back as a bare ${VAR} therefore did not keep it literal: the expansion either replaced it from the launch configuration or reported "Invalid variable reference" -- the very symptom this change set out to remove. Both literal forms now come out as ${dollar}{VAR}, the one form that expansion leaves alone and collapses to a plain $ once it is finished. Drops the changelog entry, since those are written separately before a release. --- Extension/CHANGELOG.md | 1 - .../src/Debugger/ParsedEnvironmentFile.ts | 20 +++++++++++++------ .../tests/ParsedEnvironmentFile.test.ts | 18 +++++++++++++---- 3 files changed, 28 insertions(+), 11 deletions(-) diff --git a/Extension/CHANGELOG.md b/Extension/CHANGELOG.md index 8a7104620..e81081eab 100644 --- a/Extension/CHANGELOG.md +++ b/Extension/CHANGELOG.md @@ -113,7 +113,6 @@ * Fix a `cpptools` crash during shutdown on macOS. * Various localization updates. * Update dependencies. -* Fix `${VAR}` interpolation in `.env` files used by `envFile`: references to variables defined earlier in the same file (or in the OS environment) are now resolved. Single-quoted values and `\${VAR}` are left as written. [#13781](https://github.com/microsoft/vscode-cpptools/issues/13781) ## Version 1.34.4: September 9, 2026 ### Enhancements diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 30cd9e1a2..9fa85f4bf 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -64,14 +64,22 @@ export class ParsedEnvironmentFile { value = value.replace(/(^['"]|['"]$)/g, ""); // Resolve ${VAR} references against the variables defined so far in the file, - // then the process environment. A single-quoted value and a reference written - // with a backslash both stay literal; the backslash itself is dropped, so - // \${VAR} ends up as the text ${VAR}. A name we don't know is left as-is - // instead of being dropped. - if (!singleQuoted) { + // then the process environment. A name we don't know is left as-is rather than + // dropped, so ${workspaceFolder} and the other launch variables still resolve + // from the debug configuration. + // + // A reference that has to stay literal is written back as ${dollar}{VAR}. This + // parser's output goes on to expandAllStrings (configurationProvider.expand), + // which substitutes every remaining ${...} it knows and warns about the rest, so + // a bare ${VAR} would be replaced or reported instead of reaching the debuggee. + // ${dollar} is the one form that expansion leaves alone and collapses to a plain + // $ once it is done, which is what makes the literal survive the trip. + if (singleQuoted) { + value = value.replace(/\$\{([\w\.\-]+)\}/g, (_full: string, name: string): string => `\${dollar}{${name}}`); + } else { value = value.replace(/(\\)?\$\{([\w\.\-]+)\}/g, (full: string, escape: string | undefined, name: string): string => { if (escape) { - return full.substring(1); + return `\${dollar}{${name}}`; } if (env.has(name)) { return env.get(name); diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index 24d38d66a..52a9e81c0 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -147,19 +147,29 @@ EMAIL=\${USER}@example.org`; } }); - test("Leave a reference inside a single-quoted value alone", () => { + test("Protect a reference inside a single-quoted value from the launch expansion", () => { const content: string = `USER=admin\nEMAIL='\${USER}@example.org'`; const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); - assertEnvironmentEqual(result.Env, "EMAIL", "${USER}@example.org"); + // ${dollar} is the one form expandAllStrings leaves alone, and it collapses to a plain $ + // when that expansion finishes (expand.ts), so the debuggee still receives the literal. + assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); }); - test("Leave an escaped reference alone, without the backslash", () => { + test("Protect an escaped reference the same way, without the backslash", () => { const content: string = `USER=admin\nEMAIL=\\\${USER}@example.org`; const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); - assertEnvironmentEqual(result.Env, "EMAIL", "${USER}@example.org"); + assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); + }); + + test("Leave a resolved reference bare, so the expansion can still see it", () => { + const content: string = `USER=admin\nEMAIL=\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); }); }); From 1f8a192837e89b633c54f992987aba13cf9b124a Mon Sep 17 00:00:00 2001 From: Abdelhadi Salmaoui Date: Sat, 21 Nov 2026 23:58:59 +0100 Subject: [PATCH 4/7] Protect colon-qualified references in quoted and escaped values The literal protection only matched dotenv-style names, but the launch expansion also rewrites ${env:...} and runs ${command:...} on anything up to the closing brace. A single-quoted value and a backslash-escaped reference were therefore still expanded, and the escaped one kept its backslash. Every ${ opener in a single-quoted value is now written back, and so is every escaped opener, whatever follows it. Unescaped dotenv-style names are still the only ones resolved here. --- .../src/Debugger/ParsedEnvironmentFile.ts | 15 ++++++++-- .../tests/ParsedEnvironmentFile.test.ts | 28 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 9fa85f4bf..031ce94c6 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -74,13 +74,24 @@ export class ParsedEnvironmentFile { // a bare ${VAR} would be replaced or reported instead of reaching the debuggee. // ${dollar} is the one form that expansion leaves alone and collapses to a plain // $ once it is done, which is what makes the literal survive the trip. + // + // The dotenv-style names are not the only ones that expansion touches: it also + // rewrites ${env:...} and runs ${command:...}, both matched on anything up to the + // closing brace. So the protection cannot be limited to [\w\.\-]+ -- every ${ in + // a single-quoted value, and every escaped ${ anywhere, is written back, whatever + // follows it. Unescaped dotenv-style names are the only ones resolved here; an + // unescaped ${env:...} is left for the launch expansion, as before. if (singleQuoted) { - value = value.replace(/\$\{([\w\.\-]+)\}/g, (_full: string, name: string): string => `\${dollar}{${name}}`); + value = value.replace(/\$\{/g, `\${dollar}{`); } else { - value = value.replace(/(\\)?\$\{([\w\.\-]+)\}/g, (full: string, escape: string | undefined, name: string): string => { + value = value.replace(/(\\)?\$\{([\w\.\-]+)\}|\\\$\{/g, (full: string, escape: string | undefined, name: string | undefined): string => { if (escape) { return `\${dollar}{${name}}`; } + if (name === undefined) { + // Escaped opener that is not a dotenv-style name, e.g. \${env:HOME}. + return `\${dollar}{`; + } if (env.has(name)) { return env.get(name); } diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index 52a9e81c0..9f1725f94 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -165,6 +165,34 @@ EMAIL=\${USER}@example.org`; assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); }); + test("Protect a launch reference inside a single-quoted value", () => { + // ${env:...} and ${command:...} are matched on anything up to the closing brace, so a + // dotenv-style name filter would let them through and the launch expansion would + // substitute (or run) them even though the value is single-quoted. + const content: string = `HOME_DIR='\${env:HOME}'\nCMD='\${command:workbench.action.files.newUntitledFile}'`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${dollar}{env:HOME}"); + assertEnvironmentEqual(result.Env, "CMD", "${dollar}{command:workbench.action.files.newUntitledFile}"); + }); + + test("Protect an escaped launch reference, without the backslash", () => { + const content: string = `HOME_DIR=\\\${env:HOME}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${dollar}{env:HOME}"); + }); + + test("Leave an unescaped launch reference bare for the expansion", () => { + const content: string = `HOME_DIR=\${env:HOME}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "HOME_DIR", "${env:HOME}"); + }); + test("Leave a resolved reference bare, so the expansion can still see it", () => { const content: string = `USER=admin\nEMAIL=\${USER}@example.org`; const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); From d15097cedf668abf0a5f39b9118421527cd0b7fc Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Tue, 24 Nov 2026 13:28:26 +0100 Subject: [PATCH 5/7] Keep the path separator before an escaped reference Treating every backslash in front of ${ as an escape broke Windows paths. In C:\Users\${env:USERNAME}\project the backslash separates path segments, so the value used to expand to C:\Users\Alice\project and now came out as C:\Users${env:USERNAME}\project with the reference left literal. A backslash only escapes the reference where it cannot be a separator: at the start of the value, or after =, a space or a quote. The expansion is also done in one pass now, so a reference the file cannot answer is passed through instead of being rebuilt. Environment names are case-insensitive on Windows, so ${PATH} now finds a Path entry from the launch configuration instead of falling through to the host environment. Tested on the compiled output: 19 passing, and the two new cases fail on the previous commit. --- .../src/Debugger/ParsedEnvironmentFile.ts | 95 ++++++++++++++++--- .../tests/ParsedEnvironmentFile.test.ts | 25 +++++ 2 files changed, 106 insertions(+), 14 deletions(-) diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index 031ce94c6..f0619be99 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -84,20 +84,7 @@ export class ParsedEnvironmentFile { if (singleQuoted) { value = value.replace(/\$\{/g, `\${dollar}{`); } else { - value = value.replace(/(\\)?\$\{([\w\.\-]+)\}|\\\$\{/g, (full: string, escape: string | undefined, name: string | undefined): string => { - if (escape) { - return `\${dollar}{${name}}`; - } - if (name === undefined) { - // Escaped opener that is not a dotenv-style name, e.g. \${env:HOME}. - return `\${dollar}{`; - } - if (env.has(name)) { - return env.get(name); - } - const processValue: string | undefined = process.env[name]; - return processValue !== undefined ? processValue : full; - }); + value = ParsedEnvironmentFile.resolveReferences(value, (name: string) => ParsedEnvironmentFile.lookup(name, env)); } env.set(key, value); @@ -127,4 +114,84 @@ export class ParsedEnvironmentFile { return new ParsedEnvironmentFile(arrayEnv, warning); } + + /** + * Resolve the references the file can actually answer, and leave the others untouched so + * the launch expansion still sees them. + */ + private static resolveReferences(value: string, known: (name: string) => string | undefined): string { + let result: string = ""; + let i: number = 0; + while (i < value.length) { + const char: string = value.charAt(i); + + // An escaped reference is literal. ${dollar} is the form the launch expansion leaves + // alone and collapses back to a plain $ once it is done, so the debuggee is the only + // place that sees the literal. + if (char === "\\" && value.charAt(i + 1) === "$" && ParsedEnvironmentFile.isReferenceEscape(value, i)) { + const end: number = value.indexOf("}", i + 2); + if (end !== -1) { + result += `\${dollar}{${value.substring(i + 3, end)}}`; + i = end + 1; + continue; + } + } + + if (char === "$" && value.charAt(i + 1) === "{") { + const end: number = value.indexOf("}", i + 2); + if (end !== -1) { + const name: string = value.substring(i + 2, end); + const resolved: string | undefined = /^[\w\.\-]+$/.test(name) ? known(name) : undefined; + result += resolved !== undefined ? resolved : value.substring(i, end + 1); + i = end + 1; + continue; + } + } + + result += char; + i += 1; + } + + return result; + } + + /** + * A backslash only escapes the reference where it cannot be a path separator instead. In + * C:\Users\${env:USERNAME}\project the backslash separates path segments, so the reference + * still expands. At the start of a value, or after =, a quote or a space, it is an escape. + */ + private static isReferenceEscape(value: string, index: number): boolean { + if (index === 0) { + return true; + } + return !/[A-Za-z0-9_.\-:\/\\]/.test(value.charAt(index - 1)); + } + + /** + * Environment variable names are case-insensitive on Windows, and the launch configuration + * may have spelled the name differently than the reference does, so Path and PATH have to + * find each other there. + */ + private static lookup(name: string, env: Map): string | undefined { + if (env.has(name)) { + return env.get(name); + } + + if (process.platform === "win32") { + const folded: string = name.toLowerCase(); + for (const key of env.keys()) { + if (key.toLowerCase() === folded) { + return env.get(key); + } + } + for (const key of Object.keys(process.env)) { + if (key.toLowerCase() === folded) { + return process.env[key]; + } + } + return undefined; + } + + return process.env[name]; + } } diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index 9f1725f94..cdda6bfdd 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -200,4 +200,29 @@ EMAIL=\${USER}@example.org`; assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); assertEnvironmentEqual(result.Env, "EMAIL", "admin@example.org"); }); + + test("Keep the separator before a reference in a Windows path", () => { + // The backslash in front of ${ here separates path segments, it does not escape the + // reference. Dropping it turned C:\Users\Alice\project into C:\Users${env:USERNAME}. + const content: string = `USER_DIR=C:\\Users\\\${env:USERNAME}\\project`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "USER_DIR", "C:\\Users\\${env:USERNAME}\\project"); + }); + + test("Match an initial environment name regardless of case on Windows", function () { + if (process.platform !== "win32") { + this.skip(); + } + + // Path and PATH are the same variable on Windows, and the launch configuration may use + // either spelling. + const content: string = `COPY=\${PATH}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent( + content, "TestEnvFileName", [{ name: "Path", value: "C:\\override" }]); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "COPY", "C:\\override"); + }); }); From 5ab402b2ed6161e5cbd7d6188c32825237a9c733 Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Tue, 24 Nov 2026 18:07:27 +0100 Subject: [PATCH 6/7] Replace this.skip() with a platform check this.skip() inside a function expression trips no-invalid-this, and the linter is part of CI. The test now asserts the expected value per platform instead, so it runs everywhere and still pins the Windows behaviour. --- .../tests/ParsedEnvironmentFile.test.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index cdda6bfdd..b35e43226 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -211,18 +211,15 @@ EMAIL=\${USER}@example.org`; assertEnvironmentEqual(result.Env, "USER_DIR", "C:\\Users\\${env:USERNAME}\\project"); }); - test("Match an initial environment name regardless of case on Windows", function () { - if (process.platform !== "win32") { - this.skip(); - } - + test("Match an initial environment name regardless of case on Windows", () => { // Path and PATH are the same variable on Windows, and the launch configuration may use - // either spelling. + // either spelling. Elsewhere the reference stays bare for the launch expansion. const content: string = `COPY=\${PATH}`; const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent( content, "TestEnvFileName", [{ name: "Path", value: "C:\\override" }]); + const expected: string = process.platform === "win32" ? "C:\\override" : "${PATH}"; assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); - assertEnvironmentEqual(result.Env, "COPY", "C:\\override"); + assertEnvironmentEqual(result.Env, "COPY", expected); }); }); From c8b399f0755e9cd96c13dc54af57ca10a6b21996 Mon Sep 17 00:00:00 2001 From: abdelhadi703 Date: Thu, 26 Nov 2026 11:58:19 +0100 Subject: [PATCH 7/7] Treat only a drive path as a path, and require the full opener The previous rule looked at the character before the backslash, so an escaped reference in ordinary text stopped being literal: prefix\${USER} expanded instead of staying put, which also meant a \${command:...} in prose could reach the launch expansion. The exception now applies only to values that start with a drive letter or a UNC prefix. The escape branch also matched \$ without checking for the brace, so VALUE=\$USER} was rewritten to ${dollar}{SER}. It requires the whole \${ now. Whitespace after a closing quote kept a single-quoted value from being seen as quoted, so '${USER}@example.org' followed by spaces was expanded. The value is trimmed before the quotes are read. The case test used PATH, which exists in the process environment on Linux and macOS, so it resolved there and the expected value was wrong. It uses a name that only the initial environment carries. 22 tests pass, and the three new cases fail on the previous commit. --- .../src/Debugger/ParsedEnvironmentFile.ts | 31 ++++++++-------- .../tests/ParsedEnvironmentFile.test.ts | 35 ++++++++++++++++--- 2 files changed, 46 insertions(+), 20 deletions(-) diff --git a/Extension/src/Debugger/ParsedEnvironmentFile.ts b/Extension/src/Debugger/ParsedEnvironmentFile.ts index f0619be99..e0557e77e 100644 --- a/Extension/src/Debugger/ParsedEnvironmentFile.ts +++ b/Extension/src/Debugger/ParsedEnvironmentFile.ts @@ -59,7 +59,13 @@ export class ParsedEnvironmentFile { // A single-quoted value is literal, so a reference inside it must not be // expanded. The quotes come off on the next line, so this has to be read here. - const singleQuoted: boolean = (value.length > 1) && (value.charAt(0) === "'") && (value.charAt(value.length - 1) === "'"); + // Whitespace outside the closing quote is not part of the value, and leaving it + // in hid the quote from the check below. + const trimmed: string = value.replace(/\s+$/, ""); + const singleQuoted: boolean = (trimmed.length > 1) && (trimmed.charAt(0) === "'") && (trimmed.charAt(trimmed.length - 1) === "'"); + if (singleQuoted) { + value = trimmed; + } value = value.replace(/(^['"]|['"]$)/g, ""); @@ -120,6 +126,10 @@ export class ParsedEnvironmentFile { * the launch expansion still sees them. */ private static resolveReferences(value: string, known: (name: string) => string | undefined): string { + // A backslash in front of a reference escapes it, except inside a Windows path, where it + // separates segments instead. Only a drive letter or a UNC prefix makes the value a path: + // in ordinary text \${ is an escape, on any platform. + const windowsPath: boolean = /^[A-Za-z]:[\\/]/.test(value) || /^\\\\/.test(value); let result: string = ""; let i: number = 0; while (i < value.length) { @@ -127,9 +137,10 @@ export class ParsedEnvironmentFile { // An escaped reference is literal. ${dollar} is the form the launch expansion leaves // alone and collapses back to a plain $ once it is done, so the debuggee is the only - // place that sees the literal. - if (char === "\\" && value.charAt(i + 1) === "$" && ParsedEnvironmentFile.isReferenceEscape(value, i)) { - const end: number = value.indexOf("}", i + 2); + // place that sees the literal. The whole opener has to be there: \$USER} is not a + // reference and must not be rewritten. + if (char === "\\" && value.charAt(i + 1) === "$" && value.charAt(i + 2) === "{" && !windowsPath) { + const end: number = value.indexOf("}", i + 3); if (end !== -1) { result += `\${dollar}{${value.substring(i + 3, end)}}`; i = end + 1; @@ -155,18 +166,6 @@ export class ParsedEnvironmentFile { return result; } - /** - * A backslash only escapes the reference where it cannot be a path separator instead. In - * C:\Users\${env:USERNAME}\project the backslash separates path segments, so the reference - * still expands. At the start of a value, or after =, a quote or a space, it is an escape. - */ - private static isReferenceEscape(value: string, index: number): boolean { - if (index === 0) { - return true; - } - return !/[A-Za-z0-9_.\-:\/\\]/.test(value.charAt(index - 1)); - } - /** * Environment variable names are case-insensitive on Windows, and the launch configuration * may have spelled the name differently than the reference does, so Path and PATH have to diff --git a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts index b35e43226..185374ee1 100644 --- a/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts +++ b/Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts @@ -213,13 +213,40 @@ EMAIL=\${USER}@example.org`; test("Match an initial environment name regardless of case on Windows", () => { // Path and PATH are the same variable on Windows, and the launch configuration may use - // either spelling. Elsewhere the reference stays bare for the launch expansion. - const content: string = `COPY=\${PATH}`; + // either spelling. The name is not in the process environment, so off Windows the + // reference is left bare for the launch expansion instead of resolving to a host value. + const content: string = `COPY=\${TEST_CASE_FOLDER}`; const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent( - content, "TestEnvFileName", [{ name: "Path", value: "C:\\override" }]); + content, "TestEnvFileName", [{ name: "test_case_folder", value: "from-config" }]); - const expected: string = process.platform === "win32" ? "C:\\override" : "${PATH}"; + const expected: string = process.platform === "win32" ? "from-config" : "${TEST_CASE_FOLDER}"; assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); assertEnvironmentEqual(result.Env, "COPY", expected); }); + + test("Keep an escaped reference inside ordinary text literal", () => { + // The backslash is only a path separator inside a Windows path. In prose it escapes. + const content: string = `USER=admin\nEMAIL=prefix\\\${USER}@example.org`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "prefix${dollar}{USER}@example.org"); + }); + + test("Leave a lone escaped dollar alone", () => { + // There is no reference here, so there is nothing to rewrite. + const content: string = `VALUE=\\$USER}`; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "VALUE", "\\$USER}"); + }); + + test("Protect a quoted value with trailing whitespace", () => { + const content: string = `USER=admin\nEMAIL='\${USER}@example.org' `; + const result: ParsedEnvironmentFile = ParsedEnvironmentFile.CreateFromContent(content, "TestEnvFileName", []); + + assert(!result.Warning, `Failed to assert that Warning was empty: ${result.Warning}`); + assertEnvironmentEqual(result.Env, "EMAIL", "${dollar}{USER}@example.org"); + }); });