Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@
"%python-envs.terminal.autoActivationType.off%"
],
"scope": "machine"
},
"python.terminal.useEnvFile": {
"type": "boolean",
"description": "%python-envs.terminal.useEnvFile.description%",
"default": false,
"scope": "resource"
}
}
},
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"python-envs.terminal.autoActivationType.command": "Activation by executing a command in the terminal.",
"python-envs.terminal.autoActivationType.shellStartup": "Activation by modifying the terminal shell startup script. To use this feature we will need to modify your shell startup scripts.",
"python-envs.terminal.autoActivationType.off": "No automatic activation of environments.",
"python-envs.terminal.useEnvFile.description": "Controls whether environment variables from .env files and python.envFile setting are injected into terminals.",
"python-envs.terminal.revertStartupScriptChanges.title": "Revert Shell Startup Script Changes",
"python-envs.reportIssue.title": "Report Issue",
"python-envs.setEnvManager.title": "Set Environment Manager",
Expand Down
39 changes: 38 additions & 1 deletion src/features/terminal/terminalEnvVarInjector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Disposable,
EnvironmentVariableScope,
GlobalEnvironmentVariableCollection,
window,
workspace,
WorkspaceFolder,
} from 'vscode';
Expand Down Expand Up @@ -55,6 +56,18 @@ export class TerminalEnvVarInjector implements Disposable {
return;
}

// Check if env file injection is enabled when variables change
const config = getConfiguration('python', args.uri);
const useEnvFile = config.get<boolean>('terminal.useEnvFile', false);
const envFilePath = config.get<string>('envFile');

// Only show notification when env vars change and we have an env file but injection is disabled
if (!useEnvFile && envFilePath) {
window.showInformationMessage(
'An environment file is configured but terminal environment injection is disabled. Enable "python.terminal.useEnvFile" to use environment variables from .env files in terminals.',
);
}

if (args.changeType === 2) {
// FileChangeType.Deleted
this.clearWorkspaceVariables(affectedWorkspace);
Expand All @@ -66,6 +79,20 @@ export class TerminalEnvVarInjector implements Disposable {
}),
);

// Listen for changes to the python.envFile setting
this.disposables.push(
workspace.onDidChangeConfiguration((e) => {
if (e.affectsConfiguration('python.envFile')) {
traceVerbose(
'TerminalEnvVarInjector: python.envFile setting changed, updating environment variables',
);
this.updateEnvironmentVariables().catch((error) => {
traceError('Failed to update environment variables:', error);
});
}
}),
);

// Initial load of environment variables
await this.updateEnvironmentVariables();
}
Expand Down Expand Up @@ -115,9 +142,19 @@ export class TerminalEnvVarInjector implements Disposable {
const envVarScope = this.getEnvironmentVariableCollectionScoped({ workspaceFolder });
envVarScope.clear(); // Clear existing variables for this workspace

// Track which .env file is being used for logging
// Check if env file injection is enabled
const config = getConfiguration('python', workspaceUri);
const useEnvFile = config.get<boolean>('terminal.useEnvFile', false);
const envFilePath = config.get<string>('envFile');

if (!useEnvFile) {
traceVerbose(
`TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`,
);
return;
}

// Track which .env file is being used for logging
const resolvedEnvFilePath: string | undefined = envFilePath
? path.resolve(resolveVariables(envFilePath, workspaceUri))
: undefined;
Expand Down
Loading