Skip to content

Commit 73b6cc4

Browse files
committed
Fix bug in SARIF comparison
The SARIF comparison code was comparing the index of the artifact location, which is not useful for comparison and may differ between runs of very similar queries. This adds a function to convert a SARIF result to a canonical form, which removes the index from the artifact location.
1 parent 7c233db commit 73b6cc4

File tree

1 file changed

+32
-2
lines changed

1 file changed

+32
-2
lines changed

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

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,32 @@
11
import type { Result } from "sarif";
22

3+
function toCanonicalResult(result: Result): Result {
4+
const canonicalResult = {
5+
...result,
6+
};
7+
8+
if (canonicalResult.locations) {
9+
canonicalResult.locations = canonicalResult.locations.map((location) => {
10+
const canonicalLocation = {
11+
...location,
12+
};
13+
14+
if (canonicalLocation.physicalLocation?.artifactLocation) {
15+
canonicalLocation.physicalLocation.artifactLocation = {
16+
...canonicalLocation.physicalLocation.artifactLocation,
17+
};
18+
// The index is dependent on the result of the SARIF file and usually doesn't really tell
19+
// us anything useful, so we remove it from the comparison.
20+
delete canonicalLocation.physicalLocation.artifactLocation.index;
21+
}
22+
23+
return canonicalLocation;
24+
});
25+
}
26+
27+
return canonicalResult;
28+
}
29+
330
/**
431
* Compare the alerts of two queries. Use deep equality to determine if
532
* results have been added or removed across two invocations of a query.
@@ -25,9 +52,12 @@ export function sarifDiff(fromResults: Result[], toResults: Result[]) {
2552
throw new Error("CodeQL Compare: Target query has no results.");
2653
}
2754

55+
const canonicalFromResults = fromResults.map(toCanonicalResult);
56+
const canonicalToResults = toResults.map(toCanonicalResult);
57+
2858
const results = {
29-
from: arrayDiff(fromResults, toResults),
30-
to: arrayDiff(toResults, fromResults),
59+
from: arrayDiff(canonicalFromResults, canonicalToResults),
60+
to: arrayDiff(canonicalToResults, canonicalFromResults),
3161
};
3262

3363
if (

0 commit comments

Comments
 (0)