Skip to content

Commit da8ad10

Browse files
authored
Merge pull request #250 from ionide/245-project-load-errors
Add logging about System.Runtime load exceptions
2 parents d1bb738 + 800747c commit da8ad10

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

src/FSharp.Analyzers.Cli/CustomLogging.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ type CustomFormatter(options: IOptionsMonitor<CustomOptions>) as this =
3838
else
3939
this.WritePrefix(textWriter, logEntry.LogLevel)
4040
textWriter.WriteLine(message)
41+
// logEntry.Formatter doesn't actually write the exception so we need to do that ourselves
42+
// https://github.com/dotnet/runtime/blob/2bcadad3045934b54672e626bbb6131f7d0a523c/src/libraries/Microsoft.Extensions.Logging.Console/src/SystemdConsoleFormatter.cs#L95-L101
43+
if not (isNull logEntry.Exception) then
44+
textWriter.WriteLine(logEntry.Exception.ToString())
4145

4246
member private x.WritePrefix(textWriter: TextWriter, logLevel: LogLevel) =
4347
if not (isNull formatterOptions.TimestampFormat) then

src/FSharp.Analyzers.Cli/Program.fs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ExitErrorCodes =
3434
| ProjectAndFscArgs = 19
3535
| InvalidScriptArguments = 20
3636
| InvalidProjectArguments = 21
37+
| UnhandledException = 22
3738

3839
type Arguments =
3940
| Project of string list
@@ -596,6 +597,19 @@ let main argv =
596597

597598
logger.LogInformation("Running in verbose mode")
598599

600+
AppDomain.CurrentDomain.UnhandledException.Add(fun args ->
601+
let ex = args.ExceptionObject :?> exn
602+
603+
match ex with
604+
| :? FileNotFoundException as fnf when fnf.FileName.StartsWith "System.Runtime" ->
605+
// https://github.com/ionide/FSharp.Analyzers.SDK/issues/245
606+
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)
607+
| _ ->
608+
logger.LogCritical(ex, "Unhandled exception:")
609+
factory.Dispose() // Flush any logs https://github.com/dotnet/extensions/issues/2395
610+
exit (int ExitErrorCodes.UnhandledException)
611+
)
612+
599613
let severityMapping =
600614
{
601615
TreatAsHint = results.GetResult(<@ Treat_As_Hint @>, []) |> Set.ofList

0 commit comments

Comments
 (0)