Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preset file expansion on open/save and expansion validation #3905

Merged
merged 66 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
e9badc3
initial testing
gcampbell-msft Jun 26, 2024
b58acef
test change
gcampbell-msft Jul 2, 2024
aa63da0
manually expand in expandConditionsForPresets and expandVendorForConf…
gcampbell-msft Jul 2, 2024
d3005aa
do light expansion, enough to correctly assess condition
gcampbell-msft Jul 2, 2024
177aaf6
revert unnecessary changes
gcampbell-msft Jul 2, 2024
203f1b1
remove more unused
gcampbell-msft Jul 2, 2024
c37e237
add back accidentally removed async/await, pending testing
gcampbell-msft Jul 2, 2024
e5ebb5c
not working, still broken, rethink refactor
gcampbell-msft Jul 2, 2024
9b7d442
change warning to an error in log
qarni Jul 3, 2024
47593d2
placeholder fix
qarni Jul 3, 2024
b1a52b8
update string
qarni Jul 3, 2024
916f450
Add boolean to control when we apply devenv.
gcampbell-msft Jul 9, 2024
b58429a
pushing state
gcampbell-msft Jul 9, 2024
dc1384b
Merge branch 'dev/qarni/circularEnvIssue' into dev/qarni/refactorExpa…
qarni Jul 9, 2024
7a37357
Merge branch 'dev/gcampbell/FixExpandingCondition' into dev/qarni/ref…
qarni Jul 9, 2024
c39819a
wip added reference obj
qarni Jul 10, 2024
1f47227
broken state, wip
qarni Jul 10, 2024
887f42d
works for one scenario
qarni Jul 10, 2024
9dcb089
invalidates userpresets if presets it inherits from is invalid
qarni Jul 10, 2024
cd217ca
should work for all situations with just configure presets work from …
qarni Jul 10, 2024
81cea37
clean up errorHandler var
qarni Jul 11, 2024
952c493
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 11, 2024
06dd70a
first pass add all presets
qarni Jul 12, 2024
a794b46
take out unneccessary expands
qarni Jul 12, 2024
4e0066d
unneccessary import
qarni Jul 12, 2024
1869fdc
broken, but prints to problems panel
qarni Jul 12, 2024
59c2bbc
remove unnecessary expanding of configure
qarni Jul 12, 2024
47f137b
wip error messages
qarni Jul 12, 2024
b1b226d
improve error printing
qarni Jul 15, 2024
947bb99
fix null type error
qarni Jul 15, 2024
e164506
rename
qarni Jul 15, 2024
b7ca0e0
Send the diagnostics to VS Code without overwriting the existing
qarni Jul 15, 2024
8bda6b6
cleanup, and avoid duplicate diagnostics
qarni Jul 15, 2024
7e64735
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 15, 2024
0eda305
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 16, 2024
7a59a34
changelog
qarni Jul 16, 2024
698489e
clear out errors when everything is fixed
qarni Jul 17, 2024
2361eb8
log level change to info
qarni Jul 17, 2024
952b676
we arent cacheing configurepresets right away, so we need to be able …
qarni Jul 17, 2024
1740230
add comment todo
qarni Jul 18, 2024
0063739
Merge branch 'main' into dev/qarni/refactorExpansion
gcampbell-msft Jul 18, 2024
78e7e75
cleanup
qarni Jul 18, 2024
f9156f1
add errorHandlerHelper func and fix bug with configPreset not found i…
qarni Jul 18, 2024
2b2967a
move expand vendor into expandConfigPreset and move comments
qarni Jul 19, 2024
d49c85b
name presetsFile to expandedPresetsFile
qarni Jul 19, 2024
fed1e93
pull out part of expansion into a different function
qarni Jul 19, 2024
3907c21
rename expanded presets file
qarni Jul 19, 2024
82d4ce7
when doing setConfigurePreset for select or for configure, use the or…
qarni Jul 19, 2024
0bb6b4b
cleanup
qarni Jul 19, 2024
0267f73
comments
qarni Jul 19, 2024
6e68d4c
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 19, 2024
facf115
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 22, 2024
f94526d
should fix test failures
qarni Jul 22, 2024
d5f79aa
nits and comments
qarni Jul 22, 2024
ba1d6b3
Merge branch 'dev/qarni/refactorExpansion' of https://github.com/micr…
qarni Jul 22, 2024
fc58451
tiny typo
qarni Jul 22, 2024
753943a
possible fix for include bug?
qarni Jul 23, 2024
3a935ab
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 23, 2024
9a6df6b
Merge branch 'main' into dev/qarni/refactorExpansion
qarni Jul 23, 2024
aa6762e
add tertiary preset cache
qarni Jul 23, 2024
7e6843c
Merge branch 'dev/qarni/refactorExpansion' of https://github.com/micr…
qarni Jul 23, 2024
9f798d4
rename presets caches
qarni Jul 23, 2024
ee6b0ea
expand conditions correctly
qarni Jul 23, 2024
0aee95e
update condition checking
qarni Jul 23, 2024
ae82a05
package presets with conditions that have cannot be evaluated
qarni Jul 24, 2024
2a53a6c
undo mistaken return change
qarni Jul 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Improvements:
- Add `Unspecified` option for selecting a kit variant to allow CMake itself to select the build type. [#3821](https://github.com/microsoft/vscode-cmake-tools/issues/3821)
- Skip loading variants when using CMakePresets. [#3300](https://github.com/microsoft/vscode-cmake-tools/issues/3300)
- Add setting to only show the cmake log on target failure. [#3785](https://github.com/microsoft/vscode-cmake-tools/pull/3785) [@stepeos](https://github.com/stepeos)
- Preset expansion occurs on `CMakePresets.json` or `CMakeUserPresets.json` save, and if there are no errors the expanded presets are cached. The VS Developer Environment will only be applied to a preset if it is selected. Expansion errors will show in the problems panel and preset files with errors will be invalid, and any presets they contain cannot be used. [#3905](https://github.com/microsoft/vscode-cmake-tools/pull/3905)

Bug Fixes:

Expand Down
13 changes: 13 additions & 0 deletions src/cmakeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,23 @@ export class CMakeProject {
return undefined;
}
log.debug(localize('resolving.config.preset', 'Resolving the selected configure preset'));

// We want to use the original unexpanded preset file to apply the dev env in expandConfigurePreset
// we have to first check if the preset is valid in expandedPresets since we won't be expanding the whole file here, only the path up for this preset
if (!preset.getPresetByName(preset.configurePresets(this.folderPath), configurePreset) && !preset.getPresetByName(preset.userConfigurePresets(this.folderPath), configurePreset)) {
return undefined;
}

// TODO: move applyDevEnv here to decouple from expandConfigurePreset

// modify the preset parent environment, in certain cases, to apply the Vs Dev Env on top of process.env.
const expandedConfigurePreset = await preset.expandConfigurePreset(this.folderPath,
configurePreset,
lightNormalizePath(this.folderPath || '.'),
this.sourceDir,
true,
true);

if (!expandedConfigurePreset) {
log.error(localize('failed.resolve.config.preset', 'Failed to resolve configure preset: {0}', configurePreset));
return undefined;
Expand Down Expand Up @@ -298,6 +310,7 @@ export class CMakeProject {

if (configurePreset) {
const expandedConfigurePreset: preset.ConfigurePreset | undefined = await this.expandConfigPresetbyName(configurePreset);

if (!expandedConfigurePreset) {
await this.resetPresets(drv);
return;
Expand Down
9 changes: 9 additions & 0 deletions src/diagnostics/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class LazyCollection implements vscode.Disposable {
class Collections {
private readonly _cmake = new LazyCollection('cmake-configure-diags');
private readonly _build = new LazyCollection('cmake-build-diags');
private readonly _presets = new LazyCollection('cmake-presets-diags');

/**
* The `DiagnosticCollection` for the CMake configure diagnostics.
Expand All @@ -53,9 +54,17 @@ class Collections {
return this._build.getOrCreate();
}

/**
* The `DiagnosticCollection` for presets diagnostics
*/
get presets(): vscode.DiagnosticCollection {
return this._presets.getOrCreate();
}

reset() {
this._cmake.dispose();
this._build.dispose();
this._presets.dispose();
}
}

Expand Down
34 changes: 26 additions & 8 deletions src/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,22 @@ export interface ExpansionOptions {
doNotSupportCommands?: boolean;
}

export interface ExpansionErrorHandler {
errorList: [string, string][];
tempErrorList: [string, string][];
}

/**
* Replace ${variable} references in the given string with their corresponding
* values.
* @param input The input string
* @param opts Options for the expansion process
* @returns A string with the variable references replaced
*/
export async function expandString<T>(input: string | T, opts: ExpansionOptions): Promise<string | T> {
export async function expandString<T>(input: string | T, opts: ExpansionOptions, errorHandler?: ExpansionErrorHandler): Promise<string | T> {
if (typeof input !== 'string') {
return input;
}

const inputString = input as string;
try {

Expand All @@ -122,22 +126,24 @@ export async function expandString<T>(input: string | T, opts: ExpansionOptions)
let i = 0;
do {
// TODO: consider a full circular reference check?
const expansion = await expandStringHelper(result, opts);
const expansion = await expandStringHelper(result, opts, errorHandler);
result = expansion.result;
didReplacement = expansion.didReplacement;
circularReference = expansion.circularReference;
i++;
} while (i < maxRecursion && opts.recursive && didReplacement && !circularReference);

if (circularReference) {
log.error(localize('circular.variable.reference', 'Circular variable reference found: {0}', circularReference));
log.error(localize('circular.variable.reference.full', 'Circular variable reference found: {0}', circularReference));
} else if (i === maxRecursion) {
log.error(localize('reached.max.recursion', 'Reached max string expansion recursion. Possible circular reference.'));
errorHandler?.tempErrorList.push([localize('max.recursion', 'Max string expansion recursion'), ""]);
}

return replaceAll(result, '${dollar}', '$');
} catch (e) {
log.warning(localize('exception.expanding.string', 'Exception while expanding string {0}: {1}', inputString, errorToString(e)));
log.warning(localize('exception.expanding.string.full', 'Exception while expanding string {0}: {1}', inputString, errorToString(e)));
errorHandler?.tempErrorList.push([localize('exception.expanding.string', 'Exception expanding string'), inputString]);
}

return input;
Expand All @@ -148,7 +154,7 @@ export async function expandString<T>(input: string | T, opts: ExpansionOptions)
// as few times as possible, expanding as needed (lazy)
const varValueRegexp = ".+?";

async function expandStringHelper(input: string, opts: ExpansionOptions) {
async function expandStringHelper(input: string, opts: ExpansionOptions, errorHandler?: ExpansionErrorHandler) {
const envPreNormalize = opts.envOverride ? opts.envOverride : process.env;
const env = EnvironmentUtils.create(envPreNormalize);
const replacements = opts.vars;
Expand All @@ -169,7 +175,8 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) {
// Replace dollar sign at the very end of the expanding process
const replacement = replacements[key];
if (!replacement) {
log.warning(localize('invalid.variable.reference', 'Invalid variable reference {0} in string: {1}', full, input));
log.warning(localize('invalid.variable.reference.full', 'Invalid variable reference {0} in string: {1}', full, input));
errorHandler?.tempErrorList.push([localize('invalid.variable.reference', 'Invalid variable reference'), input]);
} else {
subs.set(full, replacement);
}
Expand Down Expand Up @@ -206,6 +213,7 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) {
const varNameReplacement = mat2 ? mat2[1] : undefined;
if (varNameReplacement && varNameReplacement === varName) {
circularReference = `\"${varName}\" : \"${input}\"`;
errorHandler?.tempErrorList.push([localize('circular.variable.reference', 'Circular variable reference'), full]);
break;
}
subs.set(full, replacement);
Expand Down Expand Up @@ -254,7 +262,8 @@ async function expandStringHelper(input: string, opts: ExpansionOptions) {
const result = await vscode.commands.executeCommand(command, opts.vars.workspaceFolder);
subs.set(full, `${result}`);
} catch (e) {
log.warning(localize('exception.executing.command', 'Exception while executing command {0} for string: {1} {2}', command, input, errorToString(e)));
log.warning(localize('exception.executing.command.full', 'Exception while executing command {0} for string: {1} {2}', command, input, errorToString(e)));
errorHandler?.tempErrorList.push([localize('exception.executing.command', 'Exception executing command'), input]);
}
}

Expand Down Expand Up @@ -298,3 +307,12 @@ export function getParentEnvSubstitutions(input: string, subs: Map<string, strin

return subs;
}

export function errorHandlerHelper(presetName: string, errorHandler?: ExpansionErrorHandler) {
if (errorHandler) {
for (const error of errorHandler.tempErrorList || []) {
errorHandler.errorList.push([error[0], `'${error[1]}' in preset '${presetName}'`]);
}
errorHandler.tempErrorList = [];
}
}
Loading
Loading