From 9ab3cd228ac00b0a09079b4e2434eac6e6e31cce Mon Sep 17 00:00:00 2001 From: Chet Husk Date: Sun, 21 Apr 2024 11:44:56 -0500 Subject: [PATCH] better logging and diagnostics --- src/Ionide.ProjInfo/Library.fs | 85 ++++++++++++++++++++-------------- 1 file changed, 49 insertions(+), 36 deletions(-) diff --git a/src/Ionide.ProjInfo/Library.fs b/src/Ionide.ProjInfo/Library.fs index 6a3ff740..da9b1a52 100644 --- a/src/Ionide.ProjInfo/Library.fs +++ b/src/Ionide.ProjInfo/Library.fs @@ -331,10 +331,10 @@ module ProjectLoader = type LoadedProject = internal LoadedProject of ProjectInstance - let internal msBuildLogger = lazy (LogProvider.getLoggerByName "MsBuild") //lazy because dotnet test wont pickup our logger otherwise + let internal projectLoaderLogger = LogProvider.getLoggerByName "ProjectLoader" let msBuildToLogProvider () = - let msBuildLogger = msBuildLogger.Value + let msBuildLogger = (LogProvider.getLoggerByName "MsBuild") //lazy because dotnet test wont pickup our logger otherwise { new ILogger with member this.Initialize(eventSource: IEventSource) : unit = @@ -393,26 +393,18 @@ module ProjectLoader = combined - // it's _super_ important that the 'same' project (path + properties) is only created once in a project collection, so we have to check on this here - let findOrCreateMatchingProject path (collection: ProjectCollection) globalProps = - let createNewProject properties = - try - Project( - projectFile = path, - projectCollection = collection, - globalProperties = properties, - toolsVersion = null, - loadSettings = - (ProjectLoadSettings.IgnoreMissingImports - ||| ProjectLoadSettings.IgnoreInvalidImports) - ) - with :? System.InvalidOperationException as ex -> + type ProjectAlreadyLoaded(projectPath: string, collection: ProjectCollection, properties: IDictionary, innerException) = + inherit System.Exception("", innerException) + let mutable _message = null + override this.Message = + match _message with + | null -> // if the project is already loaded throw a nicer message let message = System.Text.StringBuilder() message - .AppendLine("The project '{path}' already exists in the project collection with the same global properties.") + .AppendLine($"The project '{projectPath}' already exists in the project collection with the same global properties.") .AppendLine("The global properties requested were:") |> ignore @@ -426,7 +418,7 @@ module ProjectLoader = message.AppendLine("There are projects of the following properties already in the collection:") |> ignore - for project in collection.GetLoadedProjects(path) do + for project in collection.GetLoadedProjects(projectPath) do message.AppendLine($"Evaluation #{project.LastEvaluationId}") |> ignore @@ -437,7 +429,26 @@ module ProjectLoader = message.AppendLine() |> ignore - failwith (message.ToString()) + _message <- message.ToString() + | _ -> () + + _message + + // it's _super_ important that the 'same' project (path + properties) is only created once in a project collection, so we have to check on this here + let findOrCreateMatchingProject path (collection: ProjectCollection) globalProps = + let createNewProject properties = + try + Project( + projectFile = path, + projectCollection = collection, + globalProperties = properties, + toolsVersion = null, + loadSettings = + (ProjectLoadSettings.IgnoreMissingImports + ||| ProjectLoadSettings.IgnoreInvalidImports) + ) + with :? System.InvalidOperationException as ex -> + raise (ProjectAlreadyLoaded(path, collection, properties, ex)) let hasSameGlobalProperties (globalProps: IDictionary) (incomingProject: Project) = if @@ -626,6 +637,12 @@ module ProjectLoader = else Error(sw.ToString()) with exc -> + projectLoaderLogger.error ( + Log.setMessage "Generic error while loading project {path}" + >> Log.addExn exc + >> Log.addContextDestructured "path" path + ) + Error(exc.Message) let getFscArgs (LoadedProject project) = @@ -1309,24 +1326,20 @@ type WorkspaceLoader private (toolsPath: ToolsPath, ?globalProperties: (string * match mappedProjectInfo with | Ok project -> - try - cache.Add(p, project) - - let lst = - project.ReferencedProjects - |> Seq.choose (fun n -> - if cache.ContainsKey n.ProjectFileName then - None - else - Some n.ProjectFileName - ) - |> Seq.toList + cache.Add(p, project) + + let lst = + project.ReferencedProjects + |> Seq.choose (fun n -> + if cache.ContainsKey n.ProjectFileName then + None + else + Some n.ProjectFileName + ) + |> Seq.toList - let info = Some project - lst, info - with exc -> - loadingNotification.Trigger(WorkspaceProjectState.Failed(p, GenericError(p, exc.Message))) - [], None + let info = Some project + lst, info | Error msg -> loadingNotification.Trigger(WorkspaceProjectState.Failed(p, GenericError(p, msg))) [], None