Skip to content

Commit

Permalink
better logging and diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Apr 21, 2024
1 parent f99e8e9 commit 9ab3cd2
Showing 1 changed file with 49 additions and 36 deletions.
85 changes: 49 additions & 36 deletions src/Ionide.ProjInfo/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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<string, string>, 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

Expand All @@ -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

Expand All @@ -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<string, string>) (incomingProject: Project) =
if
Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 9ab3cd2

Please sign in to comment.