Skip to content

Commit cf2e76d

Browse files
authored
Fix build not using project collection global properties (#146)
Also bring back ability to build in-memory projects
1 parent ca7e4b8 commit cf2e76d

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

src/Microsoft.Build.Utilities.ProjectCreation.UnitTests/BuildTests.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,23 @@ public void CanRestoreAndBuild()
134134
buildOutput.MessageEvents.High.ShouldContain(i => i.Message == "Building...", buildOutput.GetConsoleLog());
135135
}
136136

137+
[Fact]
138+
public void ProjectWithGlobalPropertiesUsedDuringBuild()
139+
{
140+
ProjectCollection projectCollection = new ProjectCollection(new Dictionary<string, string>
141+
{
142+
["Property1"] = "F6EBAC88A10E453B9AF8FA656A574737",
143+
});
144+
145+
ProjectCreator creator = ProjectCreator.Create(projectCollection: projectCollection)
146+
.TaskMessage("$(Property1)", MessageImportance.High)
147+
.TryBuild(out bool result, out BuildOutput buildOutput);
148+
149+
result.ShouldBeTrue(buildOutput.GetConsoleLog());
150+
151+
buildOutput.Messages.Last().ShouldBe("F6EBAC88A10E453B9AF8FA656A574737");
152+
}
153+
137154
[Fact]
138155
public void ProjectCollectionLoggersWork()
139156
{
@@ -222,12 +239,12 @@ public void ProjectCollectionLoggersWorkWithRestore()
222239
}
223240

224241
[Fact]
225-
public void ProjectWithNoPathBuildThrowsInvalidOperationException()
242+
public void ProjectWithNoPathRestoreThrowsInvalidOperationException()
226243
{
227244
InvalidOperationException exception = Should.Throw<InvalidOperationException>(() =>
228245
{
229246
ProjectCreator.Templates.LogsMessage("6E83EF78-959F-45A2-9FE3-08BAD99C0F92")
230-
.TryBuild(out bool _);
247+
.TryRestore(out bool _);
231248
});
232249

233250
exception.Message.ShouldBe("Project has not been given a path to save to.");

src/Microsoft.Build.Utilities.ProjectCreation/ProjectCreator.Build.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
//
33
// Licensed under the MIT license.
44

5+
using Microsoft.Build.Evaluation;
56
using Microsoft.Build.Execution;
67
using System;
78
using System.Collections.Generic;
@@ -491,8 +492,6 @@ private void Build(bool restore, string[]? targets, IDictionary<string, string>?
491492
{
492493
targetOutputs = null;
493494

494-
Save();
495-
496495
if (restore)
497496
{
498497
Restore(globalProperties, buildOutput, out result, out targetOutputs);
@@ -503,8 +502,21 @@ private void Build(bool restore, string[]? targets, IDictionary<string, string>?
503502
}
504503
}
505504

505+
ProjectInstance projectInstance;
506+
507+
if (globalProperties != null)
508+
{
509+
TryGetProject(out Project project, globalProperties);
510+
511+
projectInstance = project.CreateProjectInstance();
512+
}
513+
else
514+
{
515+
projectInstance = ProjectInstance;
516+
}
517+
506518
BuildResult buildResult = BuildManagerHost.Build(
507-
FullPath,
519+
projectInstance,
508520
targets!,
509521
globalProperties!,
510522
new List<Framework.ILogger>(ProjectCollection.Loggers.Concat(buildOutput.AsEnumerable())),

src/Microsoft.Build.Utilities.ProjectCreation/ProjectCreator.Project.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public Project Project
3636
{
3737
if (_project == null)
3838
{
39-
TryGetProject(out _project, _globalProperties, string.IsNullOrEmpty(RootElement.ToolsVersion) ? null : RootElement.ToolsVersion, ProjectCollection);
39+
TryGetProject(out _project, _globalProperties);
4040
}
4141

4242
_project.ReevaluateIfNecessary();
@@ -69,8 +69,8 @@ public ProjectCreator TryGetProject(
6969
project = new Project(
7070
RootElement,
7171
globalProperties,
72-
toolsVersion,
73-
projectCollection ?? ProjectCollection.GlobalProjectCollection,
72+
toolsVersion ?? (string.IsNullOrEmpty(RootElement.ToolsVersion) ? null : RootElement.ToolsVersion),
73+
projectCollection ?? ProjectCollection,
7474
projectLoadSettings);
7575

7676
return this;

0 commit comments

Comments
 (0)