Skip to content

Commit

Permalink
Integrate analyzers via msbuild (#2968)
Browse files Browse the repository at this point in the history
* integrate analyzers via msbuild magic

* try wildcard for sarif upload, might need to change to directory

* try another wildcard for sarif files

* collect sarif files in dedicated directory to be able to upload them with github/codeql-action/upload-sarif

* add back env vars

* remove stale open statement

* update analyzers tool and gr analyzers package

* move setting of env vars

* delete "Directory.Solution.props"

* remove message

* needs some UsingTask magic I don't know currently

* fix build for now

* cool kids project cracking

* restore all fail-on-warnings

* use analysisreports dir

* use $(SolutionFileName) instead of hard coding name

* Set environment variables in target.

---------

Co-authored-by: nojaf <[email protected]>
  • Loading branch information
dawedawe and nojaf authored Oct 26, 2023
1 parent c24ecd6 commit ee06697
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 59 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
]
},
"fsharp-analyzers": {
"version": "0.16.0",
"version": "0.17.0",
"commands": [
"fsharp-analyzers"
]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
uses: github/codeql-action/upload-sarif@v2
if: matrix.os == 'ubuntu-latest'
with:
sarif_file: analysis.sarif
sarif_file: analysisreports
- name: "Documentation"
if: matrix.os == 'windows-latest' && github.ref == 'refs/heads/main'
uses: peaceiris/actions-gh-pages@v3
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,4 +201,4 @@ tests/.repositories/**

# Analyzer files
.analyzerpackages
analysis.sarif
*.sarif
6 changes: 6 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,10 @@ Some common use cases include:
<PackageReference Include="Ionide.KeepAChangelog.Tasks" Version="0.1.8" PrivateAssets="all" />
<PackageReference Include="DotNet.ReproducibleBuilds" Version="1.1.1" PrivateAssets="all" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="G-Research.FSharp.Analyzers" Version="0.1.6">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>build</IncludeAssets>
</PackageReference>
</ItemGroup>
</Project>
22 changes: 22 additions & 0 deletions Directory.Build.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project>

<Target
Name="AnalyzeProject"
DependsOnTargets="Restore;ResolveAssemblyReferencesDesignTime;ResolveProjectReferencesDesignTime;ResolvePackageDependenciesDesignTime;FindReferenceAssembliesForReferences;_GenerateCompileDependencyCache;_ComputeNonExistentFileProperty;BeforeBuild;BeforeCompile;CoreCompile">

<Message Importance="normal" Text="fsc arguments: @(FscCommandLineArgs)" />
<Message Importance="High" Text="Analyzing $(MSBuildProjectFile)"/>
<PropertyGroup>
<DOTNET_ROLL_FORWARD_TO_PRERELEASE>1</DOTNET_ROLL_FORWARD_TO_PRERELEASE>
<DOTNET_ROLL_FORWARD>LatestMajor</DOTNET_ROLL_FORWARD>
</PropertyGroup>
<Exec
ContinueOnError="true"
Command="dotnet fsharp-analyzers --fsc-args &quot;@(FscCommandLineArgs)&quot; --analyzers-path &quot;$(PkgG-Research_FSharp_Analyzers)\analyzers\dotnet\fs&quot; --exclude-analyzer PartialAppAnalyzer --fail-on-warnings GRA-STRING-001 GRA-STRING-002 GRA-STRING-003 GRA-UNIONCASE-001 --verbose --report &quot;../../analysisreports/$(MSBuildProjectName)-analysis.sarif&quot;">
<Output TaskParameter="ExitCode" PropertyName="LastExitCode" />
</Exec>
<Error Condition="'$(LastExitCode)' == '-2'" Text="Problems were found $(MSBuildProjectFile)" />

</Target>

</Project>
15 changes: 15 additions & 0 deletions Directory.Solution.targets
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project>

<ItemGroup>
<ProjectsToAnalyze Include="src\**\*.fsproj" Exclude="src\Fantomas.FCS\Fantomas.FCS.fsproj"/>
</ItemGroup>

<Target Name="AnalyzeSolution">
<Exec Command="dotnet build -c Release $(SolutionFileName)" />
<MSBuild
Projects="@(ProjectsToAnalyze)"
Targets="AnalyzeProject"
Properties="DesignTimeBuild=True;Configuration=Release;ProvideCommandLineArgs=True;SkipCompilerExecution=True" />
</Target>

</Project>
17 changes: 0 additions & 17 deletions analyzers/analyzers.fsproj

This file was deleted.

6 changes: 0 additions & 6 deletions analyzers/packages.lock.json

This file was deleted.

38 changes: 5 additions & 33 deletions build.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

open System
open System.IO
open System.Text.RegularExpressions
open Fun.Build
open CliWrap
open CliWrap.Buffered
Expand Down Expand Up @@ -58,31 +57,16 @@ let pushPackage nupkg =
return result.ExitCode
}

let analyzersProjectPath = __SOURCE_DIRECTORY__ </> "analyzers/analyzers.fsproj"

let analyzersVersion =
let s = File.ReadAllText(analyzersProjectPath)
let regex = Regex(@"\[\s*(\d+\.\d+\.\d+)\s*\]")
let matches = regex.Match(s)
matches.Groups[1].Value

let analyzeProjects (projectPaths: string seq) =
let projects = String.concat " " projectPaths
let analyzerPath =
$"./.analyzerpackages/g-research.fsharp.analyzers/{analyzersVersion}"
let failOnWarnings =
"GRA-STRING-001 GRA-STRING-002 GRA-STRING-003 GRA-UNIONCASE-001"
let excludeAnalyzers = "PartialAppAnalyzer"
let report = "./analysis.sarif"
$"dotnet fsharp-analyzers --project %s{projects} --analyzers-path \"%s{analyzerPath}\" --verbose --fail-on-warnings %s{failOnWarnings} --report %s{report} --exclude-analyzer %s{excludeAnalyzers}"
let analysisReportsDir = "analysisreports"

pipeline "Build" {
workingDir __SOURCE_DIRECTORY__
stage "RestoreTools" { run "dotnet tool restore" }
stage "Clean" {
run (
cleanFolders
[| "bin"
[| analysisReportsDir
"bin"
"src/Fantomas.FCS/bin/Release"
"src/Fantomas.FCS/obj/Release"
"src/Fantomas.Core/bin/Release"
Expand All @@ -94,22 +78,10 @@ pipeline "Build" {
)
}
stage "CheckFormat" { run "dotnet fantomas src docs build.fsx --check" }
stage "RestoreAnalyzers" { run $"dotnet restore %s{analyzersProjectPath}" }
stage "Build" { run "dotnet build -c Release" }
stage "Analyze" {
envVars
[| "DOTNET_ROLL_FORWARD_TO_PRERELEASE", "1"
"DOTNET_ROLL_FORWARD", "LatestMajor" |]
run (
analyzeProjects
[ "./src/Fantomas/Fantomas.fsproj"
"./src/Fantomas.Benchmarks/Fantomas.Benchmarks.fsproj"
"./src/Fantomas.Client/Fantomas.Client.fsproj"
"./src/Fantomas.Client.Tests/Fantomas.Client.Tests.fsproj"
"./src/Fantomas.Core/Fantomas.Core.fsproj"
"./src/Fantomas.Core.Tests/Fantomas.Core.Tests.fsproj"
"./src/Fantomas.Tests/Fantomas.Tests.fsproj" ]
)
run (fun _ -> async { System.IO.Directory.CreateDirectory(analysisReportsDir) |> ignore })
run "dotnet msbuild /t:AnalyzeSolution"
}
stage "UnitTests" { run "dotnet test -c Release" }
stage "Pack" { run "dotnet pack --no-restore -c Release -o ./bin" }
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Benchmarks/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"resolved": "6.0.4",
"contentHash": "CYqAfmO7JvN38M+ACkstS8taVfl8C0mCkvSiBAshfKuu2Nut6+8MuFU7Wahu09wGIyFPlRz5ArFWxSOM5mhMSA=="
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"BenchmarkDotNet.Annotations": {
"type": "Transitive",
"resolved": "0.13.2",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Client.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@
"resolved": "6.0.1",
"contentHash": "VrFAiW8dEEekk+0aqlbvMNZzDvYXmgWZwAt68AUBqaWK8RnoEVUNglj66bZzhs4/U63q0EfXlhcEKnH1sTYLjw=="
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Microsoft.NET.Test.Sdk": {
"type": "Direct",
"requested": "[17.5.0, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Client/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"resolved": "5.0.1",
"contentHash": "96YN8cgEVSpFhENBs+oXmxskFOQIjdW/pGCtyR+X8URYGbbQk0Fpu56uKHTOl7BEj6bSiZCuE8sjr9FdlXjoUQ=="
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Ionide.KeepAChangelog.Tasks": {
"type": "Direct",
"requested": "[0.1.8, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Core.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"NUnit": "[3.13.2, 3.14.0)"
}
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Microsoft.NET.Test.Sdk": {
"type": "Direct",
"requested": "[17.5.0, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Core/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
"resolved": "6.0.1",
"contentHash": "VrFAiW8dEEekk+0aqlbvMNZzDvYXmgWZwAt68AUBqaWK8RnoEVUNglj66bZzhs4/U63q0EfXlhcEKnH1sTYLjw=="
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Ionide.KeepAChangelog.Tasks": {
"type": "Direct",
"requested": "[0.1.8, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.FCS/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"FsLexYacc.Runtime": "11.2.0"
}
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Ionide.KeepAChangelog.Tasks": {
"type": "Direct",
"requested": "[0.1.8, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas.Tests/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@
"NUnit": "[3.13.2, 3.14.0)"
}
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Microsoft.NET.Test.Sdk": {
"type": "Direct",
"requested": "[17.5.0, )",
Expand Down
6 changes: 6 additions & 0 deletions src/Fantomas/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
"resolved": "6.0.1",
"contentHash": "VrFAiW8dEEekk+0aqlbvMNZzDvYXmgWZwAt68AUBqaWK8RnoEVUNglj66bZzhs4/U63q0EfXlhcEKnH1sTYLjw=="
},
"G-Research.FSharp.Analyzers": {
"type": "Direct",
"requested": "[0.1.6, )",
"resolved": "0.1.6",
"contentHash": "1WyLN4LsQVQUprNr5adpLcYA7Dbn1ohGQ3Pch4qoVflKWZRuFb81pDziFZN/KOqPHdlQUx2BLAE5LIKGD46o5g=="
},
"Ignore": {
"type": "Direct",
"requested": "[0.1.46, )",
Expand Down

0 comments on commit ee06697

Please sign in to comment.