Skip to content

Commit 5098463

Browse files
committed
Stop UTILITY targets from affecting IntelliSense
Some `UTILITY` targets, such as the ones created by `Doxygen.cmake`, have a list of associated sources but don't have any useful `includePath`, `defines`, etc. If the selected Build Target is `all` or some other non-artifact target, these targets could be chosen as a fallback source of configuration instead of a more complete one. Filter out targets with empty configurations before selecting a fallback, so that doesn't happen.
1 parent 015abf5 commit 5098463

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ Bug Fixes:
3232
- 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)
3333
- Fix issue with switching to presets during Quick Start. [#4409](https://github.com/microsoft/vscode-cmake-tools/issues/4409)
3434
- Fix bug that shows empty lines in Pinned Commands view. [#4406](https://github.com/microsoft/vscode-cmake-tools/issues/4406)
35+
- Fix incorrect IntelliSense configuration when a `UTILITY` has source files. [#4404](https://github.com/microsoft/vscode-cmake-tools/issues/4404)
3536

3637
## 1.20.53
3738

src/cpptools.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,27 @@ export function getIntelliSenseMode(cptVersion: cpptools.Version, compilerPath:
315315
}
316316
}
317317

318+
/**
319+
* Try to find a target configuration with some populated properties.
320+
*
321+
* All targets get defaults for `compilerPath`, `compilerArgs`, and
322+
* `compilerFragments`, even `UTILITY` targets defined with
323+
* `add_custom_command()` that provide no other useful configuration, so if
324+
* possible, return one with more than just those populated.
325+
*/
326+
function fallbackConfiguration(configurations: Map<string, cpptools.SourceFileConfigurationItem> | undefined) {
327+
if (!configurations) {
328+
return undefined;
329+
}
330+
for (const item of configurations.values()) {
331+
const { configuration: { includePath, defines, intelliSenseMode, standard} } = item;
332+
if (includePath.length || defines.length || intelliSenseMode || standard) {
333+
return item;
334+
}
335+
}
336+
return configurations.values().next().value;
337+
}
338+
318339
/**
319340
* The actual class that provides information to the cpptools extension. See
320341
* the `CustomConfigurationProvider` interface for information on how this class
@@ -350,7 +371,7 @@ export class CppConfigurationProvider implements cpptools.CustomConfigurationPro
350371
if (this.activeTarget && configurations?.has(this.activeTarget)) {
351372
return configurations!.get(this.activeTarget);
352373
} else {
353-
return configurations?.values().next().value; // Any value is fine if the target doesn't match
374+
return fallbackConfiguration(configurations);
354375
}
355376
}
356377

test/unit-tests/cpptools.test.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,21 @@ suite('CppTools tests', () => {
319319
name: 'cpptools-test2',
320320
sourceDirectory: smokeFolder,
321321
targets: [
322+
{
323+
name: 'utilityTarget',
324+
type: 'UTILITY',
325+
fileGroups: [{
326+
sources: [sourceFile3],
327+
isGenerated: false
328+
}]
329+
},
322330
{
323331
name: 'target3',
324332
type: 'EXECUTABLE',
325333
fileGroups: [{
326334
sources: [sourceFile3],
327335
isGenerated: false,
336+
defines: ['DEFINE3'], // make this a more attractive fallback than utilityTarget
328337
compileCommandFragments: ['-DFRAGMENT3'],
329338
language: 'CXX'
330339
}]
@@ -368,7 +377,8 @@ suite('CppTools tests', () => {
368377
// Verify the browsePath with a different folder.
369378
const configurations2 = await provider.provideConfigurations([uri3]);
370379
expect(configurations2.length).to.eq(1);
371-
expect(configurations2[0].configuration.defines).to.be.empty;
380+
expect(configurations2[0].configuration.defines.length).to.eq(1);
381+
expect(configurations2[0].configuration.defines).to.contain('DEFINE3');
372382
expect(configurations2[0].configuration.compilerFragments).to.contain('-DFRAGMENT3');
373383
const browseConfig2 = await provider.provideFolderBrowseConfiguration(vscode.Uri.file(smokeFolder));
374384
expect(browseConfig2?.browsePath.length).to.eq(1);

0 commit comments

Comments
 (0)