@@ -121,12 +121,12 @@ export const TERMINAL_ENV_SETTING_NAME =
121121 * @returns the value of the applicable `terminal.integrated.env.*` setting,
122122 * without evaluation of macros such as `${env:...}`.
123123 */
124- export function getTerminalEnv ( ) {
124+ export function getTerminalEnv ( ) : { [ name : string ] : string | null } {
125125 const custom_env = vscode . workspace
126126 . getConfiguration ( )
127- . get < { [ name : string ] : string } > ( TERMINAL_ENV_SETTING_NAME ) ;
127+ . get < { [ name : string ] : string | null } > ( TERMINAL_ENV_SETTING_NAME ) ;
128128
129- return custom_env ;
129+ return custom_env ?? { } ;
130130}
131131
132132/**
@@ -139,11 +139,18 @@ export function getEvaluatedTerminalEnv() {
139139
140140 if ( custom_env ) {
141141 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+ }
147154 }
148155 }
149156
@@ -157,15 +164,30 @@ export function getEvaluatedTerminalEnv() {
157164 * The targetEnv can be `process.env` to apply the changes to the environment of
158165 * the running process.
159166 */
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+ }
164176
165177 if ( custom_env ) {
166178 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+ }
169191 }
170192 }
171193}
0 commit comments