Skip to content

Commit 703e4c6

Browse files
committed
@W-17159591@ SARIF output now passes official validation
1 parent d1d032b commit 703e4c6

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

packages/code-analyzer-core/src/output-formats/sarif-output-format.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import path from 'node:path';
12
import {CodeLocation, EngineRunResults, RunResults, Violation} from "../results";
23
import * as sarif from "sarif";
34
import {Rule, SeverityLevel} from "../rules";
@@ -38,7 +39,7 @@ function toSarifRun(engineRunResults: EngineRunResults, runDir: string): sarif.R
3839
rules: rules.map(toSarifReportingDescriptor),
3940
}
4041
},
41-
results: violations.map(v => toSarifResult(v, ruleNames.indexOf(v.getRule().getName()))),
42+
results: violations.map(v => toSarifResult(v, runDir, ruleNames.indexOf(v.getRule().getName()))),
4243
invocations: [
4344
{
4445
executionSuccessful: true,
@@ -50,7 +51,7 @@ function toSarifRun(engineRunResults: EngineRunResults, runDir: string): sarif.R
5051
};
5152
}
5253

53-
function toSarifResult(violation: Violation, ruleIndex: number) : sarif.Result {
54+
function toSarifResult(violation: Violation, runDir: string, ruleIndex: number) : sarif.Result {
5455
const primaryCodeLocation = violation.getCodeLocations()[violation.getPrimaryLocationIndex()];
5556
return {
5657
ruleId: violation.getRule().getName(),
@@ -60,26 +61,35 @@ function toSarifResult(violation: Violation, ruleIndex: number) : sarif.Result {
6061

6162
// Note that sarif format has a limit of 10 elements in the locations array, so we only store
6263
// the primary location (which is what most utilities expect) here
63-
locations: [toSarifLocation(primaryCodeLocation)],
64+
locations: [toSarifLocation(primaryCodeLocation, runDir)],
6465

6566
// And then we store the full locations array in the relatedLocations field if users want to see all of them
66-
relatedLocations: violation.getCodeLocations().map(toSarifLocation)
67+
relatedLocations: violation.getCodeLocations().map(codeLoc => toSarifLocation(codeLoc, runDir))
6768
};
6869
}
6970

70-
function toSarifLocation(codeLocation: CodeLocation): sarif.Location {
71-
return {
72-
physicalLocation: {
73-
artifactLocation: {
74-
uri: codeLocation.getFile(),
75-
},
76-
region: {
77-
startLine: codeLocation.getStartLine(),
78-
startColumn: codeLocation.getStartColumn(),
79-
endLine: codeLocation.getEndLine(),
80-
endColumn: codeLocation.getEndColumn()
81-
} as sarif.Region
71+
function toSarifLocation(codeLocation: CodeLocation, runDir: string): sarif.Location {
72+
if (codeLocation.getFile()) {
73+
return {
74+
physicalLocation: {
75+
artifactLocation: {
76+
uri: path.relative(runDir, codeLocation.getFile()!),
77+
uriBaseId: runDir
78+
},
79+
region: {
80+
startLine: codeLocation.getStartLine(),
81+
startColumn: codeLocation.getStartColumn(),
82+
endLine: codeLocation.getEndLine(),
83+
endColumn: codeLocation.getEndColumn()
84+
} as sarif.Region
85+
}
8286
}
87+
} else {
88+
return {
89+
physicalLocation: {
90+
artifactLocation: {}
91+
}
92+
};
8393
}
8494
}
8595

packages/code-analyzer-core/test/output-format.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,15 @@ describe("Tests for the SARIF output format", () => {
127127
it("When results contain multiple violations , we create sarif text correctly", () => {
128128
const formattedText: string = runResults.toFormattedOutput(OutputFormat.SARIF);
129129
const expectedText: string = getContentsOfExpectedOutputFile('multipleViolations.goldfile.sarif', true, true);
130+
fs.writeFileSync(path.join(__dirname, '..', '..', '..', 'mult.txt'), expectedText);
130131
expect(formattedText).toEqual(expectedText);
131132
});
132133

133134
it("When results contain violation of type UnexpectedError, we create sarif text correctly", async () => {
134135
const resultsWithUnexpectedError: RunResults = await createResultsWithUnexpectedError();
135136
const formattedText: string = resultsWithUnexpectedError.toFormattedOutput(OutputFormat.SARIF);
136137
const expectedText: string = getContentsOfExpectedOutputFile('unexpectedEngineErrorViolation.goldfile.sarif', true, true);
138+
fs.writeFileSync(path.join(__dirname, '..', '..', '..', 'err.txt'), expectedText);
137139
expect(formattedText).toEqual(expectedText);
138140
});
139141
});

packages/code-analyzer-core/test/test-data/expectedOutputFiles/multipleViolations.goldfile.sarif

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
{
5858
"physicalLocation": {
5959
"artifactLocation": {
60-
"uri": "{{RUNDIR}}test{{PATHSEP}}config.test.ts"
60+
"uri": "test{{PATHSEP}}config.test.ts",
61+
"uriBaseId": "{{RUNDIR}}"
6162
},
6263
"region": {
6364
"startLine": 3,
@@ -72,7 +73,8 @@
7273
{
7374
"physicalLocation": {
7475
"artifactLocation": {
75-
"uri": "{{RUNDIR}}test{{PATHSEP}}config.test.ts"
76+
"uri": "test{{PATHSEP}}config.test.ts",
77+
"uriBaseId": "{{RUNDIR}}"
7678
},
7779
"region": {
7880
"startLine": 3,
@@ -95,7 +97,8 @@
9597
{
9698
"physicalLocation": {
9799
"artifactLocation": {
98-
"uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts"
100+
"uri": "test{{PATHSEP}}run.test.ts",
101+
"uriBaseId": "{{RUNDIR}}"
99102
},
100103
"region": {
101104
"startLine": 21,
@@ -110,7 +113,8 @@
110113
{
111114
"physicalLocation": {
112115
"artifactLocation": {
113-
"uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts"
116+
"uri": "test{{PATHSEP}}run.test.ts",
117+
"uriBaseId": "{{RUNDIR}}"
114118
},
115119
"region": {
116120
"startLine": 21,
@@ -133,7 +137,8 @@
133137
{
134138
"physicalLocation": {
135139
"artifactLocation": {
136-
"uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts"
140+
"uri": "test{{PATHSEP}}run.test.ts",
141+
"uriBaseId": "{{RUNDIR}}"
137142
},
138143
"region": {
139144
"startLine": 56,
@@ -146,7 +151,8 @@
146151
{
147152
"physicalLocation": {
148153
"artifactLocation": {
149-
"uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts"
154+
"uri": "test{{PATHSEP}}run.test.ts",
155+
"uriBaseId": "{{RUNDIR}}"
150156
},
151157
"region": {
152158
"startLine": 56,
@@ -198,7 +204,8 @@
198204
{
199205
"physicalLocation": {
200206
"artifactLocation": {
201-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
207+
"uri": "test{{PATHSEP}}stubs.ts",
208+
"uriBaseId": "{{RUNDIR}}"
202209
},
203210
"region": {
204211
"startLine": 76,
@@ -211,7 +218,8 @@
211218
{
212219
"physicalLocation": {
213220
"artifactLocation": {
214-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
221+
"uri": "test{{PATHSEP}}stubs.ts",
222+
"uriBaseId": "{{RUNDIR}}"
215223
},
216224
"region": {
217225
"startLine": 4,
@@ -222,7 +230,8 @@
222230
{
223231
"physicalLocation": {
224232
"artifactLocation": {
225-
"uri": "{{RUNDIR}}test{{PATHSEP}}test-helpers.ts"
233+
"uri": "test{{PATHSEP}}test-helpers.ts",
234+
"uriBaseId": "{{RUNDIR}}"
226235
},
227236
"region": {
228237
"startLine": 9,
@@ -233,7 +242,8 @@
233242
{
234243
"physicalLocation": {
235244
"artifactLocation": {
236-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
245+
"uri": "test{{PATHSEP}}stubs.ts",
246+
"uriBaseId": "{{RUNDIR}}"
237247
},
238248
"region": {
239249
"startLine": 76,
@@ -285,7 +295,8 @@
285295
{
286296
"physicalLocation": {
287297
"artifactLocation": {
288-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
298+
"uri": "test{{PATHSEP}}stubs.ts",
299+
"uriBaseId": "{{RUNDIR}}"
289300
},
290301
"region": {
291302
"startLine": 90,
@@ -300,7 +311,8 @@
300311
{
301312
"physicalLocation": {
302313
"artifactLocation": {
303-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
314+
"uri": "test{{PATHSEP}}stubs.ts",
315+
"uriBaseId": "{{RUNDIR}}"
304316
},
305317
"region": {
306318
"startLine": 20,
@@ -313,7 +325,8 @@
313325
{
314326
"physicalLocation": {
315327
"artifactLocation": {
316-
"uri": "{{RUNDIR}}test{{PATHSEP}}test-helpers.ts"
328+
"uri": "test{{PATHSEP}}test-helpers.ts",
329+
"uriBaseId": "{{RUNDIR}}"
317330
},
318331
"region": {
319332
"startLine": 5,
@@ -324,7 +337,8 @@
324337
{
325338
"physicalLocation": {
326339
"artifactLocation": {
327-
"uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts"
340+
"uri": "test{{PATHSEP}}stubs.ts",
341+
"uriBaseId": "{{RUNDIR}}"
328342
},
329343
"region": {
330344
"startLine": 90,

packages/code-analyzer-core/test/test-data/expectedOutputFiles/unexpectedEngineErrorViolation.goldfile.sarif

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,14 @@
3030
"locations": [
3131
{
3232
"physicalLocation": {
33-
"artifactLocation": {},
34-
"region": {}
33+
"artifactLocation": {}
3534
}
3635
}
3736
],
3837
"relatedLocations": [
3938
{
4039
"physicalLocation": {
41-
"artifactLocation": {},
42-
"region": {}
40+
"artifactLocation": {}
4341
}
4442
}
4543
]

0 commit comments

Comments
 (0)