Skip to content

Commit

Permalink
Validate config preset in initialization (#2978)
Browse files Browse the repository at this point in the history
  • Loading branch information
elahehrashedi authored Jan 30, 2023
1 parent 2f8f1fc commit 2a297fd
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Improvements:
Bug Fixes:
- Compatibility between test and build presets was not enforced. [#2904](https://github.com/microsoft/vscode-cmake-tools/issues/2904)

## 1.13.44
Bug Fixes:
- Validate presets in initialization. [#2976](https://github.com/microsoft/vscode-cmake-tools/issues/2976)

## 1.13.43
Bug Fixes:
- Fix an issue causing the Add Presets commands not to appear. [PR #2977](https://github.com/microsoft/vscode-cmake-tools/pull/2977)
Expand Down
11 changes: 8 additions & 3 deletions src/cmakeProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,9 +987,14 @@ export class CMakeProject {

async initializeKitOrPresets() {
if (this.useCMakePresets) {
const configurePreset = this.workspaceContext.state.configurePresetName;
if (configurePreset) {
await this.presetsController.setConfigurePreset(configurePreset);
const latestConfigPresetName = this.workspaceContext.state.configurePresetName;
if (latestConfigPresetName) {
// Check if the latest configurePresetName from the previous session is still valid.
const presets = await this.presetsController.getAllConfigurePresets();
const latestConfigPreset: preset.ConfigurePreset | undefined = presets.find(preset => preset.name === latestConfigPresetName);
if (latestConfigPreset && !latestConfigPreset.hidden) {
await this.presetsController.setConfigurePreset(latestConfigPresetName);
}
}
} else {
// Check if the CMakeProject remembers what kit it was last using in this dir:
Expand Down
36 changes: 22 additions & 14 deletions src/presetsController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,11 +607,14 @@ export class PresetsController {
return chosenPresets?.preset;
}

async selectConfigurePreset(): Promise<boolean> {
async getAllConfigurePresets(): Promise<preset.ConfigurePreset[]> {
preset.expandVendorForConfigurePresets(this.folderPath);
await preset.expandConditionsForPresets(this.folderPath, this._sourceDir);
return preset.configurePresets(this.folderPath).concat(preset.userConfigurePresets(this.folderPath));
}

const allPresets = preset.configurePresets(this.folderPath).concat(preset.userConfigurePresets(this.folderPath));
async selectConfigurePreset(): Promise<boolean> {
const allPresets: preset.ConfigurePreset[] = await this.getAllConfigurePresets();
const presets = allPresets.filter(
_preset => {
const supportedHost = (_preset.vendor as preset.VendorVsSettings)?.['microsoft.com/VisualStudioSettings/CMake/1.0']?.hostOS;
Expand Down Expand Up @@ -700,7 +703,7 @@ export class PresetsController {
const buildPresets = preset.allBuildPresets(this.folderPath);
for (const buildPreset of buildPresets) {
// Set active build preset as the first valid build preset matches the selected configure preset
if (buildPreset.configurePreset === selectedConfigurePreset) {
if (buildPreset.configurePreset === selectedConfigurePreset && !buildPreset.hidden) {
await this.setBuildPreset(buildPreset.name, false/*needToCheckConfigurePreset*/, false/*checkChangingPreset*/);
currentBuildPreset = this.project.buildPreset?.name;
}
Expand Down Expand Up @@ -833,22 +836,27 @@ export class PresetsController {
private checkCompatibility(configurePreset: preset.ConfigurePreset | null, buildPreset?: preset.BuildPreset | null, testPreset?: preset.TestPreset | null): {buildPresetCompatible: boolean; testPresetCompatible: boolean} {
let testPresetCompatible = true;
let buildPresetCompatible = true;
// We only check compatibility when we are setting the build or test preset. So we need to exclude the hidden presets.
if (testPreset) {
const configMatches = testPreset.configurePreset === configurePreset?.name;
let buildTypeMatches = buildPreset?.configuration === testPreset.configuration;
if (!buildTypeMatches) {
if (util.isMultiConfGeneratorFast(configurePreset?.generator)) {
const buildType = buildPreset?.configuration || configurePreset?.cacheVariables?.['CMAKE_CONFIGURATION_TYPES']?.toString().split(';')?.[0] || 'Debug';
buildTypeMatches = testPreset.configuration === buildType || testPreset.configuration === undefined;
} else {
const buildType = configurePreset?.cacheVariables?.['CMAKE_BUILD_TYPE'] || 'Debug';
buildTypeMatches = buildPreset === undefined || testPreset.configuration === buildType || testPreset.configuration === undefined;
if (testPreset.hidden) {
testPresetCompatible = false;
} else {
const configMatches = testPreset.configurePreset === configurePreset?.name;
let buildTypeMatches = buildPreset?.configuration === testPreset.configuration;
if (!buildTypeMatches) {
if (util.isMultiConfGeneratorFast(configurePreset?.generator)) {
const buildType = buildPreset?.configuration || configurePreset?.cacheVariables?.['CMAKE_CONFIGURATION_TYPES']?.toString().split(';')?.[0] || 'Debug';
buildTypeMatches = testPreset.configuration === buildType || testPreset.configuration === undefined;
} else {
const buildType = configurePreset?.cacheVariables?.['CMAKE_BUILD_TYPE'] || 'Debug';
buildTypeMatches = buildPreset === undefined || testPreset.configuration === buildType || testPreset.configuration === undefined;
}
}
testPresetCompatible = configMatches && buildTypeMatches;
}
testPresetCompatible = configMatches && buildTypeMatches;
}
if (buildPreset) {
buildPresetCompatible = configurePreset?.name === buildPreset.configurePreset;
buildPresetCompatible = (configurePreset?.name === buildPreset.configurePreset) && !buildPreset.hidden;
}
return {buildPresetCompatible, testPresetCompatible};
}
Expand Down

0 comments on commit 2a297fd

Please sign in to comment.