Skip to content
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

Exit the commandline tool if the version of FSharp.Core cannot be loaded #127

Merged
merged 5 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres
to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed
* [Use fixed version of FCS and FSharp.Core](https://github.com/ionide/FSharp.Analyzers.SDK/pull/127) (thanks @nojaf!)

## [0.16.0] - 2023-10-16

### Added
Expand Down
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
32 changes: 21 additions & 11 deletions 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,10 +56,10 @@ let printInfo (fmt: Printf.TextWriterFormat<'a>) : 'a =
else
unbox (mkKn typeof<'a>)

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

let loadProject toolsPath projPath =
Expand Down Expand Up @@ -89,12 +90,7 @@ let runProject (client: Client<CliAnalyzerAttribute, CliContext>) toolsPath proj
let fileContent = File.ReadAllText fileName
let sourceText = SourceText.ofString fileContent

Utils.typeCheckFile
fcs
(fun s -> printError "%s" s)
option
fileName
(Utils.SourceOfSource.SourceText sourceText)
Utils.typeCheckFile fcs printError option fileName (Utils.SourceOfSource.SourceText sourceText)
|> Option.map (Utils.createContext checkProjectResults fileName sourceText)
)
|> Array.map (fun ctx ->
Expand Down Expand Up @@ -270,13 +266,28 @@ let main argv =

let logger =
{ new Logger with
member _.Error msg = printError "%s" msg
member _.Error msg = printError msg

member _.Verbose msg =
if verbose then
printInfo "%s" msg
}

AssemblyLoadContext.Default.add_Resolving (fun _ctx assemblyName ->
if assemblyName.Name <> "FSharp.Core" then
null
else

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.
"""

printError msg
exit 1
)

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

Expand All @@ -296,7 +307,6 @@ let main argv =
| Some [] ->
printError
"No project given. Use `--project PATH_TO_FSPROJ`. Pass path relative to current directory.%s"
""

None
| Some projects ->
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