diff --git a/src/FSharp.Analyzers.Cli/CustomLogging.fs b/src/FSharp.Analyzers.Cli/CustomLogging.fs index a1552a1..e5d6d05 100644 --- a/src/FSharp.Analyzers.Cli/CustomLogging.fs +++ b/src/FSharp.Analyzers.Cli/CustomLogging.fs @@ -38,6 +38,10 @@ type CustomFormatter(options: IOptionsMonitor) as this = else this.WritePrefix(textWriter, logEntry.LogLevel) textWriter.WriteLine(message) + // logEntry.Formatter doesn't actually write the exception so we need to do that ourselves + // https://github.com/dotnet/runtime/blob/2bcadad3045934b54672e626bbb6131f7d0a523c/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs#L95-L101 + if not (isNull logEntry.Exception) then + textWriter.WriteLine(logEntry.Exception.ToString()) member private x.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) = if not (isNull formatterOptions.TimestampFormat) then diff --git a/src/FSharp.Analyzers.Cli/Program.fs b/src/FSharp.Analyzers.Cli/Program.fs index 60f5cef..5ab2ced 100644 --- a/src/FSharp.Analyzers.Cli/Program.fs +++ b/src/FSharp.Analyzers.Cli/Program.fs @@ -34,6 +34,7 @@ type ExitErrorCodes = | ProjectAndFscArgs = 19 | InvalidScriptArguments = 20 | InvalidProjectArguments = 21 + | UnhandledException = 22 type Arguments = | Project of string list @@ -596,6 +597,19 @@ let main argv = logger.LogInformation("Running in verbose mode") + AppDomain.CurrentDomain.UnhandledException.Add(fun args -> + let ex = args.ExceptionObject :?> exn + + match ex with + | :? FileNotFoundException as fnf when fnf.FileName.StartsWith "System.Runtime" -> + // https://github.com/ionide/FSharp.Analyzers.SDK/issues/245 + logger.LogCritical(ex, "FSharp.Analyzers.Cli could not find {0}. If you're using a preview version of the .NET SDK, you may need to set DOTNET_ROLL_FORWARD_TO_PRERELEASE=1 in your environment before running this tool.", fnf.FileName) + | _ -> + logger.LogCritical(ex, "Unhandled exception:") + factory.Dispose() // Flush any logs https://github.com/dotnet/extensions/issues/2395 + exit (int ExitErrorCodes.UnhandledException) + ) + let severityMapping = { TreatAsHint = results.GetResult(<@ Treat_As_Hint @>, []) |> Set.ofList