Skip to content

Conversation

@StellaHuang95
Copy link
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
`TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`,
);
// Clear any previously set variables when injection is disabled
envVarScope.clear();
Copy link
Contributor

@anthonykim1 anthonykim1 Jan 30, 2026

Choose a reason for hiding this comment

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

Can you test this with python-envs.terminal.autoActivationType set to shell Startup?
We don't want to clear variables from shell startup as that is what allows auto-activation for shell startup.

My concern is that calling clear on this will also clear activation related variable as well. These are variables with VSCODE_PYTHON_(SHELL)_ACTIVATE

`TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`,
);
// Clear any previously set variables when injection is disabled
envVarScope.clear();
Copy link
Member

Choose a reason for hiding this comment

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

I think there is a potential issue here since when envVarScope.clear() is called on the scoped collection, it clears ALL variables in that workspace scope - not just the .env variables, but also the shell activation variables set by ShellStartupActivationVariablesManager. If you could review and confirm if this is the case or not that would be great!

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

.env File no longer updates commented variables

3 participants