Skip to content
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
82 changes: 82 additions & 0 deletions src/Build.UnitTests/ProjectCache/ProjectCacheTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ public override async Task<CacheResult> GetCacheResultAsync(

logger.LogMessage($"MockCache: GetCacheResultAsync for {buildRequest.ProjectFullPath}", MessageImportance.High);

buildRequest.ProjectInstance.ShouldNotBeNull("The cache plugin expects evaluated projects.");

if (_projectQuerySleepTime is not null)
{
await Task.Delay(_projectQuerySleepTime.Value);
Expand Down Expand Up @@ -1349,6 +1351,86 @@ Task<BuildResult> BuildProjectFileAsync(int projectNumber)
}
}

[Theory]
[InlineData(false, false)]
// TODO: Reenable when this gets into the main branch.
//[InlineData(true, true)]
Comment on lines +1356 to +1357
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When what gets into which main branch?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Literal meaning. When this comment gets into the main branch, the test should be reenabled :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, so there's a fix in our main that's not in our 16.11 that means we should change this on merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, the fix is in #6400 which we deemed to risky for 16.11

public void ParallelStressTestForVsWorkaround(bool useSynchronousLogging, bool disableInprocNode)
{
var currentBuildEnvironment = BuildEnvironmentHelper.Instance;

try
{
BuildEnvironmentHelper.ResetInstance_ForUnitTestsOnly(
new BuildEnvironment(
currentBuildEnvironment.Mode,
currentBuildEnvironment.CurrentMSBuildExePath,
currentBuildEnvironment.RunningTests,
runningInVisualStudio: true,
visualStudioPath: currentBuildEnvironment.VisualStudioInstallRootDirectory));

BuildManager.ProjectCacheItems.ShouldBeEmpty();

var referenceNumbers = Enumerable.Range(2, NativeMethodsShared.GetLogicalCoreCount() * 2).ToArray();

var testData = new GraphCacheResponse(
new Dictionary<int, int[]>
{
{1, referenceNumbers}
},
referenceNumbers.ToDictionary(k => k, k => GraphCacheResponse.SuccessfulProxyTargetResult())
);

var graph = testData.CreateGraph(_env);

// Even though the assembly cache is discovered, we'll be overriding it with a descriptor based cache.
BuildManager.ProjectCacheItems.ShouldHaveSingleItem();

var cache = new InstanceMockCache(testData, TimeSpan.FromMilliseconds(50));

using var buildSession = new Helpers.BuildManagerSession(_env, new BuildParameters
{
MaxNodeCount = NativeMethodsShared.GetLogicalCoreCount(),
ProjectCacheDescriptor = ProjectCacheDescriptor.FromInstance(
cache,
entryPoints: null,
graph),
UseSynchronousLogging = useSynchronousLogging,
DisableInProcNode = disableInprocNode
});

var buildResultTasks = new List<Task<BuildResult>>();

foreach (var node in graph.ProjectNodes.Where(n => referenceNumbers.Contains(GetProjectNumber(n))))
{
var buildResultTask = buildSession.BuildProjectFileAsync(
node.ProjectInstance.FullPath,
globalProperties:
new Dictionary<string, string> { { "SolutionPath", graph.GraphRoots.First().ProjectInstance.FullPath } });

buildResultTasks.Add(buildResultTask);
}

foreach (var buildResultTask in buildResultTasks)
{
buildResultTask.Result.OverallResult.ShouldBe(BuildResultCode.Success);
}

buildSession.BuildProjectFile(
graph.GraphRoots.First().ProjectInstance.FullPath,
globalProperties:
new Dictionary<string, string> {{"SolutionPath", graph.GraphRoots.First().ProjectInstance.FullPath}})
.OverallResult.ShouldBe(BuildResultCode.Success);

cache.QueryStartStops.Count.ShouldBe(graph.ProjectNodes.Count * 2);
}
finally
{
BuildEnvironmentHelper.ResetInstance_ForUnitTestsOnly(currentBuildEnvironment);
BuildManager.ProjectCacheItems.Clear();
}
}

[Theory]
[InlineData(false, false)]
[InlineData(true, true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ async Task<CacheResult> ProcessCacheRequest(CacheRequest request)
await LateInitializePluginForVsWorkaround(request);
}

return await GetCacheResultAsync(cacheRequest.Submission.BuildRequestData);
return await GetCacheResultAsync(
new BuildRequestData(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it would make sense to check whether the ProjectInstance is set?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, added it.

request.Configuration.Project,
request.Submission.BuildRequestData.TargetNames.ToArray()));
}

static bool IsDesignTimeBuild(ProjectInstance project)
Expand Down Expand Up @@ -300,6 +303,8 @@ static bool MSBuildStringIsTrue(string msbuildString) =>

private async Task<CacheResult> GetCacheResultAsync(BuildRequestData buildRequest)
{
ErrorUtilities.VerifyThrowInternalNull(buildRequest.ProjectInstance, nameof(buildRequest.ProjectInstance));

var queryDescription = $"{buildRequest.ProjectFullPath}" +
$"\n\tTargets:[{string.Join(", ", buildRequest.TargetNames)}]" +
$"\n\tGlobal Properties: {{{string.Join(",", buildRequest.GlobalProperties.Select(kvp => $"{kvp.Name}={kvp.EvaluatedValue}"))}}}";
Expand Down
4 changes: 4 additions & 0 deletions src/Samples/ProjectCachePlugin/AssemblyMockCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

using System;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Build.Execution;
using Microsoft.Build.Experimental.ProjectCache;
using Microsoft.Build.Framework;
using Shouldly;

namespace MockCacheFromAssembly
{
Expand All @@ -33,6 +35,8 @@ public override Task<CacheResult> GetCacheResultAsync(
{
logger.LogMessage($"{nameof(AssemblyMockCache)}: GetCacheResultAsync for {buildRequest.ProjectFullPath}", MessageImportance.High);

buildRequest.ProjectInstance.ShouldNotBeNull("The cache plugin expects evaluated projects.");

ErrorFrom(nameof(GetCacheResultAsync), logger);

return Task.FromResult(CacheResult.IndicateNonCacheHit(CacheResultType.CacheNotApplicable));
Expand Down
3 changes: 3 additions & 0 deletions src/Samples/ProjectCachePlugin/ProjectCachePlugin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@
<ProjectReference Include="..\..\Build\Microsoft.Build.csproj" />
<ProjectReference Include="..\..\Framework\Microsoft.Build.Framework.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Shouldly" Version="3.0.0" />
</ItemGroup>
</Project>