Repository navigation
Fix variable interpolation in .env files loaded through envFile - #14828
Abdelhadi Salmaoui (abdelhadi703) wants to merge 14 commits into
Conversation
There was a problem hiding this comment.
🟡 Changes recommended
Interpolation incorrectly expands quoted or escaped literals, and the tests are incomplete and environment-dependent.
2 open findings
What changed in this PR
Adds dotenv-style ${VAR} interpolation for debugger envFile parsing.
Changes:
- Resolves references from prior entries and the process environment.
- Preserves unknown references.
- Adds tests and changelog documentation.
| File | Description |
|---|---|
Extension/src/Debugger/ParsedEnvironmentFile.ts |
Implements interpolation. |
Extension/test/scenarios/SingleRootProject/tests/ParsedEnvironmentFile.test.ts |
Adds interpolation tests. |
Extension/CHANGELOG.md |
Documents the fix. |
🧠 Review effort: Balanced
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
|
Both addressed in 66ea67b. The quotes came off before the references were resolved, so you are right on both counts: The process-environment fallback has a test now as well: it sets a name of its own, parses a reference to it, and restores the previous value in a 13 passing against the first commit of this branch. The two literal tests fail there -- |
|
You are right, and I had this wrong. I went looking for a later expansion before writing the previous commit and concluded there was none, because Both literal forms are now written back as Run through the expansion rule from On the wording finding: the changelog entry is gone rather than corrected, for the same reason as the other PR -- those are written separately before a release. 14 tests pass. The path through |
e34efbf to
cc80921
Compare
|
Rebased on main and added the CHANGELOG line under 1.35.6 - CONTRIBUTING asks for it and I had left it out. Build, CodeQL and the CLA check are green, so the only thing left is a review. Sean McManus (@sean-mcmanus), you seem to be merging most of what goes through here - would you take a look when you have a minute? The change is in the debugger's envFile parsing: |
cc80921 to
8721998
Compare
|
Dropped the CHANGELOG line - Sean McManus (@sean-mcmanus) mentioned on another PR that you write it yourselves before the release, so CONTRIBUTING is out of date on that point. The diff is just the parsing change and its tests now. |
8721998 to
dcb6f4c
Compare
|
Rebased onto The new cases pass: |
There was a problem hiding this comment.
🟡 Changes recommended
Literal protection fails for colon-qualified references in single-quoted or escaped values, allowing unintended environment expansion and even command execution from single-quoted values.
1 open issue
2 resolved since last review
Review effort: Balanced
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.
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.
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.
dcb6f4c to
1f8a192
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Trailing whitespace after a closing quote causes single-quoted environment values to be misclassified as unquoted, leading to unintended interpolation and malformed output.
1 open issue
1 resolved since last review
Review effort: Balanced
|
The Windows job that failed is the E2E IntelliSense step, and all 12 failures sit in MultirootDeadlockTest. They start at the before-all hook with "First workspace folder is unavailable", right after this in the log: A diagnostics collection got disposed mid-run, and everything after it comes back empty: 0 !== 12 for the inlay hints, undefined contents for the hovers, 0 == 2 for the references. Mentioning it because this branch only touches |
Yes we see this random failure in our internal runs as well, also repros for Linux (not Mac so far). I'm looking into fixing it. |
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because subtle Windows-specific casing and path-expansion interactions can produce stale or incorrectly resolved environment values.
No open issues 🎉
4 resolved since last review
Review effort: Balanced
Restricting the path check to a drive letter and UNC made a relative Windows path
look like ordinary text, so the backslash before a reference was taken as an escape:
.\${env:USERNAME}\bin came out as .${env:USERNAME}\bin, losing the separator and
leaving the reference unresolved.
A relative opener and any other backslash in the value make it a path as well, so
only prose keeps the escape.
Judging the whole value could not work both ways: prose with a regex\w in it made
prefix\${USER} expand, and a path built out of references, ${workspaceFolder}\${env:USERNAME},
lost its separator.
Each backslash is judged by its neighbours now. It separates when it follows a
closed reference, a dot or a separator, or when the value goes on with a separator
after the reference. A drive or UNC value keeps them all.
tools\ had nothing after the reference, so nothing marked it as a path and the separator was dropped. A path component in front and either the end of the value or a separator behind now decide, which still leaves prose escaping.
There was a problem hiding this comment.
🟡 Changes recommended
The separator heuristic corrupts valid Windows paths and prevents environment-variable expansion, requiring a fix and regression coverage for suffixes, punctuation, and non-ASCII path components.
1 open issue
Review effort: Balanced
Expansion of ${VAR} in an envFile value was decided by looking at the
characters around the reference, to tell a Windows path from prose. Every
attempt to widen it either ate a separator in a path built from references
or expanded a reference in text that happened to carry another backslash,
and neither can be told from the other: .\${env:USERNAME}.log and
prefix\${USER}@example.org differ only by what follows the brace.
The decision on the review is that the backslash is always an ordinary
character, so a reference behind it expands like any other and the
separator stays. A literal reference is now written ${dollar}{VAR}, or as a
whole single-quoted value, and ${dollar} is no longer looked up in the file
so a variable of that name cannot swallow it.
Tests cover both literal forms, the quoted and unquoted path cases with a
suffix and a non-ASCII component, and the parser followed by the launch
expansion.
|
Implemented the first option, in 780a0c7. A backslash is an ordinary character in a value now: it separates path segments and never escapes a reference, so An inline literal is 32 tests in the file. The new ones pin One thing to be aware of: The CI runs for this push are waiting for approval on your side - could you let them through so they can confirm the tests run where they actually run? |
There was a problem hiding this comment.
🔵 Needs a closer look
Human assessment is needed because Windows environment-variable overrides can resolve inconsistently across letter casing, a concern omitted from the published review.
No open issues 🎉
1 resolved since last review
Review effort: Balanced
1.36.0 is out, and entries are collected in the release-preparation change rather than in the fix PRs. Dropping the line leaves the file as it was.
A single-quoted value is text, so a ${dollar}{...} typed between the quotes has
to reach the debuggee as written. The replacement skipped those, on the idea
that they were already protected, but the launch expansion still collapses one
marker at the end, so ${dollar}{USER} came out as ${USER}.
Every ${ in the quoted value is protected now, and the collapse removes exactly
one marker, which leaves what was typed.
Signed-off-by: Abdelhadi Salmaoui <abdelhadisalmaoui0909@outlook.fr>
The initial environment carries Path while the file writes PATH. On Windows those
are one variable, but both entries stayed in the map and a later ${Path} read the
old value by exact match, so the file's definition was ignored.
The file now drops an entry whose name matches the one it writes case-insensitively,
which is what the platform does when a variable is set twice.
Signed-off-by: Abdelhadi Salmaoui <abdelhadisalmaoui0909@outlook.fr>



.envfiles are supposed to support variable interpolation, but when such a file isloaded through
envFile, the values keep their${...}references and the variable isnever substituted.
With a file like
EMAILcame out as the literal${USER}@example.org. Later, when the debugconfiguration gets expanded,
${USER}is looked up as alaunch.jsonvariable, isn'tfound, and prints "Invalid variable reference ${USER} in string". The env var that was
meant to land in the debuggee doesn't.
I made
ParsedEnvironmentFileresolve${VAR}while it parses each line, against thevariables already read from the file plus the process environment. A reference to a name we
don't know is left untouched, so
${workspaceFolder}and the other launch.json variableskeep working exactly as before.
Only a reference the value actually asked to expand is expanded. A single-quoted value and a
\${VAR}reference are literal in dotenv, and both were literal here before this change, sothey stay literal now.
Keeping them literal takes more than emitting them unchanged. This parser's output goes through
expandAllStringsimmediately afterwards (configurationProvider.expand), which substitutesevery
${...}it knows and warns about the rest, so a bare${VAR}either got replaced fromthe launch configuration or printed the same "Invalid variable reference" warning the issue is
about. Both literal forms are written back as
${dollar}{VAR}instead -- the one form thatexpansion leaves alone and collapses to a plain
$once it is done (expand.ts), so thedebuggee is the only place that sees the literal.
Seven tests were added to the existing
ParsedEnvironmentFilesuite: a reference to a variabledefined earlier in the same file, a reference coming from the initial environment, an unknown
reference staying literal, a reference resolved from the process environment, the two literal
forms, and a resolved reference still coming out bare.
Testing: ran the
ParsedEnvironmentFilesuite against the parser alone, compiled with therepo's TypeScript and a local
vscode-nlsstand-in (mocha 10 does not start on Node 26, so thecompiled file runs under a short TDD shim). 14 passing with this change.
I also ran that output through the expansion rule from
expand.ts, which is what the extensiondoes next. Both literal forms reach the debuggee as
${USER}with no warning, while${workspaceFolder}still resolves from the debug configuration. On the previous commit of thisbranch the single-quoted value came out as
admin@example.org, because the expansion took${USER}from the launch variables.The path through
extendDebugConfigurationitself needs the extension host and the packagesfrom the private feed, so it is not covered here.
Fixes #13781.