Skip to content

Commit 62c8173

Browse files
committed
Merge branch 'topic/vscode-env-init' into 'master'
Handle null environment variable in vscode settings Closes #1283 See merge request eng/ide/ada_language_server!1486
2 parents a2cceff + 6eaa5d4 commit 62c8173

File tree

4 files changed

+82
-17
lines changed

4 files changed

+82
-17
lines changed

integration/vscode/ada/src/debugConfigProvider.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ function getOrFindGdb(): string | undefined {
100100
const env = getEvaluatedTerminalEnv();
101101
let pathVal: string;
102102
if (env && 'PATH' in env) {
103-
pathVal = env.PATH;
103+
pathVal = env.PATH ?? '';
104104
} else if ('PATH' in process.env) {
105105
pathVal = process.env.PATH ?? '';
106106
} else {

integration/vscode/ada/src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ async function activateExtension(context: vscode.ExtensionContext) {
131131
if (customEnv && Object.keys(customEnv).length > 0) {
132132
logger.info(`Custom environment variables from ${TERMINAL_ENV_SETTING_NAME}`);
133133
for (const varName in customEnv) {
134-
const varValue: string = customEnv[varName];
135-
logger.info(`${varName}=${varValue}`);
134+
const varValue = customEnv[varName];
135+
logger.info(`${varName}=${varValue ?? '<null>'}`);
136136
}
137137
} else {
138138
logger.debug('No custom environment variables set in %s', TERMINAL_ENV_SETTING_NAME);

integration/vscode/ada/src/helpers.ts

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import assert from 'assert';
2+
import { suite, test } from 'mocha';
3+
import { setTerminalEnvironment } from '../../../src/helpers';
4+
5+
suite('Environment init', () => {
6+
test('Env init', () => {
7+
/**
8+
* We want to test the cases [existing variable, non-existing variable]
9+
* x [null value, empty string, non-empty string] exhaustively. So we
10+
* should end up with 6 test cases.
11+
*/
12+
13+
const targetEnv = {
14+
VAR1: '',
15+
VAR2: '',
16+
VAR3: '',
17+
VAR7: 'Should be preserved',
18+
};
19+
20+
const userEnv: { [name: string]: string | null } = {
21+
// Existing variables
22+
VAR1: null,
23+
VAR2: '',
24+
VAR3: 'Some new value',
25+
// Non-existing variables
26+
VAR4: null,
27+
VAR5: '',
28+
VAR6: 'Some other value',
29+
};
30+
31+
setTerminalEnvironment(targetEnv, userEnv);
32+
33+
const expected = {
34+
VAR2: '',
35+
VAR3: 'Some new value',
36+
VAR5: '',
37+
VAR6: 'Some other value',
38+
VAR7: 'Should be preserved',
39+
};
40+
41+
assert.deepEqual(targetEnv, expected);
42+
});
43+
});

0 commit comments

Comments
 (0)