Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 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 @@ -40,6 +40,12 @@
"contributes": {
"configuration": {
"properties": {
"python-envs.injectEnvVarsInTerminals": {
"type": "boolean",
"default": false,
"description": "%python-envs.injectEnvVarsInTerminals.description%",
"scope": "window"
},
"python-envs.defaultEnvManager": {
"type": "string",
"description": "%python-envs.defaultEnvManager.description%",
Expand Down
1 change: 1 addition & 0 deletions package.nls.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"python-envs.defaultEnvManager.description": "The default environment manager for creating and managing environments.",
"python-envs.injectEnvVarsInTerminals.description": "This setting defaults to false and toggles if environment variables found in the workspace are set for new terminals.",
Comment thread
eleanorjboyd marked this conversation as resolved.
Outdated
"python-envs.defaultPackageManager.description": "The default package manager for installing packages in environments.",
"python-envs.pythonProjects.description": "The list of Python projects.",
"python-envs.pythonProjects.path.description": "The path to a folder or file in the workspace to be treated as a Python project.",
Expand Down
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ export async function activate(context: ExtensionContext): Promise<PythonEnviron
terminalActivation,
shellEnvsProviders,
shellStartupProviders,
context,
);
context.subscriptions.push(terminalActivation, terminalManager);

Expand Down
34 changes: 33 additions & 1 deletion src/features/terminal/terminalManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as fsapi from 'fs-extra';
import * as path from 'path';
import { Disposable, EventEmitter, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
import { Disposable, EventEmitter, ExtensionContext, ProgressLocation, Terminal, TerminalOptions, Uri } from 'vscode';
import { PythonEnvironment, PythonEnvironmentApi, PythonProject, PythonTerminalCreateOptions } from '../../api';
import { ActivationStrings } from '../../common/localize';
import { traceInfo, traceVerbose } from '../../common/logging';
Expand Down Expand Up @@ -84,6 +84,7 @@ export class TerminalManagerImpl implements TerminalManager {
private readonly ta: TerminalActivationInternal,
private readonly startupEnvProviders: ShellEnvsProvider[],
private readonly startupScriptProviders: ShellStartupScriptProvider[],
private readonly context: ExtensionContext,
) {
this.disposables.push(
this.onTerminalOpenedEmitter,
Expand All @@ -105,6 +106,7 @@ export class TerminalManagerImpl implements TerminalManager {
env = await getEnvironmentForTerminal(api, t);
}
if (env) {
await this.injectEnvironmentVariables(env);
await this.autoActivateOnTerminalOpen(t, env);
}
}),
Expand Down Expand Up @@ -212,6 +214,29 @@ export class TerminalManagerImpl implements TerminalManager {
return ACT_TYPE_COMMAND;
}

private async injectEnvironmentVariables(environment: PythonEnvironment): Promise<void> {
const config = getConfiguration('python-envs');
const inject = config.get<boolean>('injectEnvVarsInTerminals', false);
if (!inject) {
traceInfo('Skipping environment variable injection based on `injectEnvVarsInTerminals` setting.');
return;
}
const api = await getPythonApi();
const project = api.getPythonProject(environment.environmentPath);
if (project) {
const envVars = await api.getEnvironmentVariables(project.uri);
const collection = this.context.environmentVariableCollection;
// clear the env var collection to remove any existing env vars
collection.clear();
for (const [key, value] of Object.entries(envVars)) {
if (value === undefined) {
continue; // Skip undefined values
}
collection.append(key, value);
}
}
}

private async autoActivateOnTerminalOpen(terminal: Terminal, environment: PythonEnvironment): Promise<void> {
let actType = getAutoActivationType();
const shellType = identifyTerminalShell(terminal);
Expand Down Expand Up @@ -285,6 +310,7 @@ export class TerminalManagerImpl implements TerminalManager {
// We add it to skip activation on open to prevent double activation.
// We can activate it ourselves since we are creating it.
this.skipActivationOnOpen.add(newTerminal);
await this.injectEnvironmentVariables(environment);
await this.autoActivateOnTerminalOpen(newTerminal, environment);
}

Expand Down Expand Up @@ -369,6 +395,12 @@ export class TerminalManagerImpl implements TerminalManager {
}

public async initialize(api: PythonEnvironmentApi): Promise<void> {
for (const t of terminals()) {
Comment thread
eleanorjboyd marked this conversation as resolved.
Outdated
const env = this.ta.getEnvironment(t) ?? (await getEnvironmentForTerminal(api, t));
if (env) {
await this.injectEnvironmentVariables(env);
}
}
const actType = getAutoActivationType();
if (actType === ACT_TYPE_COMMAND) {
await Promise.all(terminals().map(async (t) => this.activateUsingCommand(api, t)));
Expand Down