Skip to content

Fix issue where terminal environment variables are not removed when they are commented out or deleted from .env files.#1131

Merged
anthonykim1 merged 5 commits intomicrosoft:mainfrom
StellaHuang95:envCommentOutIssue
Feb 3, 2026
Merged

Fix issue where terminal environment variables are not removed when they are commented out or deleted from .env files.#1131
anthonykim1 merged 5 commits intomicrosoft:mainfrom
StellaHuang95:envCommentOutIssue

Conversation

@StellaHuang95
Copy link
Copy Markdown
Contributor

Fixes #936

The problem this PR fixed is when users comment out or remove a variable from their .env file:

API_KEY=secret
# DEBUG=true   ← commented out

The DEBUG variable remained in terminal environments because:

  1. dotenv.parse() ignores commented lines (seems the right behavior per dotenv spec)
  2. The returned object only contained active variables: { API_KEY: 'secret' }
  3. The old code only iterated over keys in envVars and it never saw DEBUG
  4. The delete() call was never reached for removed/commented variables

Solution is to call clear() before re-injecting variables, which makes the fix stays at the terminal injection layer rather than modifying the parsing layer, avoiding impacting on other consumers.

I also noticed some other issues with setting watcher, currently there's no file watcher watching the settings change, so a reload will be required when toggling the setting. We can consider adding a config listener.

@StellaHuang95 StellaHuang95 added the bug Issue identified by VS Code Team member as probable bug label Jan 30, 2026
@vs-code-engineering vs-code-engineering bot added this to the February 2026 milestone Feb 3, 2026
@anthonykim1 anthonykim1 merged commit 5e4a386 into microsoft:main Feb 3, 2026
7 checks passed
@StellaHuang95 StellaHuang95 deleted the envCommentOutIssue branch February 5, 2026 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issue identified by VS Code Team member as probable bug

3 participants