Skip to content

Commit

Permalink
Exit the commandline tool if the version of FSharp.Core cannot be loa…
Browse files Browse the repository at this point in the history
…ded.
  • Loading branch information
nojaf committed Oct 25, 2023
1 parent e31c6da commit 9e580f7
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="CliWrap" Version="3.6.4" />
<PackageVersion Include="FSharp.Core" Version="7.0.400" />
<PackageVersion Include="FSharp.Compiler.Service" Version="43.7.400" />
<PackageVersion Include="FSharp.Core" Version="[7.0.400]" />
<PackageVersion Include="FSharp.Compiler.Service" Version="[43.7.400]" />
<PackageVersion Include="Ionide.KeepAChangelog.Tasks" Version="0.1.8" PrivateAssets="all" />
<PackageVersion Include="McMaster.NETCore.Plugins" Version="1.4.0" />
<PackageVersion Include="Argu" Version="6.1.1" />
Expand Down
7 changes: 7 additions & 0 deletions docs/content/Getting Started.fsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ dotnet add package FSharp.Analyzers.SDK
paket add FSharp.Analyzers.SDK
```
The `FSharp.Analyzers.SDK` takes a dependency on [FSharp.Compiler.Service](https://www.nuget.org/packages/FSharp.Compiler.Service/), which has a strict dependency on `FSharp.Core`.
It is considered a best practice to use the correct `FSharp.Core` version and not the implicit one from the SDK.
```xml
<PackageReference Update="FSharp.Core" Version="7.0.400" />
```
## First analyzer
An [Analyzer<'TContext>](../reference/fsharp-analyzers-sdk-analyzer-1.html) is a function that takes a `Context` and returns a list of `Message`.
Expand Down
24 changes: 23 additions & 1 deletion src/FSharp.Analyzers.Cli/Program.fs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
open System
open System.IO
open System.Runtime.Loader
open FSharp.Compiler.CodeAnalysis
open FSharp.Compiler.Text
open Argu
Expand Down Expand Up @@ -55,12 +56,18 @@ let printInfo (fmt: Printf.TextWriterFormat<'a>) : 'a =
else
unbox (mkKn typeof<'a>)

let printError text arg =
let printError (text: Printf.TextWriterFormat<('a -> unit)>) (arg: 'a) : unit =
Console.ForegroundColor <- ConsoleColor.Red
printf "Error : "
printfn text arg
Console.ForegroundColor <- origForegroundColor

let printError2 (text: string) : unit =
Console.ForegroundColor <- ConsoleColor.Red
printf "Error : "
Console.WriteLine(text)
Console.ForegroundColor <- origForegroundColor

let loadProject toolsPath projPath =
async {
let loader = WorkspaceLoader.Create(toolsPath)
Expand Down Expand Up @@ -277,6 +284,21 @@ let main argv =
printInfo "%s" msg
}

AssemblyLoadContext.Default.add_Resolving (fun ctx assemblyName ->
if assemblyName.Name = "FSharp.Core" then
let msg =
$"""Could not load FSharp.Core %A{assemblyName.Version}. The expected assembly version of FSharp.Core is %A{Utils.currentFSharpCoreVersion}.
Consider adding <PackageReference Update="FSharp.Core" Version="<CorrectVersion>" /> to your .fsproj.
The correct version can be found over at https://www.nuget.org/packages/FSharp.Analyzers.SDK#dependencies-body-tab.
"""

printError2 msg
else
printError2 $"Could not load %s{assemblyName.Name} %A{assemblyName.Version}."

exit 1
)

let client =
Client<CliAnalyzerAttribute, CliContext>(logger, Set.ofList excludeAnalyzers)

Expand Down
9 changes: 9 additions & 0 deletions src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,15 @@ module Utils =
let currentFSharpAnalyzersSDKVersion =
Assembly.GetExecutingAssembly().GetName().Version

let currentFSharpCoreVersion =
let currentAssembly = Assembly.GetExecutingAssembly()
let references = currentAssembly.GetReferencedAssemblies()
let fc = references |> Array.tryFind (fun ra -> ra.Name = "FSharp.Core")

match fc with
| None -> failwith "FSharp.Core could not be found as a reference assembly of the SDK."
| Some fc -> fc.Version

let createContext
(checkProjectResults: FSharpCheckProjectResults)
(fileName: string)
Expand Down
1 change: 1 addition & 0 deletions src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ module Utils =
| SourceText of ISourceText

val currentFSharpAnalyzersSDKVersion: Version
val currentFSharpCoreVersion: Version

val createFCS: documentSource: option<string -> Async<option<ISourceText>>> -> FSharpChecker

Expand Down

0 comments on commit 9e580f7

Please sign in to comment.