|
| 1 | +import { CodeQLCliServer, SourceInfo } from "../codeql-cli/cli"; |
| 2 | +import { QueryRunner } from "../query-server"; |
| 3 | +import { DatabaseItem } from "../databases/local-databases"; |
| 4 | +import { ProgressCallback } from "../common/vscode/progress"; |
| 5 | +import * as Sarif from "sarif"; |
| 6 | +import { qlpackOfDatabase, resolveQueries } from "../local-queries"; |
| 7 | +import { extLogger } from "../common/logging/vscode"; |
| 8 | +import { Mode } from "./shared/mode"; |
| 9 | +import { QlPacksForLanguage } from "../databases/qlpack"; |
| 10 | +import { createLockFileForStandardQuery } from "../local-queries/standard-queries"; |
| 11 | +import { CancellationToken, CancellationTokenSource } from "vscode"; |
| 12 | +import { getOnDiskWorkspaceFolders } from "../common/vscode/workspace-folders"; |
| 13 | +import { showAndLogExceptionWithTelemetry, TeeLogger } from "../common/logging"; |
| 14 | +import { QueryResultType } from "../query-server/new-messages"; |
| 15 | +import { telemetryListener } from "../common/vscode/telemetry"; |
| 16 | +import { redactableError } from "../common/errors"; |
| 17 | +import { interpretResultsSarif } from "../query-results"; |
| 18 | +import { join } from "path"; |
| 19 | +import { assertNever } from "../common/helpers-pure"; |
| 20 | + |
| 21 | +type AutoModelQueryOptions = { |
| 22 | + queryTag: string; |
| 23 | + mode: Mode; |
| 24 | + cliServer: CodeQLCliServer; |
| 25 | + queryRunner: QueryRunner; |
| 26 | + databaseItem: DatabaseItem; |
| 27 | + qlpack: QlPacksForLanguage; |
| 28 | + sourceInfo: SourceInfo | undefined; |
| 29 | + extensionPacks: string[]; |
| 30 | + queryStorageDir: string; |
| 31 | + |
| 32 | + progress: ProgressCallback; |
| 33 | + token: CancellationToken; |
| 34 | +}; |
| 35 | + |
| 36 | +function modeTag(mode: Mode): string { |
| 37 | + switch (mode) { |
| 38 | + case Mode.Application: |
| 39 | + return "application-mode"; |
| 40 | + case Mode.Framework: |
| 41 | + return "framework-mode"; |
| 42 | + default: |
| 43 | + assertNever(mode); |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +async function runAutoModelQuery({ |
| 48 | + queryTag, |
| 49 | + mode, |
| 50 | + cliServer, |
| 51 | + queryRunner, |
| 52 | + databaseItem, |
| 53 | + qlpack, |
| 54 | + sourceInfo, |
| 55 | + extensionPacks, |
| 56 | + queryStorageDir, |
| 57 | + progress, |
| 58 | + token, |
| 59 | +}: AutoModelQueryOptions): Promise<Sarif.Log | undefined> { |
| 60 | + // First, resolve the query that we want to run. |
| 61 | + // All queries are tagged like this: |
| 62 | + // internal extract automodel <mode> <queryTag> |
| 63 | + // Example: internal extract automodel framework-mode candidates |
| 64 | + const queries = await resolveQueries( |
| 65 | + cliServer, |
| 66 | + qlpack, |
| 67 | + `Extract automodel ${queryTag}`, |
| 68 | + { |
| 69 | + kind: "problem", |
| 70 | + "tags contain all": ["automodel", modeTag(mode), ...queryTag.split(" ")], |
| 71 | + }, |
| 72 | + ); |
| 73 | + if (queries.length > 1) { |
| 74 | + throw new Error( |
| 75 | + `Found multiple auto model queries for ${mode} ${queryTag}. Can't continue`, |
| 76 | + ); |
| 77 | + } |
| 78 | + if (queries.length === 0) { |
| 79 | + throw new Error( |
| 80 | + `Did not found any auto model queries for ${mode} ${queryTag}. Can't continue`, |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + const queryPath = queries[0]; |
| 85 | + const { cleanup: cleanupLockFile } = await createLockFileForStandardQuery( |
| 86 | + cliServer, |
| 87 | + queryPath, |
| 88 | + ); |
| 89 | + |
| 90 | + // Get metadata for the query. This is required to interpret the results. We already know the kind is problem |
| 91 | + // (because of the constraint in resolveQueries), so we don't need any more checks on the metadata. |
| 92 | + const metadata = await cliServer.resolveMetadata(queryPath); |
| 93 | + |
| 94 | + const queryRun = queryRunner.createQueryRun( |
| 95 | + databaseItem.databaseUri.fsPath, |
| 96 | + { |
| 97 | + queryPath, |
| 98 | + quickEvalPosition: undefined, |
| 99 | + quickEvalCountOnly: false, |
| 100 | + }, |
| 101 | + false, |
| 102 | + getOnDiskWorkspaceFolders(), |
| 103 | + extensionPacks, |
| 104 | + queryStorageDir, |
| 105 | + undefined, |
| 106 | + undefined, |
| 107 | + ); |
| 108 | + |
| 109 | + const completedQuery = await queryRun.evaluate( |
| 110 | + progress, |
| 111 | + token, |
| 112 | + new TeeLogger(queryRunner.logger, queryRun.outputDir.logPath), |
| 113 | + ); |
| 114 | + |
| 115 | + await cleanupLockFile?.(); |
| 116 | + |
| 117 | + if (completedQuery.resultType !== QueryResultType.SUCCESS) { |
| 118 | + void showAndLogExceptionWithTelemetry( |
| 119 | + extLogger, |
| 120 | + telemetryListener, |
| 121 | + redactableError`Auto-model query ${queryTag} failed: ${ |
| 122 | + completedQuery.message ?? "No message" |
| 123 | + }`, |
| 124 | + ); |
| 125 | + return; |
| 126 | + } |
| 127 | + |
| 128 | + const interpretedResultsPath = join( |
| 129 | + queryStorageDir, |
| 130 | + `interpreted-results-${queryTag.replaceAll(" ", "-")}-${queryRun.id}.sarif`, |
| 131 | + ); |
| 132 | + |
| 133 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars -- We only need the actual SARIF data, not the extra fields added by SarifInterpretationData |
| 134 | + const { t, sortState, ...sarif } = await interpretResultsSarif( |
| 135 | + cliServer, |
| 136 | + metadata, |
| 137 | + { |
| 138 | + resultsPath: completedQuery.outputDir.bqrsPath, |
| 139 | + interpretedResultsPath, |
| 140 | + }, |
| 141 | + sourceInfo, |
| 142 | + ["--sarif-add-snippets"], |
| 143 | + ); |
| 144 | + |
| 145 | + return sarif; |
| 146 | +} |
| 147 | + |
| 148 | +type AutoModelQueriesOptions = { |
| 149 | + mode: Mode; |
| 150 | + cliServer: CodeQLCliServer; |
| 151 | + queryRunner: QueryRunner; |
| 152 | + databaseItem: DatabaseItem; |
| 153 | + queryStorageDir: string; |
| 154 | + |
| 155 | + progress: ProgressCallback; |
| 156 | +}; |
| 157 | + |
| 158 | +export type AutoModelQueriesResult = { |
| 159 | + candidates: Sarif.Log; |
| 160 | +}; |
| 161 | + |
| 162 | +export async function runAutoModelQueries({ |
| 163 | + mode, |
| 164 | + cliServer, |
| 165 | + queryRunner, |
| 166 | + databaseItem, |
| 167 | + queryStorageDir, |
| 168 | + progress, |
| 169 | +}: AutoModelQueriesOptions): Promise<AutoModelQueriesResult | undefined> { |
| 170 | + // maxStep for this part is 1500 |
| 171 | + const maxStep = 1500; |
| 172 | + |
| 173 | + const cancellationTokenSource = new CancellationTokenSource(); |
| 174 | + |
| 175 | + const qlpack = await qlpackOfDatabase(cliServer, databaseItem); |
| 176 | + |
| 177 | + // CodeQL needs to have access to the database to be able to retrieve the |
| 178 | + // snippets from it. The source location prefix is used to determine the |
| 179 | + // base path of the database. |
| 180 | + const sourceLocationPrefix = await databaseItem.getSourceLocationPrefix( |
| 181 | + cliServer, |
| 182 | + ); |
| 183 | + const sourceArchiveUri = databaseItem.sourceArchive; |
| 184 | + const sourceInfo = |
| 185 | + sourceArchiveUri === undefined |
| 186 | + ? undefined |
| 187 | + : { |
| 188 | + sourceArchive: sourceArchiveUri.fsPath, |
| 189 | + sourceLocationPrefix, |
| 190 | + }; |
| 191 | + |
| 192 | + const additionalPacks = getOnDiskWorkspaceFolders(); |
| 193 | + const extensionPacks = Object.keys( |
| 194 | + await cliServer.resolveQlpacks(additionalPacks, true), |
| 195 | + ); |
| 196 | + |
| 197 | + progress({ |
| 198 | + step: 0, |
| 199 | + maxStep, |
| 200 | + message: "Finding candidates and examples", |
| 201 | + }); |
| 202 | + |
| 203 | + const candidates = await runAutoModelQuery({ |
| 204 | + mode, |
| 205 | + queryTag: "candidates", |
| 206 | + cliServer, |
| 207 | + queryRunner, |
| 208 | + databaseItem, |
| 209 | + qlpack, |
| 210 | + sourceInfo, |
| 211 | + extensionPacks, |
| 212 | + queryStorageDir, |
| 213 | + progress: (update) => { |
| 214 | + progress({ |
| 215 | + step: update.step, |
| 216 | + maxStep, |
| 217 | + message: "Finding candidates and examples", |
| 218 | + }); |
| 219 | + }, |
| 220 | + token: cancellationTokenSource.token, |
| 221 | + }); |
| 222 | + |
| 223 | + if (!candidates) { |
| 224 | + return undefined; |
| 225 | + } |
| 226 | + |
| 227 | + return { |
| 228 | + candidates, |
| 229 | + }; |
| 230 | +} |
0 commit comments