Skip to content

Commit

Permalink
move user env expansion forkits to after vsenv merged in (#3887)
Browse files Browse the repository at this point in the history
* move user env expansion forkits to after vsenv merged in

* changelog

* fix for penv and env differentiation
  • Loading branch information
moyo1997 authored Jul 10, 2024
1 parent 573f7cc commit 418a4c9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Bug Fixes:
- Fix issue where `cmakeUserPresets.json` not showing up in project outline. [#3832](https://github.com/microsoft/vscode-cmake-tools/issues/3832)
- Fix edge case where parsing tests fails when additional output is printed before tests json. [#3750](https://github.com/microsoft/vscode-cmake-tools/issues/3750)
- Fix issue where `Configure with CMake Debugger` fails on restart because the previously used pipe to CMake Debugger is no longer available. [#3582](https://github.com/microsoft/vscode-cmake-tools/issues/3582)
- Fix custom kit PATH being overriden [#3849](https://github.com/microsoft/vscode-cmake-tools/issues/3849)

## 1.18.43

Expand Down
15 changes: 12 additions & 3 deletions src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ export interface ExpansionOptions {
* will be expanded.
*/
envOverride?: EnvironmentWithNull;
/**
* Override the values used in `${penv:var}`-style and `${penv.var}`-style expansions.
*
* Note that setting this property will disable expansion of environment
* variables for the running process. Only environment variables in this key
* will be expanded.
*/
penvOverride?: EnvironmentWithNull;
/**
* Variables for `${variant:var}`-style expansions.
*/
Expand Down Expand Up @@ -203,7 +211,7 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) {
subs.set(full, replacement);
}

getParentEnvSubstitutions(input, subs);
getParentEnvSubstitutions(input, subs, opts.penvOverride);

if (vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0) {
const folderRegex = RegExp(`\\$\\{workspaceFolder:(${varValueRegexp})\\}`, "g");
Expand Down Expand Up @@ -278,12 +286,13 @@ export function substituteAll(input: string, subs: Map<string, string>) {
return { result: finalString, didReplacement };
}

export function getParentEnvSubstitutions(input: string, subs: Map<string, string>): Map<string, string> {
export function getParentEnvSubstitutions(input: string, subs: Map<string, string>, penvOverride?: EnvironmentWithNull): Map<string, string> {
const parentEnvRegex = RegExp(`\\$penv\\{(${varValueRegexp})\\}`, "g");
for (const mat of matchAll(input, parentEnvRegex)) {
const full = mat[0];
const varName = mat[1];
const replacement = fixPaths(process.env[varName]) || '';
const replacementValue = penvOverride ? penvOverride[varName] : process.env[varName];
const replacement = fixPaths(replacementValue === null ? undefined : replacementValue) || '';
subs.set(full, replacement);
}

Expand Down
35 changes: 23 additions & 12 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1000,18 +1000,17 @@ export async function effectiveKitEnvironment(kit: Kit, opts?: expand.ExpansionO
}
let env = EnvironmentUtils.create(host_env);
const kit_env = EnvironmentUtils.create(kit.environmentVariables);
const expandOptions: expand.ExpansionOptions = {
vars: {} as expand.KitContextVars,
envOverride: host_env
};
for (const env_var of Object.keys(kit_env)) {
env[env_var] = await expand.expandString(kit_env[env_var], opts ?? expandOptions);
}
if (process.platform === 'win32') {
if (kit.visualStudio && kit.visualStudioArchitecture) {
const vs_vars = await getVSKitEnvironment(kit);
env = EnvironmentUtils.merge([env, vs_vars]);
} else {
const getVSKitEnv = process.platform === 'win32' && kit.visualStudio && kit.visualStudioArchitecture;
if (!getVSKitEnv) {
const expandOptions: expand.ExpansionOptions = {
vars: {} as expand.KitContextVars,
envOverride: host_env
};
for (const env_var of Object.keys(kit_env)) {
env[env_var] = await expand.expandString(kit_env[env_var], opts ?? expandOptions);
}

if (process.platform === 'win32') {
const path_list: string[] = [];
const cCompiler = kit.compilers?.C;
/* Force add the compiler executable dir to the PATH env */
Expand All @@ -1028,6 +1027,18 @@ export async function effectiveKitEnvironment(kit: Kit, opts?: expand.ExpansionO
env['PATH'] = path_list.join(';');
}
}
} else {
const vs_vars = await getVSKitEnvironment(kit);
const penv = EnvironmentUtils.merge([env, vs_vars]);
env = EnvironmentUtils.merge([penv, kit_env]);
const expandOptions: expand.ExpansionOptions = opts ? {...opts, envOverride: env, penvOverride: penv } : {
vars: {} as expand.KitContextVars,
envOverride: env,
penvOverride: penv
};
for (const env_var of Object.keys(kit_env)) {
env[env_var] = await expand.expandString(kit_env[env_var], expandOptions);
}
}
log.debug(localize('kit.env', 'The environment for kit {0}: {1}', `'${kit.name}'`, JSON.stringify(env, null, 2)));
return env;
Expand Down

0 comments on commit 418a4c9

Please sign in to comment.