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.
/*---------------------------------------------------------------------------------------------
environmentVariableCollection.ts ×11
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { IProcessEnvironment, isWindows } from '../../../base/common/platform.js';
import { EnvironmentVariableMutatorType, EnvironmentVariableScope, IEnvironmentVariableCollection, IExtensionOwnedEnvironmentDescriptionMutator, IExtensionOwnedEnvironmentVariableMutator, IMergedEnvironmentVariableCollection, IMergedEnvironmentVariableCollectionDiff } from './environmentVariable.js';
type VariableResolver = (str: string) => Promise<string>;
const mutatorTypeToLabelMap: Map<EnvironmentVariableMutatorType, string> = new Map([
[EnvironmentVariableMutatorType.Append, 'APPEND'],
[EnvironmentVariableMutatorType.Prepend, 'PREPEND'],
[EnvironmentVariableMutatorType.Replace, 'REPLACE']
]);
const PYTHON_ACTIVATION_VARS_PATTERN = /^VSCODE_PYTHON_(PWSH|ZSH|BASH|FISH)_ACTIVATE/;
const PYTHON_ENV_EXTENSION_ID = 'ms-python.vscode-python-envs';
export class MergedEnvironmentVariableCollection implements IMergedEnvironmentVariableCollection {
private readonly map: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
private readonly descriptionMap: Map<string, IExtensionOwnedEnvironmentDescriptionMutator[]> = new Map();
constructor(
readonly collections: ReadonlyMap<string, IEnvironmentVariableCollection>,
environmentVariableCollection.ts ×9
) {
collections.forEach((collection, extensionIdentifier) => {
this.populateDescriptionMap(collection, extensionIdentifier);
const it = collection.map.entries();
let next = it.next();
while (!next.done) {
const mutator = next.value[1];
const key = next.value[0];
if (this.blockPythonActivationVar(key, extensionIdentifier)) {
next = it.next();
continue;
}
let entry = this.map.get(key);
if (!entry) {
entry = [];
this.map.set(key, entry);
}
// If the first item in the entry is replace ignore any other entries as they would
// just get replaced by this one.
if (entry.length > 0 && entry[0].type === EnvironmentVariableMutatorType.Replace) {
continue;
}
const extensionMutator = {
extensionIdentifier,
value: mutator.value,
type: mutator.type,
scope: mutator.scope,
variable: mutator.variable,
options: mutator.options
};
if (!extensionMutator.scope) {
delete extensionMutator.scope; // Convenient for tests
}
// Mutators get applied in the reverse order than they are created
entry.unshift(extensionMutator);
next = it.next();
}
});
}
async applyToProcessEnvironment(env: IProcessEnvironment, scope: EnvironmentVariableScope | undefined, variableResolver?: VariableResolver): Promise<void> {
let lowerToActualVariableNames: { [lowerKey: string]: string | undefined } | undefined;
environmentVariableCollection.ts ×6
if (isWindows) {
lowerToActualVariableNames = {};
Object.keys(env).forEach(e => lowerToActualVariableNames![e.toLowerCase()] = e);
}
for (const [variable, mutators] of this.getVariableMap(scope)) {
environmentVariableCollection.ts ×6
const actualVariable = isWindows ? lowerToActualVariableNames![variable.toLowerCase()] || variable : variable;
for (const mutator of mutators) {
const value = variableResolver ? await variableResolver(mutator.value) : mutator.value;
if (this.blockPythonActivationVar(mutator.variable, mutator.extensionIdentifier)) {
continue;
}
// Default: true
if (mutator.options?.applyAtProcessCreation ?? true) {
switch (mutator.type) {
case EnvironmentVariableMutatorType.Append:
break;
env[actualVariable] = value + (env[actualVariable] || '');
break;
case EnvironmentVariableMutatorType.Replace:
break;
}
// Default: false
if (mutator.options?.applyAtShellIntegration ?? false) {
const key = `VSCODE_ENV_${mutatorTypeToLabelMap.get(mutator.type)!}`;
env[key] = (env[key] ? env[key] + ':' : '') + variable + '=' + this._encodeColons(value);
}
}
}
private _encodeColons(value: string): string {
return value.replaceAll(':', '\\x3a');
}
private blockPythonActivationVar(variable: string, extensionIdentifier: string): boolean {
// Only Python env extension can modify Python activate env var.
environmentVariableCollection.ts ×9
if (PYTHON_ACTIVATION_VARS_PATTERN.test(variable) && PYTHON_ENV_EXTENSION_ID !== extensionIdentifier) {
return true;
}
}
diff(other: IMergedEnvironmentVariableCollection, scope: EnvironmentVariableScope | undefined): IMergedEnvironmentVariableCollectionDiff | undefined {
const added: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
environmentVariableCollection.ts ×7
const changed: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
const removed: Map<string, IExtensionOwnedEnvironmentVariableMutator[]> = new Map();
// Find added
other.getVariableMap(scope).forEach((otherMutators, variable) => {
const currentMutators = this.getVariableMap(scope).get(variable);
const result = getMissingMutatorsFromArray(otherMutators, currentMutators);
if (result) {
}
// Find removed
this.getVariableMap(scope).forEach((currentMutators, variable) => {
const otherMutators = other.getVariableMap(scope).get(variable);
environmentVariableCollection.ts ×7
const result = getMissingMutatorsFromArray(currentMutators, otherMutators);
if (result) {
}
// Find changed
this.getVariableMap(scope).forEach((currentMutators, variable) => {
const otherMutators = other.getVariableMap(scope).get(variable);
environmentVariableCollection.ts ×7
const result = getChangedMutatorsFromArray(currentMutators, otherMutators);
if (result) {
}
if (added.size === 0 && changed.size === 0 && removed.size === 0) {
}
return { added, changed, removed };
getVariableMap(scope: EnvironmentVariableScope | undefined): Map<string, IExtensionOwnedEnvironmentVariableMutator[]> {
const result = new Map<string, IExtensionOwnedEnvironmentVariableMutator[]>();
environmentVariableCollection.ts ×2
for (const mutators of this.map.values()) {
const filteredMutators = mutators.filter(m => filterScope(m, scope));
if (filteredMutators.length > 0) {
// All of these mutators are for the same variable because they are in the same scope, hence choose anyone to form a key.
result.set(filteredMutators[0].variable, filteredMutators);
}
}
return result;
}
getDescriptionMap(scope: EnvironmentVariableScope | undefined): Map<string, string | undefined> {
for (const mutators of this.descriptionMap.values()) {
const filteredMutators = mutators.filter(m => filterScope(m, scope, true));
for (const mutator of filteredMutators) {
result.set(mutator.extensionIdentifier, mutator.description);
}
}
return result;
}
private populateDescriptionMap(collection: IEnvironmentVariableCollection, extensionIdentifier: string): void {
return;
}
let next = it.next();
while (!next.done) {
const key = next.value[0];
let entry = this.descriptionMap.get(key);
if (!entry) {
entry = [];
this.descriptionMap.set(key, entry);
}
const extensionMutator = {
extensionIdentifier,
scope: mutator.scope,
description: mutator.description
};
if (!extensionMutator.scope) {
delete extensionMutator.scope; // Convenient for tests
}
entry.push(extensionMutator);
next = it.next();
}
}
/**
* Returns whether a mutator matches with the scope provided.
* @param mutator Mutator to filter
* @param scope Scope to be used for querying
* @param strictFilter If true, mutators with global scope is not returned when querying for workspace scope.
* i.e whether mutator scope should always exactly match with query scope.
*/
mutator: IExtensionOwnedEnvironmentVariableMutator | IExtensionOwnedEnvironmentDescriptionMutator,
scope: EnvironmentVariableScope | undefined,
strictFilter = false
): boolean {
if (!mutator.scope) {
if (strictFilter) {
}
}
// If a mutator is scoped to a workspace folder, only apply it if the workspace
environmentVariableCollection.ts ×2
// folder matches.
if (mutator.scope.workspaceFolder && scope?.workspaceFolder && mutator.scope.workspaceFolder.index === scope.workspaceFolder.index) {
environmentVariableCollection.ts ×9
}
}
current: IExtensionOwnedEnvironmentVariableMutator[],
other: IExtensionOwnedEnvironmentVariableMutator[] | undefined
): IExtensionOwnedEnvironmentVariableMutator[] | undefined {
// If it doesn't exist, all are removed
if (!other) {
}
// Create a map to help
const otherMutatorExtensions = new Set<string>();
other.forEach(m => otherMutatorExtensions.add(m.extensionIdentifier));
// Find entries removed from other
const result: IExtensionOwnedEnvironmentVariableMutator[] = [];
current.forEach(mutator => {
if (!otherMutatorExtensions.has(mutator.extensionIdentifier)) {
}
}
current: IExtensionOwnedEnvironmentVariableMutator[],
other: IExtensionOwnedEnvironmentVariableMutator[] | undefined
): IExtensionOwnedEnvironmentVariableMutator[] | undefined {
// If it doesn't exist, none are changed (they are removed)
if (!other) {
}
// Create a map to help
const otherMutatorExtensions = new Map<string, IExtensionOwnedEnvironmentVariableMutator>();
other.forEach(m => otherMutatorExtensions.set(m.extensionIdentifier, m));
// Find entries that exist in both but are not equal
const result: IExtensionOwnedEnvironmentVariableMutator[] = [];
current.forEach(mutator => {
const otherMutator = otherMutatorExtensions.get(mutator.extensionIdentifier);
if (otherMutator && (mutator.type !== otherMutator.type || mutator.value !== otherMutator.value || mutator.scope?.workspaceFolder?.index !== otherMutator.scope?.workspaceFolder?.index)) {
result.push(otherMutator);
}
return result.length === 0 ? undefined : result;
}