diff --git a/package.json b/package.json index eb6cb461..31887629 100644 --- a/package.json +++ b/package.json @@ -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" } } }, diff --git a/package.nls.json b/package.nls.json index 2a62f1c3..0885ff5b 100644 --- a/package.nls.json +++ b/package.nls.json @@ -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", diff --git a/src/features/terminal/terminalEnvVarInjector.ts b/src/features/terminal/terminalEnvVarInjector.ts index 4bf48039..3f08a0cd 100644 --- a/src/features/terminal/terminalEnvVarInjector.ts +++ b/src/features/terminal/terminalEnvVarInjector.ts @@ -7,6 +7,7 @@ import { Disposable, EnvironmentVariableScope, GlobalEnvironmentVariableCollection, + window, workspace, WorkspaceFolder, } from 'vscode'; @@ -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('terminal.useEnvFile', false); + const envFilePath = config.get('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); @@ -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(); } @@ -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('terminal.useEnvFile', false); const envFilePath = config.get('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;