Skip to content

Add command for running a query suite #4037

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions extensions/ql-vscode/package.json
Original file line number Diff line number Diff line change
@@ -571,6 +571,10 @@
"command": "codeQL.runQueries",
"title": "CodeQL: Run Queries in Selected Files"
},
{
"command": "codeQL.runQuerySuite",
"title": "CodeQL: Run Selected Query Suite"
},
{
"command": "codeQL.quickEval",
"title": "CodeQL: Quick Evaluation"
@@ -1361,6 +1365,11 @@
"group": "9_qlCommands",
"when": "resourceScheme != codeql-zip-archive"
},
{
"command": "codeQL.runQuerySuite",
"group": "9_qlCommands",
"when": "resourceScheme != codeql-zip-archive && resourceExtname == .qls && !explorerResourceIsFolder && !listMultiSelection && config.codeQL.canary"
},
{
"command": "codeQL.runVariantAnalysisContextExplorer",
"group": "9_qlCommands",
@@ -1458,6 +1467,10 @@
"command": "codeQL.runQueries",
"when": "false"
},
{
"command": "codeQL.runQuerySuite",
"when": "false"
},
{
"command": "codeQL.quickEval",
"when": "editorLangId == ql"
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/codeql-cli/cli-version.ts
Original file line number Diff line number Diff line change
@@ -13,6 +13,7 @@ export interface CliFeatures {
featuresInVersionResult?: boolean;
mrvaPackCreate?: boolean;
generateSummarySymbolMap?: boolean;
queryServerRunQueries?: boolean;
}

export interface VersionAndFeatures {
1 change: 1 addition & 0 deletions extensions/ql-vscode/src/common/commands.ts
Original file line number Diff line number Diff line change
@@ -138,6 +138,7 @@ export type LocalQueryCommands = {
"codeQLQueries.createQuery": () => Promise<void>;
"codeQL.runLocalQueryFromFileTab": (uri: Uri) => Promise<void>;
"codeQL.runQueries": ExplorerSelectionCommandFunction<Uri>;
"codeQL.runQuerySuite": ExplorerSelectionCommandFunction<Uri>;
"codeQL.quickEval": (uri: Uri) => Promise<void>;
"codeQL.quickEvalCount": (uri: Uri) => Promise<void>;
"codeQL.quickEvalContextEditor": (uri: Uri) => Promise<void>;
19 changes: 8 additions & 11 deletions extensions/ql-vscode/src/compare/compare-view.ts
Original file line number Diff line number Diff line change
@@ -70,22 +70,20 @@ export class CompareView extends AbstractWebview<
selectedResultSetName?: string,
) {
const [fromSchemas, toSchemas] = await Promise.all([
this.cliServer.bqrsInfo(
from.completedQuery.query.resultsPaths.resultsPath,
),
this.cliServer.bqrsInfo(to.completedQuery.query.resultsPaths.resultsPath),
this.cliServer.bqrsInfo(from.completedQuery.query.resultsPath),
this.cliServer.bqrsInfo(to.completedQuery.query.resultsPath),
]);

const [fromSchemaNames, toSchemaNames] = await Promise.all([
getResultSetNames(
fromSchemas,
from.completedQuery.query.metadata,
from.completedQuery.query.resultsPaths.interpretedResultsPath,
from.completedQuery.query.interpretedResultsPath,
),
getResultSetNames(
toSchemas,
to.completedQuery.query.metadata,
to.completedQuery.query.resultsPaths.interpretedResultsPath,
to.completedQuery.query.interpretedResultsPath,
),
]);

@@ -101,15 +99,14 @@ export class CompareView extends AbstractWebview<
schemaNames: fromSchemaNames,
metadata: from.completedQuery.query.metadata,
interpretedResultsPath:
from.completedQuery.query.resultsPaths.interpretedResultsPath,
from.completedQuery.query.interpretedResultsPath,
},
to,
toInfo: {
schemas: toSchemas,
schemaNames: toSchemaNames,
metadata: to.completedQuery.query.metadata,
interpretedResultsPath:
to.completedQuery.query.resultsPaths.interpretedResultsPath,
interpretedResultsPath: to.completedQuery.query.interpretedResultsPath,
},
commonResultSetNames,
};
@@ -392,12 +389,12 @@ export class CompareView extends AbstractWebview<
this.getResultSet(
fromInfo.schemas,
fromResultSetName,
from.completedQuery.query.resultsPaths.resultsPath,
from.completedQuery.query.resultsPath,
),
this.getResultSet(
toInfo.schemas,
toResultSetName,
to.completedQuery.query.resultsPaths.resultsPath,
to.completedQuery.query.resultsPath,
),
]);

