Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure the built project is retained when restoring fixes #9339 #9345

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions src/MSBuild.UnitTests/XMake_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -619,30 +619,32 @@ public void InvalidMaxCPUCountSwitch4()
}

[Theory]
[InlineData("-getProperty:Foo;Bar", true, "EvalValue", false, false, false, true)]
[InlineData("-getProperty:Foo;Bar -t:Build", true, "TargetValue", false, false, false, true)]
[InlineData("-getItem:MyItem", false, "", true, false, false, true)]
[InlineData("-getItem:MyItem -t:Build", false, "", true, true, false, true)]
[InlineData("-getItem:WrongItem -t:Build", false, "", false, false, false, true)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem -t:Build", true, "TargetValue", true, true, false, true)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem", true, "EvalValue", true, false, false, true)]
[InlineData("-getProperty:Foo;Bar -getTargetResult:MyTarget", true, "TargetValue", false, false, true, true)]
[InlineData("-getProperty:Foo;Bar", true, "EvalValue", false, false, false, false)]
[InlineData("-getProperty:Foo;Bar -t:Build", true, "TargetValue", false, false, false, false)]
[InlineData("-getItem:MyItem", false, "", true, false, false, false)]
[InlineData("-getItem:MyItem -t:Build", false, "", true, true, false, false)]
[InlineData("-getItem:WrongItem -t:Build", false, "", false, false, false, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem -t:Build", true, "TargetValue", true, true, false, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem", true, "EvalValue", true, false, false, false)]
[InlineData("-getProperty:Foo;Bar -getTargetResult:MyTarget", true, "TargetValue", false, false, true, false)]
[InlineData("-getProperty:Foo;Bar", true, "EvalValue", false, false, false, true, false)]
[InlineData("-getProperty:Foo;Bar -t:Build", true, "TargetValue", false, false, false, true, false)]
[InlineData("-getItem:MyItem", false, "", true, false, false, true, false)]
[InlineData("-getItem:MyItem -t:Build", false, "", true, true, false, true, false)]
[InlineData("-getItem:WrongItem -t:Build", false, "", false, false, false, true, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem -t:Build", true, "TargetValue", true, true, false, true, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem", true, "EvalValue", true, false, false, true, false)]
[InlineData("-getProperty:Foo;Bar -getTargetResult:MyTarget", true, "TargetValue", false, false, true, true, false)]
[InlineData("-getProperty:Foo;Bar", true, "EvalValue", false, false, false, false, false)]
[InlineData("-getProperty:Foo;Bar -t:Build", true, "TargetValue", false, false, false, false, false)]
[InlineData("-getItem:MyItem", false, "", true, false, false, false, false)]
[InlineData("-getItem:MyItem -t:Build", false, "", true, true, false, false, false)]
[InlineData("-getItem:WrongItem -t:Build", false, "", false, false, false, false, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem -t:Build", true, "TargetValue", true, true, false, false, false)]
[InlineData("-getProperty:Foo;Bar -getItem:MyItem", true, "EvalValue", true, false, false, false, false)]
[InlineData("-getProperty:Foo;Bar -getTargetResult:MyTarget", true, "TargetValue", false, false, true, false, false)]
[InlineData("-getTargetResult:Restore", false, "", false, false, false, false, true)]
public void ExecuteAppWithGetPropertyItemAndTargetResult(
string extraSwitch,
bool fooPresent,
string fooResult,
bool itemIncludesAlwaysThere,
bool itemIncludesTargetItem,
bool targetResultPresent,
bool isGraphBuild)
bool isGraphBuild,
bool restoreOnly)
{
using TestEnvironment env = TestEnvironment.Create();
TransientTestFile project = env.CreateFile("testProject.csproj", @"
Expand Down Expand Up @@ -670,11 +672,15 @@ public void ExecuteAppWithGetPropertyItemAndTargetResult(

</Target>

<Target Name=""Restore"">
rainersigwald marked this conversation as resolved.
Show resolved Hide resolved

</Target>

</Project>
");
string graph = isGraphBuild ? "--graph" : "";
string results = RunnerUtilities.ExecMSBuild($" {project.Path} {extraSwitch} {graph}", out bool success);
success.ShouldBeTrue();
success.ShouldBeTrue(results);
if (fooPresent)
{
results.ShouldContain($"\"Foo\": \"{fooResult}\"");
Expand All @@ -687,7 +693,8 @@ public void ExecuteAppWithGetPropertyItemAndTargetResult(
results.Contains("targetItem").ShouldBe(itemIncludesTargetItem);

results.Contains("MyTarget").ShouldBe(targetResultPresent);
results.Contains("\"Result\": \"Success\"").ShouldBe(targetResultPresent);
results.Contains("\"Result\": \"Success\"").ShouldBe(targetResultPresent || restoreOnly);
results.ShouldNotContain(ResourceUtilities.GetResourceString("BuildFailedWithPropertiesItemsOrTargetResultsRequested"));
}

/// <summary>
Expand Down
12 changes: 9 additions & 3 deletions src/MSBuild/XMake.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1538,7 +1538,7 @@ internal static bool BuildProject(

if (enableRestore || restoreOnly)
{
result = ExecuteRestore(projectFile, toolsVersion, buildManager, restoreProperties.Count > 0 ? restoreProperties : globalProperties);
result = ExecuteRestore(projectFile, toolsVersion, buildManager, restoreProperties.Count > 0 ? restoreProperties : globalProperties, saveProjectResult: saveProjectResult);

if (result.OverallResult != BuildResultCode.Success)
{
Expand Down Expand Up @@ -1762,7 +1762,7 @@ private static GraphBuildResult ExecuteGraphBuild(BuildManager buildManager, Gra
return submission.Execute();
}

private static BuildResult ExecuteRestore(string projectFile, string toolsVersion, BuildManager buildManager, Dictionary<string, string> globalProperties)
private static BuildResult ExecuteRestore(string projectFile, string toolsVersion, BuildManager buildManager, Dictionary<string, string> globalProperties, bool saveProjectResult = false)
{
// Make a copy of the global properties
Dictionary<string, string> restoreGlobalProperties = new Dictionary<string, string>(globalProperties);
Expand All @@ -1779,13 +1779,19 @@ private static BuildResult ExecuteRestore(string projectFile, string toolsVersio
// make available an import that doesn't exist yet and the <Import /> might be missing a condition.
// - BuildRequestDataFlags.FailOnUnresolvedSdk to still fail in the case when an MSBuild project SDK can't be resolved since this is fatal and should
// fail the build.
BuildRequestDataFlags flags = BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags.FailOnUnresolvedSdk;
if (saveProjectResult)
{
flags |= BuildRequestDataFlags.ProvideProjectStateAfterBuild;
}

BuildRequestData restoreRequest = new BuildRequestData(
projectFile,
restoreGlobalProperties,
toolsVersion,
targetsToBuild: new[] { MSBuildConstants.RestoreTargetName },
hostServices: null,
flags: BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags.FailOnUnresolvedSdk);
flags: flags);

return ExecuteBuild(buildManager, restoreRequest);
}
Expand Down