Skip to content

Fix variable interpolation in .env files loaded through envFile - #14828

Open
Abdelhadi Salmaoui (abdelhadi703) wants to merge 14 commits into
microsoft:mainfrom
abdelhadi703:fix/envfile-interpolation
Open

Abdelhadi Salmaoui (abdelhadi703) wants to merge 14 commits into
microsoft:mainfrom
abdelhadi703:fix/envfile-interpolation

Conversation

@abdelhadi703

@abdelhadi703 Abdelhadi Salmaoui (abdelhadi703) commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

.env files are supposed to support variable interpolation, but when such a file is
loaded through envFile, the values keep their ${...} references and the variable is
never substituted.

With a file like

USER=admin
EMAIL=${USER}@example.org

EMAIL came out as the literal ${USER}@example.org. Later, when the debug
configuration gets expanded, ${USER} is looked up as a launch.json variable, isn't
found, and prints "Invalid variable reference ${USER} in string". The env var that was
meant to land in the debuggee doesn't.

I made ParsedEnvironmentFile resolve ${VAR} while it parses each line, against the
variables 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 variables
keep 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, so
they stay literal now.

Keeping them literal takes more than emitting them unchanged. This parser's output goes through
expandAllStrings immediately afterwards (configurationProvider.expand), which substitutes
every ${...} it knows and warns about the rest, so a bare ${VAR} either got replaced from
the 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 that
expansion leaves alone and collapses to a plain $ once it is done (expand.ts), so the
debuggee is the only place that sees the literal.

Seven tests were added to the existing ParsedEnvironmentFile suite: a reference to a variable
defined 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 ParsedEnvironmentFile suite against the parser alone, compiled with the
repo's TypeScript and a local vscode-nls stand-in (mocha 10 does not start on Node 26, so the
compiled 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 extension
does 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 this
branch the single-quoted value came out as admin@example.org, because the expansion took
${USER} from the launch variables.

The path through extendDebugConfiguration itself needs the extension host and the packages
from the private feed, so it is not covered here.

Fixes #13781.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

@abdelhadi703

Abdelhadi Salmaoui (abdelhadi703) commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor Author

Both addressed in 66ea67b.

The quotes came off before the references were resolved, so you are right on both counts: VALUE='${SECRET}' was expanded, and \${SECRET} lost its meaning while keeping its backslash. Both are literal in dotenv, and both were literal here before the interpolation existed, so they stay as written now -- the backslash is dropped, leaving the text ${VAR}. Two tests cover the two forms.

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 finally.

13 passing against the first commit of this branch. The two literal tests fail there -- admin@example.org where ${USER}@example.org is expected for the single-quoted one, and \admin@example.org for the escaped one -- and the other 11 pass either way.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Literal references are exposed to subsequent launch-variable expansion instead of reaching the debuggee unchanged.

2 open findings
2 resolved since last review

🧠 Review effort: Balanced

Comment thread Extension/CHANGELOG.md Resolved
@abdelhadi703

Copy link
Copy Markdown
Contributor Author

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 resolveVariables is only applied to envFile and sourceFileMap. I missed that resolveEnvFile is followed by expand(config, folder) two lines further down, and that expandAllStrings walks every environment value. So a bare ${VAR} was never going to survive: it gets taken from the launch variables when one matches, and reported as "Invalid variable reference" when none does -- the exact symptom this change exists to remove. Emitting the literal unchanged only moved the problem.

Both literal forms are now written back as ${dollar}{VAR}, the form that expansion skips and collapses to a plain $ at the end.

Run through the expansion rule from expand.ts, the single-quoted and escaped values both reach the debuggee as ${USER}@example.org with no warning, while ${workspaceFolder} still resolves from the debug configuration. On the previous commit the single-quoted value came out as admin@example.org, straight from the launch variables.

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 extendDebugConfiguration itself needs the extension host and the private feed's packages, so it is not covered here.

@abdelhadi703

Copy link
Copy Markdown
Contributor Author

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: ${VAR} references inside a .env file are now resolved, with single-quoted values and \${VAR} left as written, and there is a test for each case.

@abdelhadi703

Copy link
Copy Markdown
Contributor Author

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.

@abdelhadi703

Copy link
Copy Markdown
Contributor Author

Rebased onto main (04acc76f); no conflict. Two files, and the CHANGELOG is left alone as you asked on the other one.

The new cases pass: ParsedEnvironmentFile.test.ts, 14 passing. I ran that file on its own with mocha and tsx rather than the extension host — the module only needs fs and vscode-nls — and changed nothing but the import style in my local copy, since import * as assert is not callable outside the compiled extension.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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

@abdelhadi703

Copy link
Copy Markdown
Contributor Author

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:

Error: illegal state - object is disposed
  at i._checkDisposed (...)
  at i.clear (...)
  at CppProperties.handleSquigglesImpl (Extension/dist/src/LanguageServer/configurations.js:1878:39)

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 Extension/src/Debugger/ParsedEnvironmentFile.ts and its test, nothing under LanguageServer/, and it sits on the current main (9e3516f). Unit tests and the SimpleCppProject scenario pass on the same commit, and Linux and Mac are green. If this is a known Windows flake in that scenario then it is not mine to chase, I just did not want it read as a regression from the .env change.

@sean-mcmanus

Copy link
Copy Markdown
Contributor

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:

Error: illegal state - object is disposed
  at i._checkDisposed (...)
  at i.clear (...)
  at CppProperties.handleSquigglesImpl (Extension/dist/src/LanguageServer/configurations.js:1878:39)

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 Extension/src/Debugger/ParsedEnvironmentFile.ts and its test, nothing under LanguageServer/, and it sits on the current main (9e3516f). Unit tests and the SimpleCppProject scenario pass on the same commit, and Linux and Mac are green. If this is a known Windows flake in that scenario then it is not mine to chase, I just did not want it read as a regression from the .env change.

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The path heuristic incorrectly expands escaped references in ordinary text and mishandles variable-prefixed Windows paths, dropping separators and leaving variables unexpanded.

1 open issue

Review effort: Balanced

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed to evaluate a Windows case-insensitivity edge case that can cause environment-variable expansion to use stale values.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.
@abdelhadi703

Copy link
Copy Markdown
Contributor Author

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 tools\${env:USERNAME}\bin and .\${env:USERNAME}.log keep their separators and expand, unquoted and double-quoted alike. The neighbour heuristic and the path detection are gone with it, which also removes the punctuation and non-ASCII cases that kept coming back.

An inline literal is ${dollar}{VAR}, and ${dollar} is no longer looked up in the file, so a variable named dollar cannot swallow it (your other thread). A whole single-quoted value is literal too; that part is new, and the CHANGELOG says so.

32 tests in the file. The new ones pin .\${env:USERNAME}.log, tools-\${env:USERNAME}\bin, 工具\${env:USERNAME}\bin, both literal forms, and a dollar variable defined in the file. The last one parses the file and then runs expandString over the result, so it checks what the debuggee receives rather than only what the parser produced.

One thing to be aware of: \${USER} is no longer a literal reference. It is a backslash followed by an expansion, and a value that needs the literal is written ${dollar}{USER} or single-quoted whole.

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?

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 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

Comment thread Extension/CHANGELOG.md Resolved
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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because an unpublished finding indicates that differently cased Windows environment overrides can cause variable substitutions to use stale values.

No open issues 🎉

Review effort: Balanced

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Pull Request

Development

Successfully merging this pull request may close these issues.

[bug] launch.json envFile not capable of variable interpolation

3 participants