Skip to content

Commit

Permalink
Only activate CMake Tools extension on load if workspace contains CMa…
Browse files Browse the repository at this point in the history
…ke project files
  • Loading branch information
fredericbonnet committed Mar 12, 2023
1 parent b46f1e2 commit 44d44e1
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 17 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 3 additions & 14 deletions src/cmake-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '*';
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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
*
Expand Down
20 changes: 20 additions & 0 deletions src/cmake-runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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;
}
12 changes: 9 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
}
}

Expand All @@ -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;
Expand Down

0 comments on commit 44d44e1

Please sign in to comment.