Skip to content

Commit 89664f7

Browse files
authored
fix(workspace-tree): make bazel.commandLine.queryExpression behave more similarly to bazel query (#619)
* fix(workspace-tree): honor bazel.commandLine.queryExpression in tree view The Bazel Targets tree's per-package target listing used a hardcoded //pkg:all query, so targets that didn't match the user's configured queryExpression still appeared under each package node. queryPackages() already honored the expression, and #608 fixed the root-level listing, but per-package children still leaked through. Intersect the per-package target query with the user's expression so the tree matches what `bazel query <expression>` returns, consistent with the root-level and quick-picker behavior. Also updates the setting description to mention target filtering and adds test coverage for kind() filters, single-target filters, and empty-result filters. * fix formatting
1 parent bafb21a commit 89664f7

3 files changed

Lines changed: 94 additions & 3 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
"bazel.commandLine.queryExpression": {
196196
"type": "string",
197197
"default": "...:*",
198-
"description": "A [query language expression](https://bazel.build/query/language) which determines the packages displayed in the workspace tree and quick picker. The default inspects the entire workspace, but you could narrow it. For example: `//part/you/want/...:*`"
198+
"description": "A [query language expression](https://bazel.build/query/language) which determines the packages and targets displayed in the workspace tree and quick picker. The default inspects the entire workspace, but you could narrow it. For example: `//part/you/want/...:*` or `kind('.*_test rule', //...)`"
199199
},
200200
"bazel.lsp.command": {
201201
"type": "string",

src/workspace-tree/bazel_package_tree_item.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@ import {
1919
IBazelCommandAdapter,
2020
IBazelCommandOptions,
2121
} from "../bazel";
22-
import { getBazelExecutablePath } from "../extension/configuration";
22+
import {
23+
getBazelExecutablePath,
24+
getQueryExpression,
25+
} from "../extension/configuration";
2326
import { blaze_query } from "../protos";
2427
import { BazelTargetTreeItem } from "./bazel_target_tree_item";
2528
import { IBazelTreeItem } from "./bazel_tree_item";
@@ -55,10 +58,12 @@ export class BazelPackageTreeItem
5558
}
5659

5760
public async getChildren(): Promise<IBazelTreeItem[]> {
61+
const queryExpression = getQueryExpression();
62+
const targetQuery = `(${queryExpression}) intersect (//${this.packagePath}:all)`;
5863
const queryResult = await new BazelQuery(
5964
getBazelExecutablePath(),
6065
this.workspaceInfo.bazelWorkspacePath,
61-
).queryTargets(`//${this.packagePath}:all`, {
66+
).queryTargets(targetQuery, {
6267
ignoresErrors: true,
6368
sortByRuleName: true,
6469
});

test/workspace_tree.test.ts

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,92 @@ describe("Bazel Workspace Tree", function (this: Mocha.Suite) {
122122
);
123123
});
124124

125+
describe("queryExpression filtering", () => {
126+
afterEach(async () => {
127+
await vscode.workspace
128+
.getConfiguration("bazel.commandLine")
129+
.update(
130+
"queryExpression",
131+
undefined,
132+
vscode.ConfigurationTarget.Workspace,
133+
);
134+
});
135+
136+
it("should filter targets by rule kind", async () => {
137+
await vscode.workspace
138+
.getConfiguration("bazel.commandLine")
139+
.update(
140+
"queryExpression",
141+
"kind('py_.*', //...)",
142+
vscode.ConfigurationTarget.Workspace,
143+
);
144+
145+
await verifyTreeStructure(
146+
{
147+
"//pkg1": {
148+
":main (py_binary)": {},
149+
":pkg1 (py_library)": {},
150+
},
151+
},
152+
await workspaceTreeProvider.getChildren(),
153+
);
154+
});
155+
156+
it("should filter to filegroup targets only", async () => {
157+
await vscode.workspace
158+
.getConfiguration("bazel.commandLine")
159+
.update(
160+
"queryExpression",
161+
"kind('filegroup', //...)",
162+
vscode.ConfigurationTarget.Workspace,
163+
);
164+
165+
await verifyTreeStructure(
166+
{
167+
"//pkg1": {
168+
":foo (filegroup)": {},
169+
":src_files (filegroup)": {},
170+
},
171+
"//pkg2/sub-pkg": {
172+
":foobar (filegroup)": {},
173+
},
174+
},
175+
await workspaceTreeProvider.getChildren(),
176+
);
177+
});
178+
179+
it("should filter to a single target", async () => {
180+
await vscode.workspace
181+
.getConfiguration("bazel.commandLine")
182+
.update(
183+
"queryExpression",
184+
"//pkg1:main",
185+
vscode.ConfigurationTarget.Workspace,
186+
);
187+
188+
await verifyTreeStructure(
189+
{
190+
"//pkg1": {
191+
":main (py_binary)": {},
192+
},
193+
},
194+
await workspaceTreeProvider.getChildren(),
195+
);
196+
});
197+
198+
it("should show empty tree when expression matches nothing", async () => {
199+
await vscode.workspace
200+
.getConfiguration("bazel.commandLine")
201+
.update(
202+
"queryExpression",
203+
"kind('java_library', //...)",
204+
vscode.ConfigurationTarget.Workspace,
205+
);
206+
207+
await verifyTreeStructure({}, await workspaceTreeProvider.getChildren());
208+
});
209+
});
210+
125211
it("does not select tree item when bazel view is hidden", async () => {
126212
// GIVEN another view is active (Search) instead of the Explorer
127213
await vscode.commands.executeCommand("workbench.view.search");

0 commit comments

Comments
 (0)