Skip to content

WIP: Add Sarif output support to FSharpLint.Console #702

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Numpsy
Copy link
Contributor

@Numpsy Numpsy commented Mar 10, 2024

refs #554

I've recently been having a go at running some things at work through the FSharp.Analyzers.SDK analysis tools and pushing the generated Sarif report files into DevOps CI builds, and I thought it might be useful to see if I could get FSharpLint results pushed out in the same way - so, this is a first attempt at adding Sarif report generation into FSharpLint.Console.

Note: The Sarif writer code is currently a lightly modified version of the report code from https://github.com/ionide/FSharp.Analyzers.SDK just to try to get it working, so if the code style and such is wrong that's the reason.

@Numpsy
Copy link
Contributor Author

Numpsy commented Mar 10, 2024

It might also be possible to make it generate a sarif report from the SelfCheck runs in CI builds and push that into the Github code analysis integration, e.g. as done in FsAutoComplete:
https://github.com/fsharp/FsAutoComplete/blob/7d2d2ec851392fb7ed08b520a1be7782f24eb3be/.github/workflows/build.yml#L127

@Numpsy
Copy link
Contributor Author

Numpsy commented Jul 2, 2025

Rebased on top of the latest code (The Paket changes are gone now, so previous comments about that don't apply any more

Copy link
Collaborator

@xperiandri xperiandri left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, use object initializers, please

Comment on lines +3 to +8
open FSharpLint.Framework
open System.IO
open System
open Microsoft.CodeAnalysis.Sarif
open Microsoft.CodeAnalysis.Sarif.Writers
open FSharpLint.Console.Output
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
open FSharpLint.Framework
open System.IO
open System
open Microsoft.CodeAnalysis.Sarif
open Microsoft.CodeAnalysis.Sarif.Writers
open FSharpLint.Console.Output
open System
open System.IO
open Microsoft.CodeAnalysis.Sarif
open Microsoft.CodeAnalysis.Sarif.Writers
open FSharpLint.Framework
open FSharpLint.Console.Output

Comment on lines +23 to +30
let driver = ToolComponent()
driver.Name <- "FSharpLint.Console"
driver.InformationUri <- Uri("https://fsprojects.github.io/FSharpLint/")
driver.Version <- string<Version> (System.Reflection.Assembly.GetExecutingAssembly().GetName().Version)
let tool = Tool()
tool.Driver <- driver
let run = Run()
run.Tool <- tool
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let driver = ToolComponent()
driver.Name <- "FSharpLint.Console"
driver.InformationUri <- Uri("https://fsprojects.github.io/FSharpLint/")
driver.Version <- string<Version> (System.Reflection.Assembly.GetExecutingAssembly().GetName().Version)
let tool = Tool()
tool.Driver <- driver
let run = Run()
run.Tool <- tool
let driver =
ToolComponent(
Name = "FSharpLint.Console",
InformationUri = Uri("https://fsprojects.github.io/FSharpLint/"),
Version = string<Version> (System.Reflection.Assembly.GetExecutingAssembly().GetName().Version),
)
let tool = Tool(Driver = driver)
let run = Run(Tool = tool)

Comment on lines +46 to +48
let reportDescriptor = ReportingDescriptor()
reportDescriptor.Id <- analyzerResult.RuleIdentifier
reportDescriptor.Name <- analyzerResult.RuleName
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let reportDescriptor = ReportingDescriptor()
reportDescriptor.Id <- analyzerResult.RuleIdentifier
reportDescriptor.Name <- analyzerResult.RuleName
let reportDescriptor = ReportingDescriptor(
Id = analyzerResult.RuleIdentifier,
Name = analyzerResult.RuleName
)

Comment on lines +61 to +62
let result = Result()
result.RuleId <- reportDescriptor.Id
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let result = Result()
result.RuleId <- reportDescriptor.Id
let result = Result(RuleId = reportDescriptor.Id)

Comment on lines +74 to +76
let msg = Message()
msg.Text <- analyzerResult.Details.Message
result.Message <- msg
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let msg = Message()
msg.Text <- analyzerResult.Details.Message
result.Message <- msg
let msg = Message(
Text = analyzerResult.Details.Message,
Message = msg
)

Comment on lines +78 to +91
let physicalLocation = PhysicalLocation()

physicalLocation.ArtifactLocation <-
let al = ArtifactLocation()
al.Uri <- codeRoot.MakeRelativeUri(Uri(analyzerResult.Details.Range.FileName))
al

physicalLocation.Region <-
let r = Region()
r.StartLine <- analyzerResult.Details.Range.StartLine
r.StartColumn <- analyzerResult.Details.Range.StartColumn + 1
r.EndLine <- analyzerResult.Details.Range.EndLine
r.EndColumn <- analyzerResult.Details.Range.EndColumn + 1
r
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let physicalLocation = PhysicalLocation()
physicalLocation.ArtifactLocation <-
let al = ArtifactLocation()
al.Uri <- codeRoot.MakeRelativeUri(Uri(analyzerResult.Details.Range.FileName))
al
physicalLocation.Region <-
let r = Region()
r.StartLine <- analyzerResult.Details.Range.StartLine
r.StartColumn <- analyzerResult.Details.Range.StartColumn + 1
r.EndLine <- analyzerResult.Details.Range.EndLine
r.EndColumn <- analyzerResult.Details.Range.EndColumn + 1
r
let physicalLocation = PhysicalLocation(
ArtifactLocation = ArtifactLocation(Uri = codeRoot.MakeRelativeUri(Uri(analyzerResult.Details.Range.FileName))),
Region = Region(
StartLine = analyzerResult.Details.Range.StartLine,
StartColumn = (analyzerResult.Details.Range.StartColumn + 1),
EndLine = analyzerResult.Details.Range.EndLine,
EndColumn = (analyzerResult.Details.Range.EndColumn + 1),
)

Comment on lines +93 to +94
let location: Location = Location()
location.PhysicalLocation <- physicalLocation
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let location: Location = Location()
location.PhysicalLocation <- physicalLocation
let location: Location = Location(PhysicalLocation = physicalLocation)

@Numpsy
Copy link
Contributor Author

Numpsy commented Jul 9, 2025

Rebased to fix conflicts, haven't got to the other comments yet.

@xperiandri
Copy link
Collaborator

Resolves #554

This is using the Microsoft Sarif.Sdk to write Sarif files.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants