Implement environment variable injection into VS Code terminals using GlobalEnvironmentVariableCollection#683
Merged
eleanorjboyd merged 11 commits intomainfrom Aug 6, 2025
Merged
Conversation
…vironmentVariableCollection Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Injecting Environment Variables into Terminal
Implement environment variable injection into VS Code terminals using GlobalEnvironmentVariableCollection
Aug 5, 2025
Member
|
@copilot I have made a variety of changes and updates to the logic and code. Please update the tests to work with the new code and test this new logic |
eleanorjboyd
reviewed
Aug 5, 2025
…ogic Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
Contributor
Author
I've updated the tests to work with the new code and test the new logic. The tests now properly validate:
Key changes made:
All 14 tests are now passing and cover the new functionality. Commit: 8daae4f |
eleanorjboyd
approved these changes
Aug 5, 2025
DonJayamanne
approved these changes
Aug 6, 2025
Contributor
|
@eleanorjboyd Since |
eleanorjboyd
added a commit
to eleanorjboyd/vscode-python-environments
that referenced
this pull request
Aug 11, 2025
… GlobalEnvironmentVariableCollection (microsoft#683) This PR implements reactive environment variable injection into VS Code terminals using the `GlobalEnvironmentVariableCollection` API, enabling workspace-specific environment variables to be automatically available in all terminal sessions. ## Implementation Overview **New `TerminalEnvVarInjector` class** that: - Uses VS Code's `GlobalEnvironmentVariableCollection` to inject workspace environment variables into terminals - Integrates with the existing `PythonEnvVariableManager` to retrieve environment variables with proper precedence - Responds reactively to changes in `.env` files and `python.envFile` settings - Provides comprehensive logging at decision points using `traceVerbose` and `traceError` ## Key Features **Startup Behavior:** ```typescript // On extension startup, automatically loads and injects environment variables const envVars = await envVarManager.getEnvironmentVariables(workspaceUri); envVarCollection.clear(); for (const [key, value] of Object.entries(envVars)) { if (value !== process.env[key]) { envVarCollection.replace(key, value); } } ``` **Reactive Updates:** - **File changes**: Watches for changes to `.env` files through existing `onDidChangeEnvironmentVariables` event - **Setting changes**: Listens for changes to `python.envFile` configuration and switches to new files automatically - **Multi-workspace**: Handles multiple workspace folders by processing each separately **Smart Injection:** - Only injects variables that differ from `process.env` to avoid redundancy - Clears collection before re-injecting to ensure clean state - Gracefully handles missing files and configuration errors ## Integration Points - **Extension startup**: Integrated into `extension.ts` activation - **Existing infrastructure**: Uses `PythonEnvVariableManager.getEnvironmentVariables()` for consistent behavior - **Resource management**: Proper disposal and cleanup of watchers and subscriptions ## Testing Added comprehensive unit tests covering: - Environment variable injection on startup - Reactive updates to file and setting changes - Error handling for missing files and invalid configurations - Multi-workspace scenarios - Proper resource disposal This implementation follows VS Code extension best practices and provides the foundation for workspace-specific terminal environment configuration. Fixes microsoft#682. <!-- START COPILOT CODING AGENT TIPS --> --- 💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click [here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to start the survey. --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
NguyenCuong1989
referenced
this pull request
in NguyenCuong1989/vscode-python-environments
Oct 23, 2025
… GlobalEnvironmentVariableCollection (#683)
This PR implements reactive environment variable injection into VS Code
terminals using the `GlobalEnvironmentVariableCollection` API, enabling
workspace-specific environment variables to be automatically available
in all terminal sessions.
## Implementation Overview
**New `TerminalEnvVarInjector` class** that:
- Uses VS Code's `GlobalEnvironmentVariableCollection` to inject
workspace environment variables into terminals
- Integrates with the existing `PythonEnvVariableManager` to retrieve
environment variables with proper precedence
- Responds reactively to changes in `.env` files and `python.envFile`
settings
- Provides comprehensive logging at decision points using `traceVerbose`
and `traceError`
## Key Features
**Startup Behavior:**
```typescript
// On extension startup, automatically loads and injects environment variables
const envVars = await envVarManager.getEnvironmentVariables(workspaceUri);
envVarCollection.clear();
for (const [key, value] of Object.entries(envVars)) {
if (value !== process.env[key]) {
envVarCollection.replace(key, value);
}
}
```
**Reactive Updates:**
- **File changes**: Watches for changes to `.env` files through existing
`onDidChangeEnvironmentVariables` event
- **Setting changes**: Listens for changes to `python.envFile`
configuration and switches to new files automatically
- **Multi-workspace**: Handles multiple workspace folders by processing
each separately
**Smart Injection:**
- Only injects variables that differ from `process.env` to avoid
redundancy
- Clears collection before re-injecting to ensure clean state
- Gracefully handles missing files and configuration errors
## Integration Points
- **Extension startup**: Integrated into `extension.ts` activation
- **Existing infrastructure**: Uses
`PythonEnvVariableManager.getEnvironmentVariables()` for consistent
behavior
- **Resource management**: Proper disposal and cleanup of watchers and
subscriptions
## Testing
Added comprehensive unit tests covering:
- Environment variable injection on startup
- Reactive updates to file and setting changes
- Error handling for missing files and invalid configurations
- Multi-workspace scenarios
- Proper resource disposal
This implementation follows VS Code extension best practices and
provides the foundation for workspace-specific terminal environment
configuration.
Fixes #682.
<!-- START COPILOT CODING AGENT TIPS -->
---
💬 Share your feedback on Copilot coding agent for the chance to win a
$200 gift card! Click
[here](https://survey.alchemer.com/s3/8343779/Copilot-Coding-agent) to
start the survey.
---------
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: eleanorjboyd <26030610+eleanorjboyd@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR implements reactive environment variable injection into VS Code terminals using the
GlobalEnvironmentVariableCollectionAPI, enabling workspace-specific environment variables to be automatically available in all terminal sessions.Implementation Overview
New
TerminalEnvVarInjectorclass that:GlobalEnvironmentVariableCollectionto inject workspace environment variables into terminalsPythonEnvVariableManagerto retrieve environment variables with proper precedence.envfiles andpython.envFilesettingstraceVerboseandtraceErrorKey Features
Startup Behavior:
Reactive Updates:
.envfiles through existingonDidChangeEnvironmentVariableseventpython.envFileconfiguration and switches to new files automaticallySmart Injection:
process.envto avoid redundancyIntegration Points
extension.tsactivationPythonEnvVariableManager.getEnvironmentVariables()for consistent behaviorTesting
Added comprehensive unit tests covering:
This implementation follows VS Code extension best practices and provides the foundation for workspace-specific terminal environment configuration.
Fixes #682.
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.