Skip to content

Commit 533fddd

Browse files
committed
Adds ability to use FSC args in analyzer if available
1 parent 1ec1d52 commit 533fddd

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

.config/dotnet-tools.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@
66
"version": "20.0.0",
77
"commands": [
88
"fsdocs"
9-
]
9+
],
10+
"rollForward": false
11+
},
12+
"fsharp-analyzers": {
13+
"version": "0.32.0",
14+
"commands": [
15+
"fsharp-analyzers"
16+
],
17+
"rollForward": false
1018
}
1119
}
1220
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<Import Project="..\..\src\FSharp.Analyzers.Build\buildMultitargeting\FSharp.Analyzers.Build.targets" />
4+
5+
6+
<PropertyGroup>
7+
<RunAnalyzers>true</RunAnalyzers>
8+
<FSharpAnalyzersOtherFlags>--analyzers-path "../../artifacts/bin/OptionAnalyzer/debug" -v d</FSharpAnalyzersOtherFlags>
9+
</PropertyGroup>
10+
11+
<PropertyGroup>
12+
<OutputType>Exe</OutputType>
13+
<TargetFramework>net9.0</TargetFramework>
14+
</PropertyGroup>
15+
16+
<ItemGroup>
17+
<Compile Include="Program.fs" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\OptionAnalyzer\OptionAnalyzer.fsproj" />
22+
</ItemGroup>
23+
24+
</Project>

samples/MsBuildExample/Program.fs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// For more information see https://aka.ms/fsharp-console-apps
2+
3+
let value = Some 42
4+
5+
printfn "The value is: %d" value.Value // This will cause a warning from the OptionAnalyzer
Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
11
<Project>
2+
3+
<PropertyGroup>
4+
<_AnalyzerProjectOptions>--project &quot;$(MSBuildProjectFile)&quot;</_AnalyzerProjectOptions>
5+
</PropertyGroup>
6+
27
<Target Name="_AnalyzeFSharpProject">
38
<Error Condition="$(FSharpAnalyzersOtherFlags) == ''" Text="A property FSharpAnalyzersOtherFlags should exists with all the analyzer cli arguments!" />
49
<Exec
510
ContinueOnError="true"
611
IgnoreExitCode="true"
7-
Command="dotnet fsharp-analyzers --project &quot;$(MSBuildProjectFile)&quot; $(FSharpAnalyzersOtherFlags)" />
12+
Command="dotnet fsharp-analyzers $(_AnalyzerProjectOptions) $(FSharpAnalyzersOtherFlags)" />
813
</Target>
914

1015
<Target Name="AnalyzeFSharpProject" DependsOnTargets="_AnalyzeFSharpProject" />
16+
17+
<Target Name="_SetupFSharpAnalyzerProjectOptions" BeforeTargets="CoreCompile" Condition="'$(RunAnalyzers)' == 'true'">
18+
<PropertyGroup>
19+
<!--
20+
Required for F# Targets CoreCompile to output command line arguments
21+
https://github.com/dotnet/fsharp/blob/53929f2e01281a614a15033dfaae6fb6d00bb543/src/FSharp.Build/Fsc.fs#L721-L725
22+
https://github.com/dotnet/fsharp/blob/53929f2e01281a614a15033dfaae6fb6d00bb543/src/FSharp.Build/Microsoft.FSharp.Targets#L418C19-L418C32
23+
-->
24+
<ProvideCommandLineArgs>true</ProvideCommandLineArgs>
25+
</PropertyGroup>
26+
</Target>
27+
28+
<Target Name="FsharpAnalyzerAfterBuild" DependsOnTargets="_SetupFSharpAnalyzerProjectOptions" AfterTargets="AfterBuild" Condition="'$(RunAnalyzers)' == 'true'">
29+
<Error Condition="$(FSharpAnalyzersOtherFlags) == ''" Text="A property FSharpAnalyzersOtherFlags should exists with all the analyzer cli arguments!" />
30+
<!--
31+
Question: Should we only execute this target if FscCommandLineArgs is not empty?
32+
Argument for Incremental builds may skip CoreCompile if no files changed, so we don't want to run the analyzer in that case.
33+
There may be a better way to detect if CoreCompile was skipped but this seems to work.
34+
And if someone wants to run the analyzer without FscCommandLineArgs, they can run the AnalyzeFSharpProject target directly.
35+
-->
36+
<PropertyGroup>
37+
<_AnalyzerProjectOptions Condition="'@(FscCommandLineArgs->Count())' != '0'">--fsc-args &quot;@(FscCommandLineArgs)&quot;</_AnalyzerProjectOptions>
38+
</PropertyGroup>
39+
<Exec
40+
ContinueOnError="true"
41+
IgnoreExitCode="true"
42+
Condition="'@(FscCommandLineArgs->Count())' != '0'"
43+
Command="dotnet fsharp-analyzers $(_AnalyzerProjectOptions) $(FSharpAnalyzersOtherFlags)" />
44+
45+
</Target>
1146
</Project>

0 commit comments

Comments
 (0)