diff --git a/CHANGELOG.md b/CHANGELOG.md index 5f076c2..2d1b65a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- Only activate CMake Tools extension on load if workspace contains CMake + project files. This fixes issue #62. + ## [0.17.0] - 2023-01-26 ### Added diff --git a/src/cmake-adapter.ts b/src/cmake-adapter.ts index 5cdb2d1..95ca06b 100644 --- a/src/cmake-adapter.ts +++ b/src/cmake-adapter.ts @@ -32,7 +32,9 @@ import { getCtestPath, CmakeTestEvent, CmakeTestRunOptions, + isCmakeWorkspace, } from './cmake-runner'; +export { isCmakeWorkspace } from './cmake-runner'; /** Special ID value for the root suite */ const ROOT_SUITE_ID = '*'; @@ -284,7 +286,7 @@ export class CmakeAdapter implements TestAdapter { return rootSuite; } } catch (e) { - if (e instanceof CacheNotFoundError && !(await this.isCmakeWorkspace())) { + if (e instanceof CacheNotFoundError && !(await isCmakeWorkspace())) { // Ignore error when extension is not activable, return empty result instead this.log.info( `Workspace does not seem to contain CMake project files, ignoring tests` @@ -553,19 +555,6 @@ export class CmakeAdapter implements TestAdapter { ); } - /** - * Check whether the workspace contains CMake project files - * - * Note: we don't use `"activationEvents" for that because of issue - * [#57](https://github.com/fredericbonnet/cmake-test-explorer/issues/57). - * Testing the file presence explicitly allows us to make this test - * programmatically - */ - private async isCmakeWorkspace() { - const uris = await vscode.workspace.findFiles('**/CMakeLists.txt', null, 1); - return !!uris.length; - } - /** * Get & substitute config settings * diff --git a/src/cmake-runner.ts b/src/cmake-runner.ts index 7c6fdcf..7e73499 100644 --- a/src/cmake-runner.ts +++ b/src/cmake-runner.ts @@ -13,6 +13,9 @@ import { CmakeTestProcess } from './interfaces/cmake-test-process'; const { split } = require('split-cmd'); +/** Name of CMake project file used for activation */ +const CMAKE_PROJECT_FILE = 'CMakeLists.txt'; + /** Name of CMake cache file in build dir */ const CMAKE_CACHE_FILE = 'CMakeCache.txt'; @@ -350,3 +353,20 @@ export function getCmakeTestEnvironmentVariables( return acc; }, {} as { [key: string]: string }); } + +/** + * Check whether the workspace contains CMake project files + * + * Note: we don't use `"activationEvents" for that because of issue + * [#57](https://github.com/fredericbonnet/cmake-test-explorer/issues/57). + * Testing the file presence explicitly allows us to make this test + * programmatically + */ +export async function isCmakeWorkspace() { + const uris = await vscode.workspace.findFiles( + '**/' + CMAKE_PROJECT_FILE, + null, + 1 + ); + return !!uris.length; +} diff --git a/src/main.ts b/src/main.ts index b87681c..7cee605 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,7 +5,7 @@ import * as vscode from 'vscode'; import { TestHub, testExplorerExtensionId } from 'vscode-test-adapter-api'; import { Log, TestAdapterRegistrar } from 'vscode-test-adapter-util'; -import { CmakeAdapter } from './cmake-adapter'; +import { CmakeAdapter, isCmakeWorkspace } from './cmake-adapter'; /** * Main extension entry point @@ -39,7 +39,13 @@ export async function activate(context: vscode.ExtensionContext) { log.warn( `CMake integration is enabled but the CMake Tools extension is not active` ); - await cmakeExtension.activate(); + if (await isCmakeWorkspace()) { + // Only activate extension if the workspace contains CMake project files + log.info( + `Workspace contains CMake project files, waiting for CMake Tools extension to activate` + ); + await cmakeExtension.activate(); + } } } @@ -48,7 +54,7 @@ export async function activate(context: vscode.ExtensionContext) { testExplorerExtensionId ); if (log.enabled) - log.info(`Test Explorer ${testExplorerExtension ? '' : 'not '}found`); + log.warn(`Test Explorer ${testExplorerExtension ? '' : 'not '}found`); if (testExplorerExtension) { const testHub = testExplorerExtension.exports;