src/vs/platform/terminal/common/environmentVariableCollection.ts

287 LOC · 272 covered · 15 uncovered · 61 ranges · 2294 concepts · 20 introducers · 1083 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

1 > /*--------------------------------------------------------------------------------------------- environmentVariableCollection.ts ×11
2 > * Copyright (c) Microsoft Corporation. All rights reserved.
3 > * Licensed under the MIT License. See License.txt in the project root for license information.
4 > *--------------------------------------------------------------------------------------------*/
5 >
6 > import { IProcessEnvironment, isWindows } from '../../../base/common/platform.js';
7 > import { EnvironmentVariableMutatorType, EnvironmentVariableScope, IEnvironmentVariableCollection, IExtensionOwnedEnvironmentDescriptionMutator, IExtensionOwnedEnvironmentVariableMutator, IMergedEnvironmentVariableCollection, IMergedEnvironmentVariableCollectionDiff } from './environmentVariable.js';
8 >
9 > type VariableResolver = (str: string) => Promise<string>;
10 >
11 > const mutatorTypeToLabelMap: Map<EnvironmentVariableMutatorType, string> = new Map([
12 > [EnvironmentVariableMutatorType.Append, 'APPEND'],
13 > [EnvironmentVariableMutatorType.Prepend, 'PREPEND'],
14 > [EnvironmentVariableMutatorType.Replace, 'REPLACE']
15 > ]);
16 > const PYTHON_ACTIVATION_VARS_PATTERN = /^VSCODE_PYTHON_(PWSH|ZSH|BASH|FISH)_ACTIVATE/;
17 > const PYTHON_ENV_EXTENSION_ID = 'ms-python.vscode-python-envs';
18 >
19 > export class MergedEnvironmentVariableCollection implements IMergedEnvironmentVariableCollection {
20 > private readonly map: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
21 > private readonly descriptionMap: Map<string, IExtensionOwnedEnvironmentDescriptionMutator[]> = new Map();
22 >
23 > constructor(
24 > readonly collections: ReadonlyMap<string, IEnvironmentVariableCollection>, environmentVariableCollection.ts ×9
25 > ) {
26 > collections.forEach((collection, extensionIdentifier) => {
27 > this.populateDescriptionMap(collection, extensionIdentifier);
28 > const it = collection.map.entries();
29 > let next = it.next();
30 > while (!next.done) {
31 > const mutator = next.value[1];
32 > const key = next.value[0];
33 >
34 > if (this.blockPythonActivationVar(key, extensionIdentifier)) {
35 next = it.next();
36 continue;
37 }
39 > let entry = this.map.get(key);
40 > if (!entry) {
41 > entry = [];
42 > this.map.set(key, entry);
43 > }
44 >
45 > // If the first item in the entry is replace ignore any other entries as they would
46 > // just get replaced by this one.
47 > if (entry.length > 0 && entry[0].type === EnvironmentVariableMutatorType.Replace) {
48 > next = it.next(); environmentVariableCollection.ts ×1
49 > continue;
50 > }
52 > const extensionMutator = {
53 > extensionIdentifier,
54 > value: mutator.value,
55 > type: mutator.type,
56 > scope: mutator.scope,
57 > variable: mutator.variable,
58 > options: mutator.options
59 > };
60 > if (!extensionMutator.scope) {
61 > delete extensionMutator.scope; // Convenient for tests
62 > }
63 > // Mutators get applied in the reverse order than they are created
64 > entry.unshift(extensionMutator);
65 >
66 > next = it.next();
67 > }
68 > });
69 > }
71 > async applyToProcessEnvironment(env: IProcessEnvironment, scope: EnvironmentVariableScope | undefined, variableResolver?: VariableResolver): Promise<void> {
72 > let lowerToActualVariableNames: { [lowerKey: string]: string | undefined } | undefined; environmentVariableCollection.ts ×6
73 > if (isWindows) {
74 lowerToActualVariableNames = {};
75 Object.keys(env).forEach(e => lowerToActualVariableNames![e.toLowerCase()] = e);
76 }
77 > for (const [variable, mutators] of this.getVariableMap(scope)) { environmentVariableCollection.ts ×6
78 > const actualVariable = isWindows ? lowerToActualVariableNames![variable.toLowerCase()] || variable : variable;
79 > for (const mutator of mutators) {
80 > const value = variableResolver ? await variableResolver(mutator.value) : mutator.value;
81 >
82 > if (this.blockPythonActivationVar(mutator.variable, mutator.extensionIdentifier)) {
83 continue;
84 }
86 > // Default: true
87 > if (mutator.options?.applyAtProcessCreation ?? true) {
88 > switch (mutator.type) {
89 > case EnvironmentVariableMutatorType.Append:
90 > env[actualVariable] = (env[actualVariable] || '') + value; environmentVariableCollection.ts ×1
91 > break;
92 > case EnvironmentVariableMutatorType.Prepend: environmentVariableCollection.ts ×6
93 > env[actualVariable] = value + (env[actualVariable] || '');
94 > break;
95 > case EnvironmentVariableMutatorType.Replace:
96 > env[actualVariable] = value; environmentVariableCollection.ts ×1
97 > break;
99 > }
100 > // Default: false
101 > if (mutator.options?.applyAtShellIntegration ?? false) {
102 const key = `VSCODE_ENV_${mutatorTypeToLabelMap.get(mutator.type)!}`;
103 env[key] = (env[key] ? env[key] + ':' : '') + variable + '=' + this._encodeColons(value);
104 }
106 > }
107 > }
109 > private _encodeColons(value: string): string {
110 return value.replaceAll(':', '\\x3a');
111 }
113 > private blockPythonActivationVar(variable: string, extensionIdentifier: string): boolean {
114 > // Only Python env extension can modify Python activate env var. environmentVariableCollection.ts ×9
115 > if (PYTHON_ACTIVATION_VARS_PATTERN.test(variable) && PYTHON_ENV_EXTENSION_ID !== extensionIdentifier) {
116 return true;
117 }
119 > }
121 > diff(other: IMergedEnvironmentVariableCollection, scope: EnvironmentVariableScope | undefined): IMergedEnvironmentVariableCollectionDiff | undefined {
122 > const added: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map(); environmentVariableCollection.ts ×7
123 > const changed: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
124 > const removed: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
125 >
126 > // Find added
127 > other.getVariableMap(scope).forEach((otherMutators, variable) => {
128 > const currentMutators = this.getVariableMap(scope).get(variable);
129 > const result = getMissingMutatorsFromArray(otherMutators, currentMutators);
130 > if (result) {
131 > added.set(variable, result); environmentVariableCollection.ts ×1
132 > }
134 >
135 > // Find removed
136 > this.getVariableMap(scope).forEach((currentMutators, variable) => {
137 > const otherMutators = other.getVariableMap(scope).get(variable); environmentVariableCollection.ts ×7
138 > const result = getMissingMutatorsFromArray(currentMutators, otherMutators);
139 > if (result) {
140 > removed.set(variable, result); environmentVariableCollection.ts ×2
141 > }
143 >
144 > // Find changed
145 > this.getVariableMap(scope).forEach((currentMutators, variable) => {
146 > const otherMutators = other.getVariableMap(scope).get(variable); environmentVariableCollection.ts ×7
147 > const result = getChangedMutatorsFromArray(currentMutators, otherMutators);
148 > if (result) {
149 > changed.set(variable, result); environmentVariableCollection.ts ×2
150 > }
152 >
153 > if (added.size === 0 && changed.size === 0 && removed.size === 0) {
154 > return undefined; environmentVariableCollection.ts ×1
155 > }
157 > return { added, changed, removed };
160 > getVariableMap(scope: EnvironmentVariableScope | undefined): Map<string, IExtensionOwnedEnvironmentVariableMutator[]> {
161 > const result = new Map<string, IExtensionOwnedEnvironmentVariableMutator[]>(); environmentVariableCollection.ts ×2
162 > for (const mutators of this.map.values()) {
163 > const filteredMutators = mutators.filter(m => filterScope(m, scope));
164 > if (filteredMutators.length > 0) {
165 > // All of these mutators are for the same variable because they are in the same scope, hence choose anyone to form a key.
166 > result.set(filteredMutators[0].variable, filteredMutators);
167 > }
168 > }
169 > return result;
170 > }
172 > getDescriptionMap(scope: EnvironmentVariableScope | undefined): Map<string, string | undefined> {
173 > const result = new Map<string, string | undefined>(); environmentVariableCollection.ts ×3
174 > for (const mutators of this.descriptionMap.values()) {
175 > const filteredMutators = mutators.filter(m => filterScope(m, scope, true));
176 > for (const mutator of filteredMutators) {
177 > result.set(mutator.extensionIdentifier, mutator.description);
178 > }
179 > }
180 > return result;
181 > }
183 > private populateDescriptionMap(collection: IEnvironmentVariableCollection, extensionIdentifier: string): void {
184 > if (!collection.descriptionMap) { environmentVariableCollection.ts ×9
185 > return;
186 > }
187 > const it = collection.descriptionMap.entries(); environmentVariableCollection.ts ×1
188 > let next = it.next();
189 > while (!next.done) {
190 > const mutator = next.value[1]; environmentVariableCollection.ts ×3
191 > const key = next.value[0];
192 > let entry = this.descriptionMap.get(key);
193 > if (!entry) {
194 > entry = [];
195 > this.descriptionMap.set(key, entry);
196 > }
197 > const extensionMutator = {
198 > extensionIdentifier,
199 > scope: mutator.scope,
200 > description: mutator.description
201 > };
202 > if (!extensionMutator.scope) {
203 > delete extensionMutator.scope; // Convenient for tests
204 > }
205 > entry.push(extensionMutator);
206 >
207 > next = it.next();
208 > }
210 > }
212 >
213 > /**
214 > * Returns whether a mutator matches with the scope provided.
215 > * @param mutator Mutator to filter
216 > * @param scope Scope to be used for querying
217 > * @param strictFilter If true, mutators with global scope is not returned when querying for workspace scope.
218 > * i.e whether mutator scope should always exactly match with query scope.
219 > */
220 > function filterScope( environmentVariableCollection.ts ×9
221 > mutator: IExtensionOwnedEnvironmentVariableMutator | IExtensionOwnedEnvironmentDescriptionMutator,
222 > scope: EnvironmentVariableScope | undefined,
223 > strictFilter = false
224 > ): boolean {
225 > if (!mutator.scope) {
226 > if (strictFilter) {
227 > return scope === mutator.scope; environmentVariableCollection.ts ×3
228 > }
230 > }
231 > // If a mutator is scoped to a workspace folder, only apply it if the workspace environmentVariableCollection.ts ×2
232 > // folder matches.
233 > if (mutator.scope.workspaceFolder && scope?.workspaceFolder && mutator.scope.workspaceFolder.index === scope.workspaceFolder.index) { environmentVariableCollection.ts ×9
235 > }
237 > }
239 > function getMissingMutatorsFromArray( environmentVariableCollection.ts ×7
240 > current: IExtensionOwnedEnvironmentVariableMutator[],
241 > other: IExtensionOwnedEnvironmentVariableMutator[] | undefined
242 > ): IExtensionOwnedEnvironmentVariableMutator[] | undefined {
243 > // If it doesn't exist, all are removed
244 > if (!other) {
245 > return current; environmentVariableCollection.ts ×1
246 > }
248 > // Create a map to help
249 > const otherMutatorExtensions = new Set<string>();
250 > other.forEach(m => otherMutatorExtensions.add(m.extensionIdentifier));
251 >
252 > // Find entries removed from other
253 > const result: IExtensionOwnedEnvironmentVariableMutator[] = [];
254 > current.forEach(mutator => {
255 > if (!otherMutatorExtensions.has(mutator.extensionIdentifier)) {
256 > result.push(mutator); environmentVariableCollection.ts ×1
257 > }
259 >
260 > return result.length === 0 ? undefined : result; environmentVariableCollection.ts ×7
261 > }
263 > function getChangedMutatorsFromArray( environmentVariableCollection.ts ×7
264 > current: IExtensionOwnedEnvironmentVariableMutator[],
265 > other: IExtensionOwnedEnvironmentVariableMutator[] | undefined
266 > ): IExtensionOwnedEnvironmentVariableMutator[] | undefined {
267 > // If it doesn't exist, none are changed (they are removed)
268 > if (!other) {
269 > return undefined; environmentVariableCollection.ts ×2
270 > }
272 > // Create a map to help
273 > const otherMutatorExtensions = new Map<string, IExtensionOwnedEnvironmentVariableMutator>();
274 > other.forEach(m => otherMutatorExtensions.set(m.extensionIdentifier, m));
275 >
276 > // Find entries that exist in both but are not equal
277 > const result: IExtensionOwnedEnvironmentVariableMutator[] = [];
278 > current.forEach(mutator => {
279 > const otherMutator = otherMutatorExtensions.get(mutator.extensionIdentifier);
280 > if (otherMutator && (mutator.type !== otherMutator.type || mutator.value !== otherMutator.value || mutator.scope?.workspaceFolder?.index !== otherMutator.scope?.workspaceFolder?.index)) {
281 > // Return the new result, not the old one environmentVariableCollection.ts ×2
282 > result.push(otherMutator);
283 > }
285 >
286 > return result.length === 0 ? undefined : result;
287 > }