Skip to content

Commit 575cb83

Browse files
committed
Add detailed exit error codes for improved error handling in the CLI
1 parent 73962eb commit 575cb83

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/FSharp.Analyzers.Cli/Program.fs

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,22 @@ open FSharp.Analyzers.Cli.CustomLogging
1717

1818

1919
type ExitErrorCodes =
20+
| Success = 0
21+
| AnalyzerFoundError = -2
22+
| FailedAssemblyLoading = -3
23+
| AnalysisAborted = -4
24+
| FailedToLoadProject = 10
25+
| EmptyFscArgs = 11
26+
| MissingPropertyValue = 12
27+
| RuntimeAndOsOptions = 13
28+
| RuntimeAndArchOptions = 14
29+
| UnknownLoggerVerbosity = 15
30+
| AnalyzerListedMultipleTimesInTreatAsSeverity = 16
31+
| FscArgsCombinedWithMsBuildProperties = 17
32+
| FSharpCoreAssemblyLoadFailed = 18
33+
| ProjectAndFscArgs = 19
2034
| InvalidGlob = 20
35+
| InvalidProjectArguments = 21
2136

2237
type Arguments =
2338
| Project of string list
@@ -159,7 +174,7 @@ let loadProjects toolsPath properties (projPaths: string list) =
159174

160175
if Seq.length failedLoads > 0 then
161176
logger.LogError("Failed to load project '{0}'", failedLoads)
162-
exit 1
177+
exit (int ExitErrorCodes.FailedToLoadProject)
163178

164179
let loaded =
165180
FCS.mapManyOptions projectOptions
@@ -241,7 +256,7 @@ let runFscArgs
241256
=
242257
if String.IsNullOrWhiteSpace fscArgs then
243258
logger.LogError("Empty --fsc-args were passed!")
244-
exit 1
259+
exit (int ExitErrorCodes.EmptyFscArgs)
245260
else
246261

247262
let fscArgs = fscArgs.Split(';', StringSplitOptions.RemoveEmptyEntries)
@@ -486,7 +501,7 @@ let expandMultiProperties (properties: (string * string) list) =
486501
match pair with
487502
| [| k; v |] when String.IsNullOrWhiteSpace(v) ->
488503
logger.LogError("Missing property value for '{0}'", k)
489-
exit 1
504+
exit (int ExitErrorCodes.MissingPropertyValue)
490505
| [| k; v |] -> yield (k, v)
491506
| _ -> ()
492507

@@ -498,10 +513,10 @@ let validateRuntimeOsArchCombination (runtime, arch, os) =
498513
match runtime, os, arch with
499514
| Some _, Some _, _ ->
500515
logger.LogError("Specifying both the `-r|--runtime` and `-os` options is not supported.")
501-
exit 1
516+
exit (int ExitErrorCodes.RuntimeAndOsOptions)
502517
| Some _, _, Some _ ->
503518
logger.LogError("Specifying both the `-r|--runtime` and `-a|--arch` options is not supported.")
504-
exit 1
519+
exit (int ExitErrorCodes.RuntimeAndArchOptions)
505520
| _ -> ()
506521

507522
let getProperties (results: ParseResults<Arguments>) =
@@ -561,7 +576,7 @@ let main argv =
561576
use factory = LoggerFactory.Create(fun b -> b.AddConsole() |> ignore)
562577
let logger = factory.CreateLogger("")
563578
logger.LogError("unknown verbosity level given {0}", x)
564-
exit 1
579+
exit (int ExitErrorCodes.UnknownLoggerVerbosity)
565580

566581
use factory =
567582
LoggerFactory.Create(fun builder ->
@@ -595,7 +610,7 @@ let main argv =
595610
if not (severityMapping.IsValid()) then
596611
logger.LogError("An analyzer code may only be listed once in the <treat-as-severity> arguments.")
597612

598-
exit 1
613+
exit (int ExitErrorCodes.AnalyzerListedMultipleTimesInTreatAsSeverity)
599614

600615
let projOpts = results.GetResults <@ Project @> |> List.concat
601616
let fscArgs = results.TryGetResult <@ FSC_Args @>
@@ -639,7 +654,7 @@ let main argv =
639654

640655
if Option.isSome fscArgs && not properties.IsEmpty then
641656
logger.LogError("fsc-args can't be combined with MSBuild properties.")
642-
exit 1
657+
exit (int ExitErrorCodes.FscArgsCombinedWithMsBuildProperties)
643658

644659
properties
645660
|> List.iter (fun (k, v) -> logger.LogInformation("Property {0}={1}", k, v))
@@ -698,7 +713,7 @@ let main argv =
698713
"""
699714

700715
logger.LogError(msg)
701-
exit 1
716+
exit (int ExitErrorCodes.FSharpCoreAssemblyLoadFailed)
702717
)
703718

704719
let client = Client<CliAnalyzerAttribute, CliContext>(logger)
@@ -725,7 +740,7 @@ let main argv =
725740
// None
726741
| Some _ when projOpts |> List.isEmpty |> not ->
727742
logger.LogError("`--project` and `--fsc-args` cannot be combined.")
728-
exit 1
743+
exit (int ExitErrorCodes.ProjectAndFscArgs)
729744
| Some fscArgs ->
730745
runFscArgs client fscArgs exclInclFiles severityMapping
731746
|> Async.RunSynchronously
@@ -745,7 +760,7 @@ let main argv =
745760
for projPath in projects do
746761
if not (File.Exists(projPath)) then
747762
logger.LogError("Invalid `--project` argument. File does not exist: '{projPath}'", projPath)
748-
exit 1
763+
exit (int ExitErrorCodes.InvalidProjectArguments)
749764
async {
750765
let! loadedProjects = loadProjects toolsPath properties projects
751766

@@ -797,8 +812,8 @@ let main argv =
797812
failedAssemblies
798813
)
799814

800-
exit -3
815+
exit (int ExitErrorCodes.FailedAssemblyLoading)
801816

802-
if check then -2
803-
elif hasError then -4
804-
else 0
817+
if check then (int ExitErrorCodes.AnalyzerFoundError)
818+
elif hasError then (int ExitErrorCodes.AnalysisAborted)
819+
else (int ExitErrorCodes.Success)

0 commit comments

Comments
 (0)