Skip to content

Commit f78c997

Browse files
authored
add useEnvFile setting (#710)
1 parent 7b2fb86 commit f78c997

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,12 @@
103103
"%python-envs.terminal.autoActivationType.off%"
104104
],
105105
"scope": "machine"
106+
},
107+
"python.terminal.useEnvFile": {
108+
"type": "boolean",
109+
"description": "%python-envs.terminal.useEnvFile.description%",
110+
"default": false,
111+
"scope": "resource"
106112
}
107113
}
108114
},

package.nls.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"python-envs.terminal.autoActivationType.command": "Activation by executing a command in the terminal.",
1111
"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.",
1212
"python-envs.terminal.autoActivationType.off": "No automatic activation of environments.",
13+
"python-envs.terminal.useEnvFile.description": "Controls whether environment variables from .env files and python.envFile setting are injected into terminals.",
1314
"python-envs.terminal.revertStartupScriptChanges.title": "Revert Shell Startup Script Changes",
1415
"python-envs.reportIssue.title": "Report Issue",
1516
"python-envs.setEnvManager.title": "Set Environment Manager",

src/features/terminal/terminalEnvVarInjector.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Disposable,
88
EnvironmentVariableScope,
99
GlobalEnvironmentVariableCollection,
10+
window,
1011
workspace,
1112
WorkspaceFolder,
1213
} from 'vscode';
@@ -55,6 +56,18 @@ export class TerminalEnvVarInjector implements Disposable {
5556
return;
5657
}
5758

59+
// Check if env file injection is enabled when variables change
60+
const config = getConfiguration('python', args.uri);
61+
const useEnvFile = config.get<boolean>('terminal.useEnvFile', false);
62+
const envFilePath = config.get<string>('envFile');
63+
64+
// Only show notification when env vars change and we have an env file but injection is disabled
65+
if (!useEnvFile && envFilePath) {
66+
window.showInformationMessage(
67+
'An environment file is configured but terminal environment injection is disabled. Enable "python.terminal.useEnvFile" to use environment variables from .env files in terminals.',
68+
);
69+
}
70+
5871
if (args.changeType === 2) {
5972
// FileChangeType.Deleted
6073
this.clearWorkspaceVariables(affectedWorkspace);
@@ -66,6 +79,20 @@ export class TerminalEnvVarInjector implements Disposable {
6679
}),
6780
);
6881

82+
// Listen for changes to the python.envFile setting
83+
this.disposables.push(
84+
workspace.onDidChangeConfiguration((e) => {
85+
if (e.affectsConfiguration('python.envFile')) {
86+
traceVerbose(
87+
'TerminalEnvVarInjector: python.envFile setting changed, updating environment variables',
88+
);
89+
this.updateEnvironmentVariables().catch((error) => {
90+
traceError('Failed to update environment variables:', error);
91+
});
92+
}
93+
}),
94+
);
95+
6996
// Initial load of environment variables
7097
await this.updateEnvironmentVariables();
7198
}
@@ -115,9 +142,19 @@ export class TerminalEnvVarInjector implements Disposable {
115142
const envVarScope = this.getEnvironmentVariableCollectionScoped({ workspaceFolder });
116143
envVarScope.clear(); // Clear existing variables for this workspace
117144

118-
// Track which .env file is being used for logging
145+
// Check if env file injection is enabled
119146
const config = getConfiguration('python', workspaceUri);
147+
const useEnvFile = config.get<boolean>('terminal.useEnvFile', false);
120148
const envFilePath = config.get<string>('envFile');
149+
150+
if (!useEnvFile) {
151+
traceVerbose(
152+
`TerminalEnvVarInjector: Env file injection disabled for workspace: ${workspaceUri.fsPath}`,
153+
);
154+
return;
155+
}
156+
157+
// Track which .env file is being used for logging
121158
const resolvedEnvFilePath: string | undefined = envFilePath
122159
? path.resolve(resolveVariables(envFilePath, workspaceUri))
123160
: undefined;

0 commit comments

Comments
 (0)