diff --git a/packages/code-analyzer-core/src/output-formats/sarif-output-format.ts b/packages/code-analyzer-core/src/output-formats/sarif-output-format.ts index 2d31d925..8d0aeb1d 100644 --- a/packages/code-analyzer-core/src/output-formats/sarif-output-format.ts +++ b/packages/code-analyzer-core/src/output-formats/sarif-output-format.ts @@ -1,3 +1,4 @@ +import path from 'node:path'; import {CodeLocation, EngineRunResults, RunResults, Violation} from "../results"; import * as sarif from "sarif"; import {Rule, SeverityLevel} from "../rules"; @@ -38,7 +39,7 @@ function toSarifRun(engineRunResults: EngineRunResults, runDir: string): sarif.R rules: rules.map(toSarifReportingDescriptor), } }, - results: violations.map(v => toSarifResult(v, ruleNames.indexOf(v.getRule().getName()))), + results: violations.map(v => toSarifResult(v, runDir, ruleNames.indexOf(v.getRule().getName()))), invocations: [ { executionSuccessful: true, @@ -50,7 +51,7 @@ function toSarifRun(engineRunResults: EngineRunResults, runDir: string): sarif.R }; } -function toSarifResult(violation: Violation, ruleIndex: number) : sarif.Result { +function toSarifResult(violation: Violation, runDir: string, ruleIndex: number) : sarif.Result { const primaryCodeLocation = violation.getCodeLocations()[violation.getPrimaryLocationIndex()]; return { ruleId: violation.getRule().getName(), @@ -60,26 +61,35 @@ function toSarifResult(violation: Violation, ruleIndex: number) : sarif.Result { // Note that sarif format has a limit of 10 elements in the locations array, so we only store // the primary location (which is what most utilities expect) here - locations: [toSarifLocation(primaryCodeLocation)], + locations: [toSarifLocation(primaryCodeLocation, runDir)], // And then we store the full locations array in the relatedLocations field if users want to see all of them - relatedLocations: violation.getCodeLocations().map(toSarifLocation) + relatedLocations: violation.getCodeLocations().map(codeLoc => toSarifLocation(codeLoc, runDir)) }; } -function toSarifLocation(codeLocation: CodeLocation): sarif.Location { - return { - physicalLocation: { - artifactLocation: { - uri: codeLocation.getFile(), - }, - region: { - startLine: codeLocation.getStartLine(), - startColumn: codeLocation.getStartColumn(), - endLine: codeLocation.getEndLine(), - endColumn: codeLocation.getEndColumn() - } as sarif.Region +function toSarifLocation(codeLocation: CodeLocation, runDir: string): sarif.Location { + if (codeLocation.getFile()) { + return { + physicalLocation: { + artifactLocation: { + uri: path.relative(runDir, codeLocation.getFile()!), + uriBaseId: runDir + }, + region: { + startLine: codeLocation.getStartLine(), + startColumn: codeLocation.getStartColumn(), + endLine: codeLocation.getEndLine(), + endColumn: codeLocation.getEndColumn() + } as sarif.Region + } } + } else { + return { + physicalLocation: { + artifactLocation: {} + } + }; } } diff --git a/packages/code-analyzer-core/test/test-data/expectedOutputFiles/multipleViolations.goldfile.sarif b/packages/code-analyzer-core/test/test-data/expectedOutputFiles/multipleViolations.goldfile.sarif index ca3cf85b..7968d3ac 100644 --- a/packages/code-analyzer-core/test/test-data/expectedOutputFiles/multipleViolations.goldfile.sarif +++ b/packages/code-analyzer-core/test/test-data/expectedOutputFiles/multipleViolations.goldfile.sarif @@ -57,7 +57,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}config.test.ts" + "uri": "test{{PATHSEP}}config.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 3, @@ -72,7 +73,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}config.test.ts" + "uri": "test{{PATHSEP}}config.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 3, @@ -95,7 +97,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts" + "uri": "test{{PATHSEP}}run.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 21, @@ -110,7 +113,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts" + "uri": "test{{PATHSEP}}run.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 21, @@ -133,7 +137,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts" + "uri": "test{{PATHSEP}}run.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 56, @@ -146,7 +151,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}run.test.ts" + "uri": "test{{PATHSEP}}run.test.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 56, @@ -198,7 +204,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 76, @@ -211,7 +218,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 4, @@ -222,7 +230,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}test-helpers.ts" + "uri": "test{{PATHSEP}}test-helpers.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 9, @@ -233,7 +242,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 76, @@ -285,7 +295,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 90, @@ -300,7 +311,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 20, @@ -313,7 +325,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}test-helpers.ts" + "uri": "test{{PATHSEP}}test-helpers.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 5, @@ -324,7 +337,8 @@ { "physicalLocation": { "artifactLocation": { - "uri": "{{RUNDIR}}test{{PATHSEP}}stubs.ts" + "uri": "test{{PATHSEP}}stubs.ts", + "uriBaseId": "{{RUNDIR}}" }, "region": { "startLine": 90, diff --git a/packages/code-analyzer-core/test/test-data/expectedOutputFiles/unexpectedEngineErrorViolation.goldfile.sarif b/packages/code-analyzer-core/test/test-data/expectedOutputFiles/unexpectedEngineErrorViolation.goldfile.sarif index 284708e2..08ac7d17 100644 --- a/packages/code-analyzer-core/test/test-data/expectedOutputFiles/unexpectedEngineErrorViolation.goldfile.sarif +++ b/packages/code-analyzer-core/test/test-data/expectedOutputFiles/unexpectedEngineErrorViolation.goldfile.sarif @@ -30,16 +30,14 @@ "locations": [ { "physicalLocation": { - "artifactLocation": {}, - "region": {} + "artifactLocation": {} } } ], "relatedLocations": [ { "physicalLocation": { - "artifactLocation": {}, - "region": {} + "artifactLocation": {} } } ]