Skip to content

Commit 7554c41

Browse files
committed
Do not use the CLI server to determine query pack language
This removes the use of the CLI `codeql resolve library-path` command to detect the language of a query pack. Instead, it uses the `qlpack.yml` file to determine the language. This is slightly less correct since it only works for `codeql/${language}-all` dependencies, but it is much faster and more reliable. It also doesn't result in the CLI server restarting for invalid query packs (such as in the `github/codeql` repository or in any workspaces containing it).
1 parent 704895b commit 7554c41

File tree

4 files changed

+53
-96
lines changed

4 files changed

+53
-96
lines changed

extensions/ql-vscode/src/extension.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,7 @@ async function activateWithInstalledDistribution(
799799
);
800800
ctx.subscriptions.push(databaseUI);
801801

802-
const queriesModule = QueriesModule.initialize(
803-
app,
804-
languageContext,
805-
cliServer,
806-
);
802+
const queriesModule = QueriesModule.initialize(app, languageContext);
807803

808804
void extLogger.log("Initializing evaluator log viewer.");
809805
const evalLogViewer = new EvalLogViewer();

extensions/ql-vscode/src/queries-panel/queries-module.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CodeQLCliServer } from "../codeql-cli/cli";
21
import { extLogger } from "../common/logging/vscode";
32
import { App } from "../common/app";
43
import { DisposableObject } from "../common/disposable-object";
@@ -26,23 +25,18 @@ export class QueriesModule extends DisposableObject {
2625
public static initialize(
2726
app: App,
2827
languageContext: LanguageContextStore,
29-
cliServer: CodeQLCliServer,
3028
): QueriesModule {
3129
const queriesModule = new QueriesModule(app);
3230
app.subscriptions.push(queriesModule);
3331

34-
queriesModule.initialize(app, languageContext, cliServer);
32+
queriesModule.initialize(app, languageContext);
3533
return queriesModule;
3634
}
3735

38-
private initialize(
39-
app: App,
40-
langauageContext: LanguageContextStore,
41-
cliServer: CodeQLCliServer,
42-
): void {
36+
private initialize(app: App, langauageContext: LanguageContextStore): void {
4337
void extLogger.log("Initializing queries panel.");
4438

45-
const queryPackDiscovery = new QueryPackDiscovery(cliServer);
39+
const queryPackDiscovery = new QueryPackDiscovery();
4640
this.push(queryPackDiscovery);
4741
void queryPackDiscovery.initialRefresh();
4842

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

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { basename, dirname } from "path";
2-
import { CodeQLCliServer, QuerySetup } from "../codeql-cli/cli";
32
import { Event } from "vscode";
4-
import { QueryLanguage, dbSchemeToLanguage } from "../common/query-language";
3+
import { QueryLanguage } from "../common/query-language";
54
import { FALLBACK_QLPACK_FILENAME, QLPACK_FILENAMES } from "../common/ql";
65
import { FilePathDiscovery } from "../common/vscode/file-path-discovery";
7-
import { getErrorMessage } from "../common/helpers-pure";
8-
import { extLogger } from "../common/logging/vscode";
9-
import { EOL } from "os";
106
import { containsPath } from "../common/files";
11-
import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders";
7+
import { load } from "js-yaml";
8+
import { readFile } from "fs-extra";
9+
import { QlPackFile } from "../packaging/qlpack-file";
1210

1311
interface QueryPack {
1412
path: string;
@@ -19,7 +17,7 @@ interface QueryPack {
1917
* Discovers all query packs in the workspace.
2018
*/
2119
export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
22-
constructor(private readonly cliServer: CodeQLCliServer) {
20+
constructor() {
2321
super("Query Pack Discovery", `**/{${QLPACK_FILENAMES.join(",")}}`);
2422
}
2523

@@ -71,32 +69,32 @@ export class QueryPackDiscovery extends FilePathDiscovery<QueryPack> {
7169
}
7270

7371
protected async getDataForPath(path: string): Promise<QueryPack> {
74-
const language = await this.determinePackLanguage(path);
72+
let language: QueryLanguage | undefined;
73+
try {
74+
language = await this.determinePackLanguage(path);
75+
} catch (e) {
76+
language = undefined;
77+
}
7578
return { path, language };
7679
}
7780

7881
private async determinePackLanguage(
7982
path: string,
8083
): Promise<QueryLanguage | undefined> {
81-
let packInfo: QuerySetup | undefined = undefined;
82-
try {
83-
packInfo = await this.cliServer.resolveLibraryPath(
84-
getOnDiskWorkspaceFolders(),
85-
path,
86-
true,
87-
);
88-
} catch (err) {
89-
void extLogger.log(
90-
`Query pack discovery failed to determine language for query pack: ${path}${EOL}Reason: ${getErrorMessage(
91-
err,
92-
)}`,
93-
);
84+
const qlPack = load(await readFile(path, "utf8")) as QlPackFile | undefined;
85+
const dependencies = qlPack?.dependencies;
86+
if (!dependencies || typeof dependencies !== "object") {
87+
return;
9488
}
95-
if (packInfo?.dbscheme === undefined) {
89+
90+
const matchingLanguages = Object.values(QueryLanguage).filter(
91+
(language) => `codeql/${language}-all` in dependencies,
92+
);
93+
if (matchingLanguages.length !== 1) {
9694
return undefined;
9795
}
98-
const dbscheme = basename(packInfo.dbscheme);
99-
return dbSchemeToLanguage[dbscheme];
96+
97+
return matchingLanguages[0];
10098
}
10199

102100
protected pathIsRelevant(path: string): boolean {

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

Lines changed: 27 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,15 @@ import { Uri, workspace } from "vscode";
22
import { QueryPackDiscovery } from "../../../../src/queries-panel/query-pack-discovery";
33
import * as tmp from "tmp";
44
import { dirname, join } from "path";
5-
import { CodeQLCliServer, QuerySetup } from "../../../../src/codeql-cli/cli";
6-
import { mockedObject } from "../../utils/mocking.helpers";
7-
import { mkdirSync, writeFileSync } from "fs";
5+
import { ensureDir, writeJSON } from "fs-extra";
6+
import { QueryLanguage } from "../../../../src/common/query-language";
87

98
describe("Query pack discovery", () => {
109
let tmpDir: string;
1110
let tmpDirRemoveCallback: (() => void) | undefined;
1211

1312
let workspacePath: string;
1413

15-
let resolveLibraryPath: jest.SpiedFunction<
16-
typeof CodeQLCliServer.prototype.resolveLibraryPath
17-
>;
1814
let discovery: QueryPackDiscovery;
1915

2016
beforeEach(() => {
@@ -34,15 +30,7 @@ describe("Query pack discovery", () => {
3430
.spyOn(workspace, "workspaceFolders", "get")
3531
.mockReturnValue([workspaceFolder]);
3632

37-
const mockResolveLibraryPathValue: QuerySetup = {
38-
libraryPath: [],
39-
dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme",
40-
};
41-
resolveLibraryPath = jest
42-
.fn()
43-
.mockResolvedValue(mockResolveLibraryPathValue);
44-
const mockCliServer = mockedObject<CodeQLCliServer>({ resolveLibraryPath });
45-
discovery = new QueryPackDiscovery(mockCliServer);
33+
discovery = new QueryPackDiscovery();
4634
});
4735

4836
afterEach(() => {
@@ -60,7 +48,7 @@ describe("Query pack discovery", () => {
6048
});
6149

6250
it("locates a query pack in the same directory", async () => {
63-
makeTestFile(join(workspacePath, "qlpack.yml"));
51+
await makeTestFile(join(workspacePath, "qlpack.yml"));
6452

6553
await discovery.initialRefresh();
6654

@@ -70,7 +58,7 @@ describe("Query pack discovery", () => {
7058
});
7159

7260
it("locates a query pack using the old pack name", async () => {
73-
makeTestFile(join(workspacePath, "codeql-pack.yml"));
61+
await makeTestFile(join(workspacePath, "codeql-pack.yml"));
7462

7563
await discovery.initialRefresh();
7664

@@ -80,7 +68,7 @@ describe("Query pack discovery", () => {
8068
});
8169

8270
it("locates a query pack in a higher directory", async () => {
83-
makeTestFile(join(workspacePath, "qlpack.yml"));
71+
await makeTestFile(join(workspacePath, "qlpack.yml"));
8472

8573
await discovery.initialRefresh();
8674

@@ -92,7 +80,7 @@ describe("Query pack discovery", () => {
9280
});
9381

9482
it("doesn't recognise a query pack in a sibling directory", async () => {
95-
makeTestFile(join(workspacePath, "foo", "qlpack.yml"));
83+
await makeTestFile(join(workspacePath, "foo", "qlpack.yml"));
9684

9785
await discovery.initialRefresh();
9886

@@ -109,24 +97,11 @@ describe("Query pack discovery", () => {
10997
});
11098

11199
it("query packs override those from parent directories", async () => {
112-
makeTestFile(join(workspacePath, "qlpack.yml"));
113-
makeTestFile(join(workspacePath, "foo", "qlpack.yml"));
114-
115-
resolveLibraryPath.mockImplementation(async (_workspaces, queryPath) => {
116-
if (queryPath === join(workspacePath, "qlpack.yml")) {
117-
return {
118-
libraryPath: [],
119-
dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme",
120-
};
121-
}
122-
if (queryPath === join(workspacePath, "foo", "qlpack.yml")) {
123-
return {
124-
libraryPath: [],
125-
dbscheme: "/ql/cpp/ql/lib/semmlecode.cpp.dbscheme",
126-
};
127-
}
128-
throw new Error(`Unknown query pack: ${queryPath}`);
129-
});
100+
await makeTestFile(join(workspacePath, "qlpack.yml"), QueryLanguage.Java);
101+
await makeTestFile(
102+
join(workspacePath, "foo", "qlpack.yml"),
103+
QueryLanguage.Cpp,
104+
);
130105

131106
await discovery.initialRefresh();
132107

@@ -141,24 +116,11 @@ describe("Query pack discovery", () => {
141116
});
142117

143118
it("prefers a query pack called qlpack.yml", async () => {
144-
makeTestFile(join(workspacePath, "qlpack.yml"));
145-
makeTestFile(join(workspacePath, "codeql-pack.yml"));
146-
147-
resolveLibraryPath.mockImplementation(async (_workspaces, queryPath) => {
148-
if (queryPath === join(workspacePath, "qlpack.yml")) {
149-
return {
150-
libraryPath: [],
151-
dbscheme: "/ql/cpp/ql/lib/semmlecode.cpp.dbscheme",
152-
};
153-
}
154-
if (queryPath === join(workspacePath, "codeql-pack.yml")) {
155-
return {
156-
libraryPath: [],
157-
dbscheme: "/ql/java/ql/lib/config/semmlecode.dbscheme",
158-
};
159-
}
160-
throw new Error(`Unknown query pack: ${queryPath}`);
161-
});
119+
await makeTestFile(join(workspacePath, "qlpack.yml"), QueryLanguage.Cpp);
120+
await makeTestFile(
121+
join(workspacePath, "codeql-pack.yml"),
122+
QueryLanguage.Java,
123+
);
162124

163125
await discovery.initialRefresh();
164126

@@ -169,7 +131,14 @@ describe("Query pack discovery", () => {
169131
});
170132
});
171133

172-
function makeTestFile(path: string) {
173-
mkdirSync(dirname(path), { recursive: true });
174-
writeFileSync(path, "");
134+
async function makeTestFile(
135+
path: string,
136+
language: QueryLanguage = QueryLanguage.Java,
137+
) {
138+
await ensureDir(dirname(path));
139+
await writeJSON(path, {
140+
dependencies: {
141+
[`codeql/${language}-all`]: "*",
142+
},
143+
});
175144
}

0 commit comments

Comments
 (0)