6 changes: 2 additions & 4 deletions extensions/ql-vscode/src/compare/interpreted-results.ts
Original file line number Diff line number Diff line change
@@ -36,11 +36,9 @@ export async function compareInterpretedResults(

const [fromResultSet, toResultSet, sourceLocationPrefix] = await Promise.all([
getInterpretedResults(
fromQuery.completedQuery.query.resultsPaths.interpretedResultsPath,
),
getInterpretedResults(
toQuery.completedQuery.query.resultsPaths.interpretedResultsPath,
fromQuery.completedQuery.query.interpretedResultsPath,
),
getInterpretedResults(toQuery.completedQuery.query.interpretedResultsPath),
database.getSourceLocationPrefix(cliServer),
]);

1 change: 1 addition & 0 deletions extensions/ql-vscode/src/debugger/debug-protocol.ts
Original file line number Diff line number Diff line change
@@ -39,6 +39,7 @@ export interface EvaluationCompletedEvent extends Event {
resultType: QueryResultType;
message: string | undefined;
evaluationTime: number;
outputBaseName: string;
};
}

35 changes: 25 additions & 10 deletions extensions/ql-vscode/src/debugger/debug-session.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import type { BaseLogger, LogOptions } from "../common/logging";
import { queryServerLogger } from "../common/logging/vscode";
import { QueryResultType } from "../query-server/messages";
import type {
CoreQueryResults,
CoreQueryResult,
CoreQueryRun,
QueryRunner,
} from "../query-server";
@@ -25,6 +25,7 @@ import type * as CodeQLProtocol from "./debug-protocol";
import type { QuickEvalContext } from "../run-queries-shared";
import { getErrorMessage } from "../common/helpers-pure";
import { DisposableObject } from "../common/disposable-object";
import { basename } from "path";

// More complete implementations of `Event` for certain events, because the classes from
// `@vscode/debugadapter` make it more difficult to provide some of the message values.
@@ -107,9 +108,9 @@ class EvaluationCompletedEvent
public readonly event = "codeql-evaluation-completed";
public readonly body: CodeQLProtocol.EvaluationCompletedEvent["body"];

constructor(results: CoreQueryResults) {
constructor(result: CoreQueryResult) {
super("codeql-evaluation-completed");
this.body = results;
this.body = result;
}
}

