Skip to content

Commit

Permalink
fix the project-uniqueness comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
baronfel committed Apr 21, 2024
1 parent 103824e commit a7d9807
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 24 deletions.
89 changes: 66 additions & 23 deletions src/Ionide.ProjInfo/Library.fs
Original file line number Diff line number Diff line change
Expand Up @@ -385,18 +385,59 @@ module ProjectLoader =
with set (v: LoggerVerbosity): unit = ()
}

let mergeGlobalProperties (collection: ProjectCollection) (otherProperties: IDictionary<string, string>) =
let combined = Dictionary(collection.GlobalProperties)

for kvp in otherProperties do
combined.Add(kvp.Key, kvp.Value)

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 =
Project(
projectFile = path,
projectCollection = collection,
globalProperties = properties,
toolsVersion = null,
loadSettings =
(ProjectLoadSettings.IgnoreMissingImports
||| ProjectLoadSettings.IgnoreInvalidImports)
)
try
Project(
projectFile = path,
projectCollection = collection,
globalProperties = properties,
toolsVersion = null,
loadSettings =
(ProjectLoadSettings.IgnoreMissingImports
||| ProjectLoadSettings.IgnoreInvalidImports)
)
with :? System.InvalidOperationException as ex ->

// 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 global properties requested were:")
|> ignore

for (KeyValue(k, v)) in properties do
message.AppendLine($" {k} = {v}")
|> ignore

message.AppendLine()
|> ignore

message.AppendLine("There are projects of the following properties already in the collection:")
|> ignore

for project in collection.GetLoadedProjects(path) do
message.AppendLine($"Evaluation #{project.LastEvaluationId}")
|> ignore

for (KeyValue(k, v)) in project.GlobalProperties do
message.AppendLine($" {k} = {v}")
|> ignore

message.AppendLine()
|> ignore

failwith (message.ToString())

let hasSameGlobalProperties (globalProps: IDictionary<string, string>) (incomingProject: Project) =
if
Expand All @@ -411,13 +452,19 @@ module ProjectLoader =
&& incomingProject.GlobalProperties.[k] = v
)

match collection.GetLoadedProjects(path) with
| null -> createNewProject globalProps
| existingProjects when existingProjects.Count = 0 -> createNewProject globalProps
| existingProjects ->
existingProjects
|> Seq.tryFind (hasSameGlobalProperties globalProps)
|> Option.defaultWith (fun _ -> createNewProject globalProps)
lock
(collection)
(fun _ ->
match collection.GetLoadedProjects(path) with
| null -> createNewProject globalProps
| existingProjects when existingProjects.Count = 0 -> createNewProject globalProps
| existingProjects ->
let totalGlobalProps = mergeGlobalProperties collection globalProps

existingProjects
|> Seq.tryFind (hasSameGlobalProperties totalGlobalProps)
|> Option.defaultWith (fun _ -> createNewProject globalProps)
)

let getTfm (pi: ProjectInstance) isLegacyFrameworkProj =
let tfm =
Expand Down Expand Up @@ -985,14 +1032,10 @@ type WorkspaceLoaderViaProjectGraph private (toolsPath, ?globalProperties: (stri
tfm
[]
(globalProperties.Keys
|> Set.ofSeq)

let combined = Dictionary(globalProperties)

for kvp in ourGlobalProperties do
combined.Add(kvp.Key, kvp.Value)
|> Set.ofSeq
|> Set.union (Set.ofSeq projectCollection.GlobalProperties.Keys))

let tfm_specific_project = ProjectLoader.findOrCreateMatchingProject projectPath projectCollection combined
let tfm_specific_project = ProjectLoader.findOrCreateMatchingProject projectPath projectCollection ourGlobalProperties
tfm_specific_project.CreateProjectInstance()

let projectGraphProjects (paths: string seq) =
Expand Down
2 changes: 1 addition & 1 deletion test/Ionide.ProjInfo.Tests/Tests.fs
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ let testLegacyFrameworkMultiProject toolsPath workspaceLoader isRelease (workspa
let testSample2 toolsPath workspaceLoader isRelease (workspaceFactory: ToolsPath * (string * string) list -> IWorkspaceLoader) =
testCase
|> withLog
(sprintf "can load sample2 - %s - isRelease is %b" workspaceLoader isRelease)
(sprintf "can load sample2 - isRelease is %b - %s" isRelease workspaceLoader)
(fun logger fs ->
let testDir = inDir fs "load_sample2"
copyDirFromAssets fs ``sample2 NetSdk library``.ProjDir testDir
Expand Down

0 comments on commit a7d9807

Please sign in to comment.