@@ -121,12 +121,12 @@ export const TERMINAL_ENV_SETTING_NAME =
121
121
* @returns the value of the applicable `terminal.integrated.env.*` setting,
122
122
* without evaluation of macros such as `${env:...}`.
123
123
*/
124
- export function getTerminalEnv ( ) {
124
+ export function getTerminalEnv ( ) : { [ name : string ] : string | null } {
125
125
const custom_env = vscode . workspace
126
126
. getConfiguration ( )
127
- . get < { [ name : string ] : string } > ( TERMINAL_ENV_SETTING_NAME ) ;
127
+ . get < { [ name : string ] : string | null } > ( TERMINAL_ENV_SETTING_NAME ) ;
128
128
129
- return custom_env ;
129
+ return custom_env ?? { } ;
130
130
}
131
131
132
132
/**
@@ -139,11 +139,18 @@ export function getEvaluatedTerminalEnv() {
139
139
140
140
if ( custom_env ) {
141
141
for ( const var_name in custom_env ) {
142
- // Substitute VS Code variable references that might be present
143
- // in the JSON settings configuration (e.g: "PATH": "${workspaceFolder}/obj")
144
- custom_env [ var_name ] = custom_env [ var_name ] . replace ( / ( \$ \{ .* \} ) / , ( substring ) =>
145
- substituteVariables ( substring , false )
146
- ) ;
142
+ /**
143
+ * The User can specify `"VAR": null` in his settings, so we only
144
+ * apply substitution to non-null values.
145
+ */
146
+ if ( custom_env [ var_name ] != null ) {
147
+ // Substitute VS Code variable references that might be present
148
+ // in the JSON settings configuration (e.g: "PATH": "${workspaceFolder}/obj")
149
+ custom_env [ var_name ] =
150
+ custom_env [ var_name ] ?. replace ( / ( \$ \{ .* \} ) / , ( substring ) =>
151
+ substituteVariables ( substring , false )
152
+ ) ?? null ;
153
+ }
147
154
}
148
155
}
149
156
@@ -157,15 +164,30 @@ export function getEvaluatedTerminalEnv() {
157
164
* The targetEnv can be `process.env` to apply the changes to the environment of
158
165
* the running process.
159
166
*/
160
- export function setTerminalEnvironment ( targetEnv : NodeJS . ProcessEnv ) {
161
- // Retrieve the user's custom environment variables if specified in their
162
- // settings/workspace
163
- const custom_env = getEvaluatedTerminalEnv ( ) ;
167
+ export function setTerminalEnvironment (
168
+ targetEnv : NodeJS . ProcessEnv ,
169
+ custom_env ?: { [ name : string ] : string | null }
170
+ ) {
171
+ if ( custom_env == undefined ) {
172
+ // Retrieve the user's custom environment variables if specified in their
173
+ // settings/workspace
174
+ custom_env = getEvaluatedTerminalEnv ( ) ;
175
+ }
164
176
165
177
if ( custom_env ) {
166
178
for ( const var_name in custom_env ) {
167
- const var_value : string = custom_env [ var_name ] ;
168
- targetEnv [ var_name ] = var_value ;
179
+ const var_value = custom_env [ var_name ] ;
180
+ if ( var_value == null ) {
181
+ /**
182
+ * If the value is null, delete it from the target env if it
183
+ * exists.
184
+ */
185
+ if ( var_name in targetEnv ) {
186
+ delete targetEnv [ var_name ] ;
187
+ }
188
+ } else {
189
+ targetEnv [ var_name ] = var_value ;
190
+ }
169
191
}
170
192
}
171
193
}
0 commit comments