Skip to content

Commit 0265bef

Browse files
authored
Move query language to the QlPackDetails (#3270)
1 parent aada351 commit 0265bef

5 files changed

Lines changed: 36 additions & 28 deletions

File tree

extensions/ql-vscode/src/variant-analysis/ql-pack-details.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { QueryLanguage } from "../common/query-language";
2+
13
/**
24
* Details about the original QL pack that is used for triggering
35
* a variant analysis.
@@ -12,4 +14,6 @@ export interface QlPackDetails {
1214
// The path to the QL pack file (a qlpack.yml or codeql-pack.yml) or undefined if
1315
// it doesn't exist.
1416
qlPackFilePath: string | undefined;
17+
18+
language: QueryLanguage;
1519
}

extensions/ql-vscode/src/variant-analysis/run-remote-query.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ import {
3434
QLPACK_FILENAMES,
3535
QLPACK_LOCK_FILENAMES,
3636
} from "../common/ql";
37-
import type { QueryLanguage } from "../common/query-language";
3837
import { tryGetQueryMetadata } from "../codeql-cli/query-metadata";
39-
import { askForLanguage, findLanguage } from "../codeql-cli/query-language";
4038
import type { QlPackFile } from "../packaging/qlpack-file";
4139
import { expandShortPaths } from "../common/short-paths";
4240
import type { QlPackDetails } from "./ql-pack-details";
@@ -46,11 +44,6 @@ import type { QlPackDetails } from "./ql-pack-details";
4644
*/
4745
const QUERY_PACK_NAME = "codeql-remote/query";
4846

49-
interface GeneratedQueryPack {
50-
base64Pack: string;
51-
language: string;
52-
}
53-
5447
/**
5548
* Two possibilities:
5649
* 1. There is no qlpack.yml (or codeql-pack.yml) in this directory. Assume this is a lone query and generate a synthetic qlpack for it.
@@ -62,7 +55,7 @@ async function generateQueryPack(
6255
cliServer: CodeQLCliServer,
6356
qlPackDetails: QlPackDetails,
6457
tmpDir: RemoteQueryTempDir,
65-
): Promise<GeneratedQueryPack> {
58+
): Promise<string> {
6659
const queryFile = qlPackDetails.queryFile;
6760

6861
const originalPackRoot = qlPackDetails.qlPackRootPath;
@@ -77,13 +70,6 @@ async function generateQueryPack(
7770
const cliSupportsMrvaPackCreate =
7871
await cliServer.cliConstraints.supportsMrvaPackCreate();
7972

80-
const language: QueryLanguage | undefined = mustSynthesizePack
81-
? await askForLanguage(cliServer) // open popup to ask for language if not already hardcoded
82-
: await findLanguage(cliServer, Uri.file(queryFile));
83-
if (!language) {
84-
throw new UserCancellationException("Could not determine language");
85-
}
86-
8773
let queryPackDir: string;
8874
let needsInstall: boolean;
8975
if (mustSynthesizePack) {
@@ -97,7 +83,7 @@ async function generateQueryPack(
9783
await createNewQueryPack(
9884
queryFile,
9985
queryPackDir,
100-
language,
86+
qlPackDetails.language,
10187
packRelativePath,
10288
);
10389
// Clear the cliServer cache so that the previous qlpack text is purged from the CLI.
@@ -176,10 +162,7 @@ async function generateQueryPack(
176162
precompilationOpts,
177163
);
178164
const base64Pack = (await readFile(bundlePath)).toString("base64");
179-
return {
180-
base64Pack,
181-
language,
182-
};
165+
return base64Pack;
183166
}
184167

185168
async function createNewQueryPack(
@@ -305,7 +288,6 @@ interface PreparedRemoteQuery {
305288
queryMetadata: QueryMetadata | undefined;
306289
controllerRepo: Repository;
307290
queryStartTime: number;
308-
language: string;
309291
}
310292

311293
export async function prepareRemoteQueryRun(
@@ -351,16 +333,14 @@ export async function prepareRemoteQueryRun(
351333

352334
const tempDir = await createRemoteQueriesTempDirectory();
353335

354-
let pack: GeneratedQueryPack;
336+
let base64Pack: string;
355337

356338
try {
357-
pack = await generateQueryPack(cliServer, qlPackDetails, tempDir);
339+
base64Pack = await generateQueryPack(cliServer, qlPackDetails, tempDir);
358340
} finally {
359341
await tempDir.remoteQueryDir.cleanup();
360342
}
361343

362-
const { base64Pack, language } = pack;
363-
364344
if (token.isCancellationRequested) {
365345
throw new UserCancellationException("Cancelled");
366346
}
@@ -384,7 +364,6 @@ export async function prepareRemoteQueryRun(
384364
queryMetadata,
385365
controllerRepo,
386366
queryStartTime,
387-
language,
388367
};
389368
}
390369

extensions/ql-vscode/src/variant-analysis/variant-analysis-manager.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ import type { QueryTreeViewItem } from "../queries-panel/query-tree-view-item";
8888
import { RequestError } from "@octokit/request-error";
8989
import { handleRequestError } from "./custom-errors";
9090
import { createMultiSelectionCommand } from "../common/vscode/selection-commands";
91-
import { askForLanguage } from "../codeql-cli/query-language";
91+
import { askForLanguage, findLanguage } from "../codeql-cli/query-language";
9292
import type { QlPackDetails } from "./ql-pack-details";
9393
import { findPackRoot, getQlPackFilePath } from "../common/ql";
9494

@@ -278,6 +278,7 @@ export class VariantAnalysisManager
278278
queryFile: problemQueries[0],
279279
qlPackRootPath: packDir,
280280
qlPackFilePath,
281+
language,
281282
};
282283

283284
await this.runVariantAnalysis(
@@ -315,10 +316,21 @@ export class VariantAnalysisManager
315316
// Build up details to pass to the functions that run the variant analysis.
316317
const qlPackRootPath = await findPackRoot(uri.fsPath);
317318
const qlPackFilePath = await getQlPackFilePath(qlPackRootPath);
319+
320+
// Open popup to ask for language if not already hardcoded
321+
const language = qlPackFilePath
322+
? await findLanguage(this.cliServer, uri)
323+
: await askForLanguage(this.cliServer);
324+
325+
if (!language) {
326+
throw new UserCancellationException("Could not determine query language");
327+
}
328+
318329
const qlPackDetails: QlPackDetails = {
319330
queryFile: uri.fsPath,
320331
qlPackRootPath,
321332
qlPackFilePath,
333+
language,
322334
};
323335

324336
return withProgress(
@@ -353,7 +365,6 @@ export class VariantAnalysisManager
353365
queryMetadata,
354366
controllerRepo,
355367
queryStartTime,
356-
language,
357368
} = await prepareRemoteQueryRun(
358369
this.cliServer,
359370
this.app.credentials,
@@ -364,6 +375,7 @@ export class VariantAnalysisManager
364375
);
365376

366377
const queryName = getQueryName(queryMetadata, queryFile);
378+
const language = qlPackDetails.language;
367379
const variantAnalysisLanguage = parseVariantAnalysisQueryLanguage(language);
368380
if (variantAnalysisLanguage === undefined) {
369381
throw new UserCancellationException(

extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-manager.test.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ describe("Variant Analysis Manager", () => {
108108
queryFile: filePath,
109109
qlPackRootPath,
110110
qlPackFilePath,
111+
language: QueryLanguage.Javascript,
111112
};
112113

113114
await variantAnalysisManager.runVariantAnalysis(
@@ -135,6 +136,7 @@ describe("Variant Analysis Manager", () => {
135136
queryFile: filePath,
136137
qlPackRootPath,
137138
qlPackFilePath: undefined,
139+
language: QueryLanguage.Javascript,
138140
};
139141

140142
await variantAnalysisManager.runVariantAnalysis(
@@ -167,6 +169,7 @@ describe("Variant Analysis Manager", () => {
167169
queryFile: filePath,
168170
qlPackRootPath,
169171
qlPackFilePath,
172+
language: QueryLanguage.Javascript,
170173
};
171174

172175
await variantAnalysisManager.runVariantAnalysis(
@@ -194,6 +197,7 @@ describe("Variant Analysis Manager", () => {
194197
queryFile: filePath,
195198
qlPackRootPath,
196199
qlPackFilePath: undefined,
200+
language: QueryLanguage.Javascript,
197201
};
198202

199203
const promise = variantAnalysisManager.runVariantAnalysis(
@@ -369,6 +373,7 @@ describe("Variant Analysis Manager", () => {
369373
queryFile: filePath,
370374
qlPackRootPath: getFileOrDir(qlPackRootPath),
371375
qlPackFilePath: qlPackFilePath && getFileOrDir(qlPackFilePath),
376+
language: QueryLanguage.Javascript,
372377
};
373378

374379
await variantAnalysisManager.runVariantAnalysis(

extensions/ql-vscode/test/vscode-tests/cli-integration/variant-analysis/variant-analysis-submission-integration.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ describe("Variant Analysis Submission Integration", () => {
8686
it("shows the error message", async () => {
8787
await showQlDocument("query.ql");
8888

89+
// Select target language for your query
90+
quickPickSpy.mockResolvedValueOnce(
91+
mockedQuickPickItem({
92+
label: "JavaScript",
93+
language: "javascript",
94+
}),
95+
);
96+
8997
await commandManager.execute("codeQL.runVariantAnalysis");
9098

9199
expect(showErrorMessageSpy).toHaveBeenCalledWith(

0 commit comments

Comments
 (0)