Skip to content

Commit 11edd0b

Browse files
committed
Use bqrs diff before sarif diff
1 parent d814adc commit 11edd0b

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

extensions/ql-vscode/src/codeql-cli/cli.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,9 +1255,11 @@ export class CodeQLCliServer implements Disposable {
12551255
async bqrsDiff(
12561256
bqrsPath1: string,
12571257
bqrsPath2: string,
1258+
options?: BqrsDiffOptions,
12581259
): Promise<{
12591260
uniquePath1: string;
12601261
uniquePath2: string;
1262+
path: string;
12611263
cleanup: () => Promise<void>;
12621264
}> {
12631265
const { path, cleanup } = await dir({ unsafeCleanup: true });
@@ -1270,13 +1272,14 @@ export class CodeQLCliServer implements Disposable {
12701272
uniquePath1,
12711273
"--right",
12721274
uniquePath2,
1273-
"--retain-result-sets=''",
1275+
"--retain-result-sets",
1276+
options?.retainResultSets?.join(",") ?? "",
12741277
bqrsPath1,
12751278
bqrsPath2,
12761279
],
12771280
"Diffing bqrs files",
12781281
);
1279-
return { uniquePath1, uniquePath2, cleanup };
1282+
return { uniquePath1, uniquePath2, path, cleanup };
12801283
}
12811284

12821285
private async runInterpretCommand(
Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
import { Uri } from "vscode";
2-
import type { Log } from "sarif";
3-
import { pathExists } from "fs-extra";
4-
import { sarifParser } from "../common/sarif-parser";
2+
import { join } from "path";
53
import type { CompletedLocalQueryInfo } from "../query-results";
64
import type { DatabaseManager } from "../databases/local-databases";
75
import type { CodeQLCliServer } from "../codeql-cli/cli";
86
import type { InterpretedQueryCompareResult } from "../common/interface-types";
97

108
import { sarifDiff } from "./sarif-diff";
119

12-
async function getInterpretedResults(
13-
interpretedResultsPath: string,
14-
): Promise<Log | undefined> {
15-
if (!(await pathExists(interpretedResultsPath))) {
16-
return undefined;
17-
}
18-
19-
return await sarifParser(interpretedResultsPath);
20-
}
21-
2210
export async function compareInterpretedResults(
2311
databaseManager: DatabaseManager,
2412
cliServer: CodeQLCliServer,
@@ -34,37 +22,65 @@ export async function compareInterpretedResults(
3422
);
3523
}
3624

37-
const [fromResultSet, toResultSet, sourceLocationPrefix] = await Promise.all([
38-
getInterpretedResults(
39-
fromQuery.completedQuery.query.interpretedResultsPath,
40-
),
41-
getInterpretedResults(toQuery.completedQuery.query.interpretedResultsPath),
42-
database.getSourceLocationPrefix(cliServer),
43-
]);
25+
const { uniquePath1, uniquePath2, path, cleanup } = await cliServer.bqrsDiff(
26+
fromQuery.completedQuery.query.resultsPath,
27+
toQuery.completedQuery.query.resultsPath,
28+
{ retainResultSets: ["nodes", "edges", "subpaths"] },
29+
);
30+
try {
31+
const sarifOutput1 = join(path, "from.sarif");
32+
const sarifOutput2 = join(path, "to.sarif");
4433

45-
if (!fromResultSet || !toResultSet) {
46-
throw new Error(
47-
"Could not find interpreted results for one or both queries.",
34+
const sourceLocationPrefix =
35+
await database.getSourceLocationPrefix(cliServer);
36+
const sourceArchiveUri = database.sourceArchive;
37+
const sourceInfo =
38+
sourceArchiveUri === undefined
39+
? undefined
40+
: {
41+
sourceArchive: sourceArchiveUri.fsPath,
42+
sourceLocationPrefix,
43+
};
44+
45+
const fromResultSet = await cliServer.interpretBqrsSarif(
46+
toQuery.completedQuery.query.metadata!,
47+
uniquePath1,
48+
sarifOutput1,
49+
sourceInfo,
50+
);
51+
const toResultSet = await cliServer.interpretBqrsSarif(
52+
toQuery.completedQuery.query.metadata!,
53+
uniquePath2,
54+
sarifOutput2,
55+
sourceInfo,
4856
);
49-
}
5057

51-
const fromResults = fromResultSet.runs[0].results;
52-
const toResults = toResultSet.runs[0].results;
58+
if (!fromResultSet || !toResultSet) {
59+
throw new Error(
60+
"Could not find interpreted results for one or both queries.",
61+
);
62+
}
5363

54-
if (!fromResults) {
55-
throw new Error("No results found in the 'from' query.");
56-
}
64+
const fromResults = fromResultSet.runs[0].results;
65+
const toResults = toResultSet.runs[0].results;
5766

58-
if (!toResults) {
59-
throw new Error("No results found in the 'to' query.");
60-
}
67+
if (!fromResults) {
68+
throw new Error("CodeQL Compare: Source query has no results.");
69+
}
70+
71+
if (!toResults) {
72+
throw new Error("CodeQL Compare: Target query has no results.");
73+
}
6174

62-
const { from, to } = sarifDiff(fromResults, toResults);
75+
const { from, to } = sarifDiff(fromResults, toResults);
6376

64-
return {
65-
kind: "interpreted",
66-
sourceLocationPrefix,
67-
from,
68-
to,
69-
};
77+
return {
78+
kind: "interpreted",
79+
sourceLocationPrefix,
80+
from,
81+
to,
82+
};
83+
} finally {
84+
await cleanup();
85+
}
7086
}

extensions/ql-vscode/src/compare/sarif-diff.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ function toCanonicalResult(result: Result): Result {
9797
* 2. If the queries are 100% disjoint
9898
*/
9999
export function sarifDiff(fromResults: Result[], toResults: Result[]) {
100-
if (!fromResults.length) {
101-
throw new Error("CodeQL Compare: Source query has no results.");
102-
}
103-
104-
if (!toResults.length) {
105-
throw new Error("CodeQL Compare: Target query has no results.");
106-
}
107-
108100
const canonicalFromResults = fromResults.map(toCanonicalResult);
109101
const canonicalToResults = toResults.map(toCanonicalResult);
110102

@@ -113,13 +105,6 @@ export function sarifDiff(fromResults: Result[], toResults: Result[]) {
113105
to: arrayDiff(canonicalToResults, canonicalFromResults),
114106
};
115107

116-
if (
117-
fromResults.length === diffResults.from.length &&
118-
toResults.length === diffResults.to.length
119-
) {
120-
throw new Error("CodeQL Compare: No overlap between the selected queries.");
121-
}
122-
123108
// We don't want to return the canonical results, we want to return the original results.
124109
// We can retrieve this by finding the index of the canonical result in the canonical results
125110
// and then using that index to find the original result. This is possible because we know that

0 commit comments

Comments
 (0)