Skip to content

Commit

Permalink
Merge pull request #127 from nojaf/fsharp-8-guidance
Browse files Browse the repository at this point in the history
Exit the commandline tool if the version of FSharp.Core cannot be loaded
  • Loading branch information
nojaf authored Oct 25, 2023
2 parents e31c6da + 63446db commit 31d7b8a
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 13 deletions.
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

0 comments on commit 31d7b8a

Please sign in to comment.