Skip to content

Stop UTILITY targets from breaking IntelliSense (#4404) #4405

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
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
@@ -30,6 +30,7 @@ Bug Fixes:
- Fix ENOENT error at vscode startup on some circumstances [#2855](https://github.com/microsoft/vscode-cmake-tools/issues/2855) Contributed by STMicroelectronics
- Fix repeat execution option in test presets [#4443](https://github.com/microsoft/vscode-cmake-tools/issues/4443)
- Fix bug that makes some build hang [#4424](https://github.com/microsoft/vscode-cmake-tools/issues/4424) and [#4465](https://github.com/microsoft/vscode-cmake-tools/issues/4465)
- Fix incorrect IntelliSense configuration when a `UTILITY` has source files. [#4404](https://github.com/microsoft/vscode-cmake-tools/issues/4404)

## 1.20.53

23 changes: 22 additions & 1 deletion src/cpptools.ts
Original file line number Diff line number Diff line change
@@ -315,6 +315,27 @@ export function getIntelliSenseMode(cptVersion: cpptools.Version, compilerPath:
}
}

/**
* Try to find a target configuration with some populated properties.
*
* All targets get defaults for `compilerPath`, `compilerArgs`, and
* `compilerFragments`, even `UTILITY` targets defined with
* `add_custom_command()` that provide no other useful configuration, so if
* possible, return one with more than just those populated.
*/
function fallbackConfiguration(configurations: Map<string, cpptools.SourceFileConfigurationItem> | undefined) {
if (!configurations) {
return undefined;
}
for (const item of configurations.values()) {
const { configuration: { includePath, defines, intelliSenseMode, standard} } = item;
if (includePath.length || defines.length || intelliSenseMode || standard) {
return item;
}
}
return configurations.values().next().value;
}

/**
* The actual class that provides information to the cpptools extension. See
* the `CustomConfigurationProvider` interface for information on how this class
@@ -350,7 +371,7 @@ export class CppConfigurationProvider implements cpptools.CustomConfigurationPro
if (this.activeTarget && configurations?.has(this.activeTarget)) {
return configurations!.get(this.activeTarget);
} else {
return configurations?.values().next().value; // Any value is fine if the target doesn't match
return fallbackConfiguration(configurations);
}
}

12 changes: 11 additions & 1 deletion test/unit-tests/cpptools.test.ts
Original file line number Diff line number Diff line change
@@ -319,12 +319,21 @@ suite('CppTools tests', () => {
name: 'cpptools-test2',
sourceDirectory: smokeFolder,
targets: [
{
name: 'utilityTarget',
type: 'UTILITY',
fileGroups: [{
sources: [sourceFile3],
isGenerated: false
}]
},
{
name: 'target3',
type: 'EXECUTABLE',
fileGroups: [{
sources: [sourceFile3],
isGenerated: false,
defines: ['DEFINE3'], // make this a more attractive fallback than utilityTarget
compileCommandFragments: ['-DFRAGMENT3'],
language: 'CXX'
}]
@@ -368,7 +377,8 @@ suite('CppTools tests', () => {
// Verify the browsePath with a different folder.
const configurations2 = await provider.provideConfigurations([uri3]);
expect(configurations2.length).to.eq(1);
expect(configurations2[0].configuration.defines).to.be.empty;
expect(configurations2[0].configuration.defines.length).to.eq(1);
expect(configurations2[0].configuration.defines).to.contain('DEFINE3');
expect(configurations2[0].configuration.compilerFragments).to.contain('-DFRAGMENT3');
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
expect(browseConfig2?.browsePath.length).to.eq(1);