Skip to content

Commit 6fe7b82

Browse files
Only return something from getPathData once discover has run at least once
1 parent 1579859 commit 6fe7b82

6 files changed

Lines changed: 34 additions & 9 deletions

File tree

extensions/ql-vscode/src/common/vscode/file-path-discovery.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ interface PathData {
3434
* relevant, and what extra data to compute for each file.
3535
*/
3636
export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
37+
/**
38+
* Has `discover` been called. This allows distinguishing between
39+
* "no paths found" and not having scanned yet.
40+
*/
41+
private discoverHasCompletedOnce = false;
42+
3743
/** The set of known paths and associated data that we are tracking */
3844
private pathData: T[] = [];
3945

@@ -73,7 +79,10 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
7379
this.push(this.watcher.onDidChange(this.fileChanged.bind(this)));
7480
}
7581

76-
protected getPathData(): ReadonlyArray<Readonly<T>> {
82+
protected getPathData(): ReadonlyArray<Readonly<T>> | undefined {
83+
if (!this.discoverHasCompletedOnce) {
84+
return undefined;
85+
}
7786
return this.pathData;
7887
}
7988

@@ -171,6 +180,7 @@ export abstract class FilePathDiscovery<T extends PathData> extends Discovery {
171180
}
172181
}
173182

183+
this.discoverHasCompletedOnce = true;
174184
if (pathsUpdated) {
175185
this.onDidChangePathDataEmitter.fire();
176186
}

extensions/ql-vscode/src/queries-panel/query-discovery.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,15 @@ export class QueryDiscovery
5656
*
5757
* Trivial directories where there is only one child will be collapsed into a single node.
5858
*/
59-
public buildQueryTree(): Array<FileTreeNode<string>> {
59+
public buildQueryTree(): Array<FileTreeNode<string>> | undefined {
60+
const pathData = this.getPathData();
61+
if (pathData === undefined) {
62+
return undefined;
63+
}
64+
6065
const roots = [];
6166
for (const workspaceFolder of getOnDiskWorkspaceFoldersObjects()) {
62-
const queriesInRoot = this.getPathData().filter((query) =>
67+
const queriesInRoot = pathData.filter((query) =>
6368
containsPath(workspaceFolder.uri.fsPath, query.path),
6469
);
6570
if (queriesInRoot.length === 0) {

extensions/ql-vscode/src/queries-panel/query-pack-discovery.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
3737
*/
3838
public getLanguageForQueryFile(queryPath: string): QueryLanguage | undefined {
3939
// Find all packs in a higher directory than the query
40-
const packs = this.getPathData().filter((queryPack) =>
40+
const packs = (this.getPathData() || []).filter((queryPack) =>
4141
containsPath(dirname(queryPack.path), queryPath),
4242
);
4343

extensions/ql-vscode/src/queries-panel/query-tree-data-provider.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { DisposableObject } from "../common/disposable-object";
44
import { FileTreeNode } from "../common/file-tree-nodes";
55

66
export interface QueryDiscoverer {
7-
readonly buildQueryTree: () => Array<FileTreeNode<string>>;
7+
readonly buildQueryTree: () => Array<FileTreeNode<string>> | undefined;
88
readonly onDidChangeQueries: Event<void>;
99
}
1010

@@ -34,9 +34,11 @@ export class QueryTreeDataProvider
3434
}
3535

3636
private createTree(): QueryTreeViewItem[] {
37-
return this.queryDiscoverer
38-
.buildQueryTree()
39-
.map(this.convertFileTreeNode.bind(this));
37+
const queryTree = this.queryDiscoverer.buildQueryTree();
38+
if (queryTree === undefined) {
39+
return [];
40+
}
41+
return queryTree.map(this.convertFileTreeNode.bind(this));
4042
}
4143

4244
private convertFileTreeNode(

extensions/ql-vscode/test/vscode-tests/minimal-workspace/common/vscode/file-path-discovery.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class TestFilePathDiscovery extends FilePathDiscovery<TestData> {
3434
return this.onDidChangePathData;
3535
}
3636

37-
public getPathData(): readonly TestData[] {
37+
public getPathData(): readonly TestData[] | undefined {
3838
return super.getPathData();
3939
}
4040

@@ -123,6 +123,10 @@ describe("FilePathDiscovery", () => {
123123
});
124124

125125
describe("initialRefresh", () => {
126+
it("should return undefined until initialRefresh is called", async () => {
127+
expect(discovery.getPathData()).toEqual(undefined);
128+
});
129+
126130
it("should handle no files being present", async () => {
127131
await discovery.initialRefresh();
128132
expect(discovery.getPathData()).toEqual([]);

extensions/ql-vscode/test/vscode-tests/minimal-workspace/queries-panel/query-discovery.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ describe("Query pack discovery", () => {
5454
});
5555

5656
describe("buildQueryTree", () => {
57+
it("returns undefined before initial refresh has been done", async () => {
58+
expect(discovery.buildQueryTree()).toEqual(undefined);
59+
});
60+
5761
it("returns an empty tree when there are no query files", async () => {
5862
await discovery.initialRefresh();
5963

0 commit comments

Comments
 (0)