Skip to content

Consider adding EnvironmentVariableMutatorOptions when using EnvironmentVariableCollection API #631

@anthonykim1

Description

@anthonykim1

Related: #624

I see when collection api is being used, its being called without MutatorOption such as in:

    // Add env var for PYDEVD_DISABLE_FILE_VALIDATION to disable extra output in terminal when starting the debug session.
    collection.replace('PYDEVD_DISABLE_FILE_VALIDATION', '1');

    // Add env vars for VSCODE_DEBUGPY_ADAPTER_ENDPOINTS, BUNDLED_DEBUGPY_PATH, and PATH
    collection.replace('VSCODE_DEBUGPY_ADAPTER_ENDPOINTS', tempFilePath);

Reading the doc:

		/**
		 * Replace an environment variable with a value.
		 *
		 * Note that an extension can only make a single change to any one variable, so this will
		 * overwrite any previous calls to replace, append or prepend.
		 *
		 * @param variable The variable to replace.
		 * @param value The value to replace the variable with.
		 * @param options Options applied to the mutator, when no options are provided this will
		 * default to `{ applyAtProcessCreation: true }`.
		 */
		replace(variable: string, value: string, options?: EnvironmentVariableMutatorOptions): void;

When no options are provided, we only get { applyAtProcessCreation: true }.
Note that applyAtShellIntegration defaults to false as referred in:

	/**
	 * Options applied to the mutator.
	 */
	export interface EnvironmentVariableMutatorOptions {
		/**
		 * Apply to the environment just before the process is created. Defaults to false.
		 */
		applyAtProcessCreation?: boolean;

		/**
		 * Apply to the environment in the shell integration script. Note that this _will not_ apply
		 * the mutator if shell integration is disabled or not working for some reason. Defaults to
		 * false.
		 */
		applyAtShellIntegration?: boolean;
	}

and see the source code here: https://github.com/microsoft/vscode/blob/59990f4108d681df9c0673e8254593697cd5141d/src/vs/platform/terminal/common/environmentVariableCollection.ts#L88-L91

				// 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);
				}

Since applyAtShellIntegration happens after applyAtProcessCreation, it may be safer to set environment variable at applyAtShellIntegration if shell integration is active.

Metadata

Metadata

Assignees

No one assigned

    Labels

    triage-neededNeeds assignment to the proper sub-team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions