Skip to content

Commit

Permalink
add an --allow-version-mismatch flag, so user can ignore the dismissi…
Browse files Browse the repository at this point in the history
…ng of running analyzers due to AnalyzerSDK version check. Keeping same defaults (skips the analyzers).
  • Loading branch information
smoothdeveloper committed Dec 16, 2023
1 parent 8277255 commit 649e783
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
7 changes: 6 additions & 1 deletion src/FSharp.Analyzers.Cli/Program.fs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type Arguments =
| [<Unique>] FSC_Args of string
| [<Unique>] Code_Root of string
| [<Unique; AltCommandLine("-v")>] Verbosity of string
| [<Unique>] Allow_Version_Mismatch

interface IArgParserTemplate with
member s.Usage =
Expand Down Expand Up @@ -59,6 +60,8 @@ type Arguments =
| FSC_Args _ -> "Pass in the raw fsc compiler arguments. Cannot be combined with the `--project` flag."
| Code_Root _ ->
"Root of the current code repository, used in the sarif report to construct the relative file path. The current working directory is used by default."
| Allow_Version_Mismatch ->
"When not set (default), the analyzers that don't match with the major and minor parts of the Analyzer SDK version will not be executed. This can be overriden using this flag."

type SeverityMappings =
{
Expand Down Expand Up @@ -495,6 +498,8 @@ let main argv =
logger.LogInformation("Treat as Warning: [{0}]", (severityMapping.TreatAsWarning |> String.concat ", "))
logger.LogInformation("Treat as Error: [{0}]", (severityMapping.TreatAsError |> String.concat ", "))

let allowAnalyzerSDKVersionMismatch = results.Contains <@ Allow_Version_Mismatch @>

if not (severityMapping.IsValid()) then
logger.LogError("An analyzer code may only be listed once in the <treat-as-severity> arguments.")

Expand Down Expand Up @@ -549,7 +554,7 @@ let main argv =
)

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

let dlls, analyzers =
((0, 0), analyzersPaths)
Expand Down
26 changes: 17 additions & 9 deletions src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.Client.fs
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ module Client =
|> Seq.toList

type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TContext :> Context>
(logger: ILogger, excludedAnalyzers: string Set)
(logger: ILogger, excludedAnalyzers: string Set, allowAnalyzerSDKVersionMismatch: bool)
=
do TASTCollecting.logger <- logger

let registeredAnalyzers =
ConcurrentDictionary<string, Client.RegisteredAnalyzer<'TContext> list>()

new() = Client(Abstractions.NullLogger.Instance, Set.empty)
new() = Client(Abstractions.NullLogger.Instance, Set.empty, false)

member x.LoadAnalyzers(dir: string) : int * int =
if Directory.Exists dir then
Expand Down Expand Up @@ -183,14 +183,22 @@ type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TC
then
true
else
logger.LogError(
"Trying to load {0} which was built using SDK version {1}. Expect {2} instead. Assembly will be skipped.",
name,
version,
Utils.currentFSharpAnalyzersSDKVersion
)
if allowAnalyzerSDKVersionMismatch then
logger.LogWarning(
"{0} was built using SDK version {1}. Running {2} instead, may cause runtime error.",
name,
version,
Utils.currentFSharpAnalyzersSDKVersion
)
else
logger.LogError(
"Trying to load {0} which was built using SDK version {1}. Running {2} instead. Assembly will be skipped.",
name,
version,
Utils.currentFSharpAnalyzersSDKVersion
)

false
allowAnalyzerSDKVersionMismatch
)
|> Array.map (fun (path, assembly) ->
let analyzers =
Expand Down
2 changes: 1 addition & 1 deletion src/FSharp.Analyzers.SDK/FSharp.Analyzers.SDK.Client.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type AnalysisResult =
}

type Client<'TAttribute, 'TContext when 'TAttribute :> AnalyzerAttribute and 'TContext :> Context> =
new: logger: ILogger * excludedAnalyzers: string Set -> Client<'TAttribute, 'TContext>
new: logger: ILogger * excludedAnalyzers: string Set * allowAnalyzerSDKVersionMismatch: bool -> Client<'TAttribute, 'TContext>
new: unit -> Client<'TAttribute, 'TContext>
/// <summary>
/// Loads into private state any analyzers defined in any assembly
Expand Down

0 comments on commit 649e783

Please sign in to comment.