Skip to content

Commit e8ea9b3

Browse files
committed
Bugfix: Ignore unrecognizable src queries
1 parent a143a38 commit e8ea9b3

File tree

5 files changed

+53
-50
lines changed

5 files changed

+53
-50
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,9 @@ clangd seems to have problems to resolve "isystem" added includes placed in the
7373
### Added
7474

7575
- Add a QuickPick popup to support workspace selection on startup
76+
77+
## [0.6.4] 2024-01-04
78+
79+
### Fixed
80+
81+
- Use only selected workspace for test discovery

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "vsc-bazel-tools",
33
"displayName": "VSC Bazel Tools",
44
"description": "IntelliSense support for C/C++ targets built with bazel.",
5-
"version": "0.6.3",
5+
"version": "0.6.4",
66
"publisher": "bjob",
77
"icon": "images/bazel_tools_icon.png",
88
"readme": "README.md",

src/extension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,14 @@ export async function activate(context: vscode.ExtensionContext) {
8282

8383
bazelTestCtrl.resolveHandler = async test => {
8484
if (!test) {
85-
await utils.discoverAllTestsInWorkspace();
85+
await utils.discoverAllTestsInWorkspace(bazelWorkspaceDir);
8686
} else {
8787
await utils.updateFromDisk(test);
8888
}
8989
};
9090

9191
bazelTestCtrl.refreshHandler = async () => {
92-
await utils.discoverAllTestsInWorkspace();
92+
await utils.discoverAllTestsInWorkspace(bazelWorkspaceDir);
9393
};
9494

9595
// When text documents are open, parse tests in them.

src/test_controller.ts

Lines changed: 42 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,60 +40,57 @@ export class Utils {
4040
return file.path.endsWith('.cpp') || file.path.endsWith('.cc');
4141
}
4242

43-
public async discoverAllTestsInWorkspace() {
44-
if (!vscode.workspace.workspaceFolders) {
45-
return []; // handle the case of no open folders
46-
}
43+
public async discoverAllTestsInWorkspace(bazelWorkspaceDir: string) {
44+
this.workspaceDirPath = bazelWorkspaceDir;
45+
46+
await glob("**/WORKSPACE*", { nodir: true, absolute: true, cwd: this.workspaceDirPath }).then((workspaceFiles) => {
47+
this.workspaceDirPath = path.dirname(workspaceFiles[0]);
48+
logger.info(`WORKSPACE file found in: ${this.workspaceDirPath}`);
49+
}).catch(() => {
50+
logger.error(`No WORKSPACE file found in ${this.workspaceDirPath}`);
51+
});
4752

48-
return Promise.all(
49-
vscode.workspace.workspaceFolders.map(async workspaceFolder => {
50-
this.workspaceDirPath = workspaceFolder.uri.fsPath;
53+
const bazelTestTargetsQuery = await runCommand("bazel", ["query", `kind(\"cc_test\", ${this.testDiscoverLabel})`], this.workspaceDirPath);
5154

52-
await glob("**/WORKSPACE*", { nodir: true, absolute: true, cwd: this.workspaceDirPath }).then((workspaceFiles) => {
53-
this.workspaceDirPath = path.dirname(workspaceFiles[0]);
54-
logger.info(`WORKSPACE file found in: ${this.workspaceDirPath}`);
55-
}).catch(() => {
56-
logger.error(`No WORKSPACE file found in ${this.workspaceDirPath}`);
57-
});
55+
if (bazelTestTargetsQuery.error) {
56+
throw new Error(`bazel query failed:\n ${bazelTestTargetsQuery.error.message}`);
57+
}
5858

59-
const bazelTestTargetsQuery = await runCommand("bazel", ["query", `kind(\"cc_test\", ${this.testDiscoverLabel})`], this.workspaceDirPath);
59+
const bazelTestTargets = bazelTestTargetsQuery.stdout;
6060

61-
if (bazelTestTargetsQuery.error) {
62-
throw new Error(`bazel query failed:\n ${bazelTestTargetsQuery.error.message}`);
61+
for (const testTarget of bazelTestTargets) {
62+
const bazelSrcsQuery = await runCommand("bazel", ["query", `labels(srcs, ${testTarget})`, "--output=location"], this.workspaceDirPath);
63+
if (bazelSrcsQuery.error) {
64+
throw new Error(`bazel query failed:\n ${bazelSrcsQuery.error.message}`);
65+
}
66+
for (const bazelSrc of bazelSrcsQuery.stdout) {
67+
const srcMatch = bazelSrc.match(/^(.*):1:1:.*/);
68+
if (!srcMatch) { // not a src file
69+
continue;
6370
}
64-
65-
const bazelTestTargets = bazelTestTargetsQuery.stdout;
66-
67-
for (const testTarget of bazelTestTargets) {
68-
const bazelSrcsQuery = await runCommand("bazel", ["query", `labels(srcs, ${testTarget})`, "--output=location"], this.workspaceDirPath);
69-
if (bazelSrcsQuery.error) {
70-
throw new Error(`bazel query failed:\n ${bazelSrcsQuery.error.message}`);
71-
}
72-
for (const bazelSrc of bazelSrcsQuery.stdout) {
73-
const srcFileUri = vscode.Uri.file(bazelSrc.match(/^(.*):1:1:.*/)[1]);
74-
if (!Utils.isCppTestFile(srcFileUri)) {
75-
continue;
76-
}
77-
const testItem = this.getOrCreateFile(srcFileUri);
78-
bazelTestLabels.set(testItem, testTarget);
79-
await this.updateFromDisk(testItem);
80-
}
71+
const srcFileUri = vscode.Uri.file(srcMatch[1]);
72+
if (!Utils.isCppTestFile(srcFileUri)) {
73+
continue;
8174
}
75+
const testItem = this.getOrCreateFile(srcFileUri);
76+
bazelTestLabels.set(testItem, testTarget);
77+
await this.updateFromDisk(testItem);
78+
}
79+
}
80+
81+
// const pattern = new vscode.RelativePattern(workspaceFolder, '**/*.cpp');
82+
// const watcher = vscode.workspace.createFileSystemWatcher(pattern);
8283

83-
// const pattern = new vscode.RelativePattern(workspaceFolder, '**/*.cpp');
84-
// const watcher = vscode.workspace.createFileSystemWatcher(pattern);
84+
// // When files are created, make sure there's a corresponding "file" node in the tree
85+
// watcher.onDidCreate(uri => this.getOrCreateFile(uri));
86+
// watcher.onDidChange(uri => this.parseTestsInFileContents(this.getOrCreateFile(uri)));
87+
// watcher.onDidDelete(uri => this.controller.items.delete(uri.toString()));
8588

86-
// // When files are created, make sure there's a corresponding "file" node in the tree
87-
// watcher.onDidCreate(uri => this.getOrCreateFile(uri));
88-
// watcher.onDidChange(uri => this.parseTestsInFileContents(this.getOrCreateFile(uri)));
89-
// watcher.onDidDelete(uri => this.controller.items.delete(uri.toString()));
89+
// for (const file of await vscode.workspace.findFiles(pattern)) {
90+
// this.getOrCreateFile(file);
91+
// }
92+
// return watcher;
9093

91-
// for (const file of await vscode.workspace.findFiles(pattern)) {
92-
// this.getOrCreateFile(file);
93-
// }
94-
// return watcher;
95-
})
96-
);
9794
}
9895

9996
getOrCreateFile(uri: vscode.Uri) {

0 commit comments

Comments
 (0)