Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 5, 2025

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:

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


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copilot AI changed the title [WIP] Injecting Environment Variables into Terminal Implement environment variable injection into VS Code terminals using GlobalEnvironmentVariableCollection Aug 5, 2025
Copilot AI requested a review from eleanorjboyd August 5, 2025 16:17
Copilot finished work on behalf of eleanorjboyd August 5, 2025 16:17
@eleanorjboyd
Copy link
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

Copy link
Contributor Author

Copilot AI commented Aug 5, 2025

@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

I've updated the tests to work with the new code and test the new logic. The tests now properly validate:

  • Event handler registration and basic functionality
  • Environment variable change event handling for different scenarios
  • File deletion event handling
  • Global change handling and missing workspace folder scenarios
  • Proper resource disposal and cleanup

Key changes made:

  • Removed file system mocking dependencies that were incompatible with the new implementation
  • Updated mocking strategy to work with the actual scoped environment variable collection logic
  • Added comprehensive tests for the reactive file change handling
  • Tests now verify the class handles various event scenarios without throwing errors

All 14 tests are now passing and cover the new functionality. Commit: 8daae4f

Copilot finished work on behalf of eleanorjboyd August 5, 2025 18:21
Copilot AI requested a review from eleanorjboyd August 5, 2025 18:21
Copilot finished work on behalf of eleanorjboyd August 5, 2025 18:25
@eleanorjboyd eleanorjboyd marked this pull request as ready for review August 5, 2025 22:00
@vs-code-engineering vs-code-engineering bot added this to the August 2025 milestone Aug 5, 2025
@eleanorjboyd eleanorjboyd merged commit 6173146 into main Aug 6, 2025
10 checks passed
@eleanorjboyd eleanorjboyd deleted the copilot/fix-682 branch August 6, 2025 02:27
@anthonykim1
Copy link
Contributor

anthonykim1 commented Aug 6, 2025

@eleanorjboyd Since .env feature isn't working anymore from the old implementation in Python extension, would this be what provides functionality for the already existing setting in Python extension?

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] <[email protected]>
Co-authored-by: eleanorjboyd <[email protected]>
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] <[email protected]>
Co-authored-by: eleanorjboyd <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Injecting Environment Variables into Terminal

4 participants