Skip to content

Commit 6f43530

Browse files
committed
Add ability to run query with data extensions
- Add a new config that toggles between using all data extensions or none. - If using all data extensions, then before a query evaluation, run a `codeql resolve qlpacks` command with the new `--kind` option. This will return a list of extension packs in the workspace. - Pass these packs to the cli before evaluation/ - This will only work with CLI v2.12.3 (not yet released) or later. - Also include some CLI tests to ensure this works.
1 parent 57f9ff6 commit 6f43530

13 files changed

Lines changed: 171 additions & 6 deletions

File tree

extensions/ql-vscode/package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,19 @@
239239
"default": true,
240240
"description": "Enable the 'Quick Evaluation' CodeLens."
241241
},
242+
"codeQL.runningQueries.useExtensionPacks": {
243+
"type": "string",
244+
"default": "none",
245+
"enum": [
246+
"none",
247+
"all"
248+
],
249+
"enumDescriptions": [
250+
"Do not use extension packs.",
251+
"Use all extension packs found in the workspace."
252+
],
253+
"description": "Choose whether or not to run queries using extension packs. Requires CodeQL CLI v2.12.3 or later."
254+
},
242255
"codeQL.resultsDisplay.pageSize": {
243256
"type": "integer",
244257
"default": 200,

extensions/ql-vscode/src/cli.ts

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,19 +1142,26 @@ export class CodeQLCliServer implements Disposable {
11421142
* the default CLI search path is used.
11431143
* @returns A dictionary mapping qlpack name to the directory it comes from
11441144
*/
1145-
resolveQlpacks(
1145+
async resolveQlpacks(
11461146
additionalPacks: string[],
1147-
searchPath?: string[],
1147+
extensionPacksOnly = false,
11481148
): Promise<QlpacksInfo> {
11491149
const args = this.getAdditionalPacksArg(additionalPacks);
1150-
if (searchPath?.length) {
1151-
args.push("--search-path", join(...searchPath));
1150+
if (extensionPacksOnly) {
1151+
if (!(await this.cliConstraints.supportsQlpacksKind())) {
1152+
this.logger.log(
1153+
"Warning: Running with extension packs is only supported by CodeQL CLI v2.12.3 or later.",
1154+
);
1155+
return {};
1156+
}
1157+
args.push("--kind", "extension", "--no-recursive");
11521158
}
11531159

11541160
return this.runJsonCodeQlCliCommand<QlpacksInfo>(
11551161
["resolve", "qlpacks"],
11561162
args,
1157-
"Resolving qlpack information",
1163+
"Resolving qlpack information" +
1164+
(extensionPacksOnly ? " (extension packs only)" : ""),
11581165
);
11591166
}
11601167

@@ -1332,6 +1339,17 @@ export class CodeQLCliServer implements Disposable {
13321339
private getAdditionalPacksArg(paths: string[]): string[] {
13331340
return paths.length ? ["--additional-packs", paths.join(delimiter)] : [];
13341341
}
1342+
1343+
public async useExtensionPacks(): Promise<boolean> {
1344+
return (
1345+
this.cliConfig.useExtensionPacks &&
1346+
(await this.cliConstraints.supportsQlpacksKind())
1347+
);
1348+
}
1349+
1350+
public async setUseExtensionPacks(useExtensionPacks: boolean) {
1351+
this.cliConfig.setUseExtensionPacks(useExtensionPacks);
1352+
}
13351353
}
13361354

13371355
/**
@@ -1620,6 +1638,11 @@ export class CliVersionConstraint {
16201638
*/
16211639
public static CLI_VERSION_WITH_WORKSPACE_RFERENCES = new SemVer("2.11.3");
16221640

1641+
/**
1642+
* CLI version that supports the `--kind` option for the `resolve qlpacks` command.
1643+
*/
1644+
public static CLI_VERSION_WITH_QLPACKS_KIND = new SemVer("2.12.3");
1645+
16231646
constructor(private readonly cli: CodeQLCliServer) {
16241647
/**/
16251648
}
@@ -1677,4 +1700,10 @@ export class CliVersionConstraint {
16771700
CliVersionConstraint.CLI_VERSION_WITH_WORKSPACE_RFERENCES,
16781701
);
16791702
}
1703+
1704+
async supportsQlpacksKind() {
1705+
return this.isVersionAtLeast(
1706+
CliVersionConstraint.CLI_VERSION_WITH_QLPACKS_KIND,
1707+
);
1708+
}
16801709
}

extensions/ql-vscode/src/config.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ const DEBUG_SETTING = new Setting("debug", RUNNING_QUERIES_SETTING);
146146
const MAX_PATHS = new Setting("maxPaths", RUNNING_QUERIES_SETTING);
147147
const RUNNING_TESTS_SETTING = new Setting("runningTests", ROOT_SETTING);
148148
const RESULTS_DISPLAY_SETTING = new Setting("resultsDisplay", ROOT_SETTING);
149+
const USE_EXTENSION_PACKS = new Setting(
150+
"useExtensionPacks",
151+
RUNNING_QUERIES_SETTING,
152+
);
149153

150154
export const ADDITIONAL_TEST_ARGUMENTS_SETTING = new Setting(
151155
"additionalTestArguments",
@@ -205,14 +209,17 @@ const CLI_SETTINGS = [
205209
NUMBER_OF_TEST_THREADS_SETTING,
206210
NUMBER_OF_THREADS_SETTING,
207211
MAX_PATHS,
212+
USE_EXTENSION_PACKS,
208213
];
209214

210215
export interface CliConfig {
211216
additionalTestArguments: string[];
212217
numberTestThreads: number;
213218
numberThreads: number;
214219
maxPaths: number;
220+
useExtensionPacks: boolean;
215221
onDidChangeConfiguration?: Event<void>;
222+
setUseExtensionPacks: (useExtensionPacks: boolean) => Promise<void>;
216223
}
217224

218225
export abstract class ConfigListener extends DisposableObject {
@@ -409,6 +416,19 @@ export class CliConfigListener extends ConfigListener implements CliConfig {
409416
return MAX_PATHS.getValue<number>();
410417
}
411418

419+
public get useExtensionPacks(): boolean {
420+
// currently, we are restricting the values of this setting to 'all' or 'none'.
421+
return USE_EXTENSION_PACKS.getValue() === "all";
422+
}
423+
424+
// Exposed for testing only
425+
public async setUseExtensionPacks(newUseExtensionPacks: boolean) {
426+
await USE_EXTENSION_PACKS.updateValue(
427+
newUseExtensionPacks ? "all" : "none",
428+
ConfigurationTarget.Global,
429+
);
430+
}
431+
412432
protected handleDidChangeConfiguration(e: ConfigurationChangeEvent): void {
413433
this.handleDidChangeConfigurationForRelevantSettings(CLI_SETTINGS, e);
414434
}

extensions/ql-vscode/src/pure/new-messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ export interface RunQueryParams {
126126
singletonExternalInputs: Record<string, string>;
127127
dilPath?: string;
128128
logPath?: string;
129+
extensionPacks?: string[];
129130
}
130131

131132
export interface RunQueryResult {

extensions/ql-vscode/src/query-server/run-queries.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ export async function compileAndRunQueryAgainstDatabase(
7070
: { query: {} };
7171

7272
const diskWorkspaceFolders = getOnDiskWorkspaceFolders();
73+
const extensionPacks = (await qs.cliServer.useExtensionPacks())
74+
? Object.keys(await qs.cliServer.resolveQlpacks(diskWorkspaceFolders, true))
75+
: undefined;
76+
7377
const db = dbItem.databaseUri.fsPath;
7478
const logPath = queryInfo ? query.evalLogPath : undefined;
7579
const queryToRun: messages.RunQueryParams = {
@@ -82,6 +86,7 @@ export async function compileAndRunQueryAgainstDatabase(
8286
dilPath: query.dilPath,
8387
logPath,
8488
target,
89+
extensionPacks,
8590
};
8691
await query.createTimestampFile();
8792
let result: messages.RunQueryResult | undefined;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
extensions:
2+
- data:
3+
- [2]
4+
- [3]
5+
- [4]
6+
addsTo:
7+
extensible: testExtensible
8+
pack: semmle/has-extension
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: semmle/targets-extension
2+
library: true
3+
version: 0.0.0
4+
extensionTargets:
5+
semmle/has-extension: '*'
6+
7+
dataExtensions:
8+
- ext/*
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
extensions:
2+
- data:
3+
- [1]
4+
5+
addsTo:
6+
extensible: testExtensible
7+
pack: semmle/has-extension
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: semmle/has-extension
2+
version: 0.0.0
3+
dependencies:
4+
codeql/javascript-all: '*'
5+
dataExtensions:
6+
- ext/*
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
import javascript
3+
4+
extensible predicate testExtensible(int i);
5+
6+
from int i
7+
where testExtensible(i)
8+
select i

0 commit comments

Comments
 (0)