Unexpected async clearing of `ProjectRootElementCache`
- Dominant language
- C#
- Stars
- 5.5k
- Forks
- 1.5k
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 141
Description
In the implementation of file-based apps (see `VirtualProjectBuildingCommand.cs` in the sdk repo for details), I have code calling MSBuild APIs with roughly the following structure:
```cs
// Implicit restore
var restoreRequest = new BuildRequestData(
CreateProjectInstance(projectCollection, addGlobalProperties: AddRestoreGlobalProperties(MSBuildArgs.RestoreGlobalProperties)),
targetsToBuild: ["Restore"],
hostServices: null,
BuildRequestDataFlags.ClearCachesAfterBuild | BuildRequestDataFlags.SkipNonexistentTargets | BuildRequestDataFlags.IgnoreMissingEmptyAndInvalidImports | BuildRequestDataFlags.FailOnUnresolvedSdk);
var restoreResult = BuildManager.DefaultBuildManager.BuildRequest(restoreRequest);
if (restoreResult.OverallResult != BuildResultCode.Success)
{
exitCode = 1;
}
// Build
var buildRequest = new BuildRequestData(
CreateProjectInstance(projectCollection),
targetsToBuild: ["Build"],
hostServices: null);
var buildResult = BuildManager.DefaultBuildManager.BuildRequest(buildRequest);
if (buildResult.OverallResult != BuildResultCode.Success)
{
exitCode = 1;
}
```
It's supposed to mimic what normal project-based msbuild implicit restore does, hence it needs the flag `ClearCachesAfterBuild`, for example.
It seems that the clearing of `ProjectRootElementCache` can happen asynchronously _after_ the `BuildRequest` call returns. That seems unexpected and is causing a flaky bug where the virtual project doesn't exist in the Build stage (see https://github.com/dotnet/sdk/issues/54819).
Could MSBuild implementation be changed so the cache clearing instead happens synchronously, i.e., be guaranteed to have happened when `BuildRequest` call returns?
Note that after we do the implicit restore with `ClearCachesAfterBuild`, we reconstruct the project instance before doing the Build stage, so if it weren't for the described race condition, the project would be guaranteed to be cached again and build of the virtual project would always work.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.