@@ -143,6 +144,7 @@ const QUERY_THREAD_NAME = "Evaluation thread";
class RunningQuery extends DisposableObject {
private readonly tokenSource = this.push(new CancellationTokenSource());
public readonly queryRun: CoreQueryRun;
private readonly queryPath: string;

public constructor(
queryRunner: QueryRunner,
@@ -154,21 +156,25 @@ class RunningQuery extends DisposableObject {
) {
super();

this.queryPath = config.query;
// Create the query run, which will give us some information about the query even before the
// evaluation has completed.
this.queryRun = queryRunner.createQueryRun(
config.database,
{
queryPath: config.query,
quickEvalPosition: quickEvalContext?.quickEvalPosition,
quickEvalCountOnly: quickEvalContext?.quickEvalCount,
},
[
{
queryPath: this.queryPath,
outputBaseName: "results",
quickEvalPosition: quickEvalContext?.quickEvalPosition,
quickEvalCountOnly: quickEvalContext?.quickEvalCount,
},
],
true,
config.additionalPacks,
config.extensionPacks,
config.additionalRunQueryArgs,
queryStorageDir,
undefined,
basename(config.query),
undefined,
);
}
@@ -208,7 +214,7 @@ class RunningQuery extends DisposableObject {
progressStart.body.cancellable = true;
this.sendEvent(progressStart);
try {
return await this.queryRun.evaluate(
const completedQuery = await this.queryRun.evaluate(
(p) => {
const progressUpdate = new ProgressUpdateEvent(
this.queryRun.id,
@@ -220,6 +226,14 @@ class RunningQuery extends DisposableObject {
this.tokenSource.token,
this.logger,
);
return (
completedQuery.results.get(this.queryPath) ?? {
resultType: QueryResultType.OTHER_ERROR,
message: "Missing query results",
evaluationTime: 0,
outputBaseName: "unknown",
}
);
} finally {
this.sendEvent(new ProgressEndEvent(this.queryRun.id));
}
@@ -229,6 +243,7 @@ class RunningQuery extends DisposableObject {
resultType: QueryResultType.OTHER_ERROR,
message,
evaluationTime: 0,
outputBaseName: "unknown",
};
}
}
13 changes: 10 additions & 3 deletions extensions/ql-vscode/src/debugger/debugger-ui.ts
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ import { debug, Uri, CancellationTokenSource } from "vscode";
import type { DebuggerCommands } from "../common/commands";
import type { DatabaseManager } from "../databases/local-databases";
import { DisposableObject } from "../common/disposable-object";
import type { CoreQueryResults } from "../query-server";
import type { CoreQueryResult } from "../query-server";
import {
getQuickEvalContext,
saveBeforeStart,
@@ -134,8 +134,15 @@ class QLDebugAdapterTracker
body: EvaluationCompletedEvent["body"],
): Promise<void> {
if (this.localQueryRun !== undefined) {
const results: CoreQueryResults = body;
await this.localQueryRun.complete(results, (_) => {});
const results: CoreQueryResult = body;
await this.localQueryRun.complete(
{
results: new Map<string, CoreQueryResult>([
[this.configuration.query, results],
]),
},
(_) => {},
);
this.localQueryRun = undefined;
}
}
Original file line number Diff line number Diff line change
@@ -7,7 +7,6 @@ import type {
import type { DatabaseItem } from "../../databases/local-databases";
import type { ChildAstItem, AstItem } from "./ast-viewer";
import type { Uri } from "vscode";
import type { QueryOutputDir } from "../../local-queries/query-output-dir";
import { fileRangeFromURI } from "../contextual/file-range-from-uri";
import { mapUrlValue } from "../../common/bqrs-raw-results-mapper";

@@ -17,15 +16,12 @@ import { mapUrlValue } from "../../common/bqrs-raw-results-mapper";
*/
export class AstBuilder {
private roots: AstItem[] | undefined;
private bqrsPath: string;
constructor(
outputDir: QueryOutputDir,
private readonly bqrsPath: string,
private cli: CodeQLCliServer,
public db: DatabaseItem,
public fileName: Uri,
) {
this.bqrsPath = outputDir.bqrsPath;
}
) {}

async getRoots(): Promise<AstItem[]> {
if (!this.roots) {
Original file line number Diff line number Diff line change
@@ -21,7 +21,6 @@ import {
} from "./query-resolver";
import type { CancellationToken, LocationLink } from "vscode";
import { Uri } from "vscode";
import type { QueryOutputDir } from "../../local-queries/query-output-dir";
import type { QueryRunner } from "../../query-server";
import { QueryResultType } from "../../query-server/messages";
import { fileRangeFromURI } from "./file-range-from-uri";
@@ -84,23 +83,28 @@ export async function getLocationsForUriString(
token,
templates,
);
if (results.resultType === QueryResultType.SUCCESS) {
const queryResult = results.results.get(query);
if (queryResult?.resultType === QueryResultType.SUCCESS) {
links.push(
...(await getLinksFromResults(results.outputDir, cli, db, filter)),
...(await getLinksFromResults(
results.outputDir.getBqrsPath(queryResult.outputBaseName),
cli,
db,
filter,
)),
);
}
}
return links;
}

async function getLinksFromResults(
outputDir: QueryOutputDir,
bqrsPath: string,
cli: CodeQLCliServer,
db: DatabaseItem,
filter: (srcFile: string, destFile: string) => boolean,
): Promise<FullLocationLink[]> {
const localLinks: FullLocationLink[] = [];
const bqrsPath = outputDir.bqrsPath;
const info = await cli.bqrsInfo(bqrsPath);
const selectInfo = info["result-sets"].find(
(schema) => schema.name === SELECT_QUERY_NAME,
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ import type { CancellationToken } from "vscode";
import type { ProgressCallback } from "../../common/vscode/progress";
import type { CoreCompletedQuery, QueryRunner } from "../../query-server";
import { createLockFileForStandardQuery } from "../../local-queries/standard-queries";
import { basename } from "path";

/**
* This wil try to determine the qlpacks for a given database. If it can't find a matching
@@ -80,13 +81,19 @@ export async function runContextualQuery(
const { cleanup } = await createLockFileForStandardQuery(cli, query);
const queryRun = qs.createQueryRun(
db.databaseUri.fsPath,
{ queryPath: query, quickEvalPosition: undefined },
[
{
queryPath: query,
outputBaseName: "results",
quickEvalPosition: undefined,
},
],
false,
getOnDiskWorkspaceFolders(),
undefined,
{},
queryStorageDir,
undefined,
basename(query),
templates,
);
void extLogger.log(
Original file line number Diff line number Diff line change
@@ -209,8 +209,14 @@ export class TemplatePrintAstProvider {
? await this.cache.get(fileUri.toString(), progress, token)
: await this.getAst(fileUri.toString(), progress, token);

const queryResults = Array.from(completedQuery.results.values());
if (queryResults.length !== 1) {
throw new Error(
`Expected exactly one query result, but found ${queryResults.length}.`,
);
}
return new AstBuilder(
completedQuery.outputDir,
completedQuery.outputDir.getBqrsPath(queryResults[0].outputBaseName),
this.cli,
this.dbm.findDatabaseItem(Uri.file(completedQuery.dbPath))!,
fileUri,
Loading
Oops, something went wrong.
Loading
Oops, something went wrong.