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

Close API gap where ProjectInstance ignores ProjectOptions.DirectoryCacheFactory #8746

Merged
merged 1 commit into from
May 16, 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
40 changes: 29 additions & 11 deletions src/Build.UnitTests/Definition/ProjectEvaluationContext_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,26 +130,44 @@ public void IsolatedContextShouldNotSupportBeingPassedAFileSystem()
Should.Throw<ArgumentException>(() => EvaluationContext.Create(EvaluationContext.SharingPolicy.Isolated, fileSystem));
}

[Fact]
public void EvaluationShouldUseDirectoryCache()
[Theory]
[InlineData(false)]
[InlineData(true)]
public void EvaluationShouldUseDirectoryCache(bool useProjectInstance)
{
var projectFile = _env.CreateFile("1.proj", @"<Project> <ItemGroup Condition=`Exists('1.file')`> <Compile Include='*.cs'/> </ItemGroup> </Project>".Cleanup()).Path;
var projectFile = _env.CreateFile("1.proj", @"<Project> <Import Project='1.file' Condition=`Exists('1.file')`/> <ItemGroup><Compile Include='*.cs'/></ItemGroup> </Project>".Cleanup()).Path;

var projectCollection = _env.CreateProjectCollection().Collection;
var directoryCacheFactory = new Helpers.LoggingDirectoryCacheFactory();

var project = Project.FromFile(
projectFile,
new ProjectOptions
{
ProjectCollection = projectCollection,
DirectoryCacheFactory = directoryCacheFactory,
});
int expectedEvaluationId;
if (useProjectInstance)
{
var projectInstance = ProjectInstance.FromFile(
projectFile,
new ProjectOptions
{
ProjectCollection = projectCollection,
DirectoryCacheFactory = directoryCacheFactory,
});
expectedEvaluationId = projectInstance.EvaluationId;
}
else
{
var project = Project.FromFile(
projectFile,
new ProjectOptions
{
ProjectCollection = projectCollection,
DirectoryCacheFactory = directoryCacheFactory,
});
expectedEvaluationId = project.LastEvaluationId;
}

directoryCacheFactory.DirectoryCaches.Count.ShouldBe(1);
var directoryCache = directoryCacheFactory.DirectoryCaches[0];

directoryCache.EvaluationId.ShouldBe(project.LastEvaluationId);
directoryCache.EvaluationId.ShouldBe(expectedEvaluationId);

directoryCache.ExistenceChecks.OrderBy(kvp => kvp.Key).ShouldBe(
new Dictionary<string, int>
Expand Down
13 changes: 1 addition & 12 deletions src/Build/Definition/Project.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1809,18 +1809,6 @@ internal void VerifyThrowInvalidOperationNotImported(ProjectRootElement otherXml
ErrorUtilities.VerifyThrowInvalidOperation(ReferenceEquals(Xml, otherXml), "OM_CannotModifyEvaluatedObjectInImportedFile", otherXml.Location.File);
}

/// <summary>
/// Returns <see cref="IDirectoryCache"/> as provided by the <see cref="IDirectoryCacheFactory"/> passed when creating the
/// project, specific for a given evaluation ID.
/// </summary>
/// <param name="evaluationId">The evaluation ID for which the cache is requested.</param>
/// <returns>An <see cref="IDirectoryCache"/> implementation, or null if this project has no <see cref="IDirectoryCacheFactory"/>
/// associated with it or it returned null.</returns>
internal IDirectoryCache GetDirectoryCacheForEvaluation(int evaluationId)
{
return _directoryCacheFactory?.GetDirectoryCacheForEvaluation(evaluationId);
}

/// <summary>
/// Internal project evaluation implementation.
/// </summary>
Expand Down Expand Up @@ -3737,6 +3725,7 @@ private void Reevaluate(
loggingServiceForEvaluation,
new ProjectItemFactory(Owner),
ProjectCollection,
Owner._directoryCacheFactory,
ProjectCollection.ProjectRootElementCache,
s_buildEventContext,
evaluationContext.SdkResolverService,
Expand Down
5 changes: 4 additions & 1 deletion src/Build/Evaluation/Evaluator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private Evaluator(
PropertyDictionary<ProjectPropertyInstance> environmentProperties,
IItemFactory<I, I> itemFactory,
IToolsetProvider toolsetProvider,
IDirectoryCacheFactory directoryCacheFactory,
ProjectRootElementCacheBase projectRootElementCache,
ISdkResolverService sdkResolverService,
int submissionId,
Expand Down Expand Up @@ -231,7 +232,7 @@ private Evaluator(

// If the host wishes to provide a directory cache for this evaluation, create a new EvaluationContext with the right file system.
_evaluationContext = evaluationContext;
IDirectoryCache directoryCache = project?.GetDirectoryCacheForEvaluation(_evaluationLoggingContext.BuildEventContext.EvaluationId);
IDirectoryCache directoryCache = directoryCacheFactory?.GetDirectoryCacheForEvaluation(_evaluationLoggingContext.BuildEventContext.EvaluationId);
if (directoryCache is not null)
{
IFileSystem fileSystem = new DirectoryCacheFileSystemWrapper(evaluationContext.FileSystem, directoryCache);
Expand Down Expand Up @@ -308,6 +309,7 @@ internal static void Evaluate(
ILoggingService loggingService,
IItemFactory<I, I> itemFactory,
IToolsetProvider toolsetProvider,
IDirectoryCacheFactory directoryCacheFactory,
ProjectRootElementCacheBase projectRootElementCache,
BuildEventContext buildEventContext,
ISdkResolverService sdkResolverService,
Expand All @@ -326,6 +328,7 @@ internal static void Evaluate(
environmentProperties,
itemFactory,
toolsetProvider,
directoryCacheFactory,
projectRootElementCache,
sdkResolverService,
submissionId,
Expand Down
29 changes: 20 additions & 9 deletions src/Build/Instance/ProjectInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.Build.Definition;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Evaluation.Context;
using Microsoft.Build.FileSystem;
using Microsoft.Build.Framework;
using Microsoft.Build.Internal;
using Microsoft.Build.Shared;
Expand Down Expand Up @@ -241,7 +242,7 @@ public ProjectInstance(string projectFile, IDictionary<string, string> globalPro
/// <param name="projectCollection">Project collection</param>
/// <returns>A new project instance</returns>
public ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection)
: this(projectFile, globalProperties, toolsVersion, subToolsetVersion, projectCollection, projectLoadSettings: null, evaluationContext: null, interactive: false)
: this(projectFile, globalProperties, toolsVersion, subToolsetVersion, projectCollection, projectLoadSettings: null, evaluationContext: null, directoryCacheFactory: null, interactive: false)
{
}

Expand All @@ -260,9 +261,11 @@ public ProjectInstance(string projectFile, IDictionary<string, string> globalPro
/// <param name="projectCollection">Project collection</param>
/// <param name="projectLoadSettings">Project load settings</param>
/// <param name="evaluationContext">The context to use for evaluation.</param>
/// <param name="directoryCacheFactory">The directory cache factory to use for file I/O.</param>
/// <param name="interactive">Indicates if loading the project is allowed to interact with the user.</param>
/// <returns>A new project instance</returns>
private ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings? projectLoadSettings, EvaluationContext evaluationContext, bool interactive)
private ProjectInstance(string projectFile, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection,
ProjectLoadSettings? projectLoadSettings, EvaluationContext evaluationContext, IDirectoryCacheFactory directoryCacheFactory, bool interactive)
{
ErrorUtilities.VerifyThrowArgumentLength(projectFile, nameof(projectFile));
ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(toolsVersion, nameof(toolsVersion));
Expand All @@ -279,7 +282,8 @@ private ProjectInstance(string projectFile, IDictionary<string, string> globalPr
BuildEventContext buildEventContext = new BuildEventContext(buildParameters.NodeId, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidProjectContextId, BuildEventContext.InvalidTaskId);
ProjectRootElement xml = ProjectRootElement.OpenProjectOrSolution(projectFile, globalProperties, toolsVersion, buildParameters.ProjectRootElementCache, true /*Explicitly Loaded*/);

Initialize(xml, globalProperties, toolsVersion, subToolsetVersion, 0 /* no solution version provided */, buildParameters, projectCollection.LoggingService, buildEventContext, projectLoadSettings: projectLoadSettings, evaluationContext: evaluationContext);
Initialize(xml, globalProperties, toolsVersion, subToolsetVersion, 0 /* no solution version provided */, buildParameters, projectCollection.LoggingService, buildEventContext,
projectLoadSettings: projectLoadSettings, evaluationContext: evaluationContext, directoryCacheFactory: directoryCacheFactory);
}

/// <summary>
Expand Down Expand Up @@ -327,7 +331,7 @@ public ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globa
/// <param name="projectCollection">Project collection</param>
/// <returns>A new project instance</returns>
public ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection)
: this(xml, globalProperties, toolsVersion, subToolsetVersion, projectCollection, projectLoadSettings: null, evaluationContext: null, interactive: false)
: this(xml, globalProperties, toolsVersion, subToolsetVersion, projectCollection, projectLoadSettings: null, evaluationContext: null, directoryCacheFactory: null, interactive: false)
{
}

Expand Down Expand Up @@ -399,9 +403,11 @@ public ProjectInstance(Project project, ProjectInstanceSettings settings)
/// <param name="projectCollection">Project collection</param>
/// <param name="projectLoadSettings">Project load settings</param>
/// <param name="evaluationContext">The context to use for evaluation.</param>
/// <param name="directoryCacheFactory">The directory cache factory to use for file I/O.</param>
/// <param name="interactive">Indicates if loading the project is allowed to interact with the user.</param>
/// <returns>A new project instance</returns>
private ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection, ProjectLoadSettings? projectLoadSettings, EvaluationContext evaluationContext, bool interactive)
private ProjectInstance(ProjectRootElement xml, IDictionary<string, string> globalProperties, string toolsVersion, string subToolsetVersion, ProjectCollection projectCollection,
ProjectLoadSettings? projectLoadSettings, EvaluationContext evaluationContext, IDirectoryCacheFactory directoryCacheFactory, bool interactive)
{
BuildEventContext buildEventContext = new BuildEventContext(0, BuildEventContext.InvalidTargetId, BuildEventContext.InvalidProjectContextId, BuildEventContext.InvalidTaskId);

Expand All @@ -410,7 +416,8 @@ private ProjectInstance(ProjectRootElement xml, IDictionary<string, string> glob
Interactive = interactive
};

Initialize(xml, globalProperties, toolsVersion, subToolsetVersion, 0 /* no solution version specified */, buildParameters, projectCollection.LoggingService, buildEventContext, projectLoadSettings: projectLoadSettings, evaluationContext: evaluationContext);
Initialize(xml, globalProperties, toolsVersion, subToolsetVersion, 0 /* no solution version specified */, buildParameters, projectCollection.LoggingService, buildEventContext,
projectLoadSettings: projectLoadSettings, evaluationContext: evaluationContext, directoryCacheFactory: directoryCacheFactory);
}

/// <summary>
Expand Down Expand Up @@ -755,6 +762,7 @@ public static ProjectInstance FromFile(string file, ProjectOptions options)
options.ProjectCollection ?? ProjectCollection.GlobalProjectCollection,
options.LoadSettings,
options.EvaluationContext,
options.DirectoryCacheFactory,
options.Interactive);
}

Expand All @@ -773,6 +781,7 @@ public static ProjectInstance FromProjectRootElement(ProjectRootElement rootElem
options.ProjectCollection ?? ProjectCollection.GlobalProjectCollection,
options.LoadSettings,
options.EvaluationContext,
options.DirectoryCacheFactory,
options.Interactive);
}

Expand Down Expand Up @@ -2702,7 +2711,8 @@ private void Initialize(
ISdkResolverService sdkResolverService = null,
int submissionId = BuildEventContext.InvalidSubmissionId,
ProjectLoadSettings? projectLoadSettings = null,
EvaluationContext evaluationContext = null)
EvaluationContext evaluationContext = null,
IDirectoryCacheFactory directoryCacheFactory = null)
{
ErrorUtilities.VerifyThrowArgumentNull(xml, nameof(xml));
ErrorUtilities.VerifyThrowArgumentLengthIfNotNull(explicitToolsVersion, "toolsVersion");
Expand Down Expand Up @@ -2792,15 +2802,16 @@ private void Initialize(
evaluationContext = evaluationContext?.ContextForNewProject() ?? EvaluationContext.Create(EvaluationContext.SharingPolicy.Isolated);

Evaluator<ProjectPropertyInstance, ProjectItemInstance, ProjectMetadataInstance, ProjectItemDefinitionInstance>.Evaluate(
this,
null,
data: this,
project: null,
xml,
projectLoadSettings ?? buildParameters.ProjectLoadSettings, /* Use override ProjectLoadSettings if specified */
buildParameters.MaxNodeCount,
buildParameters.EnvironmentPropertiesInternal,
loggingService,
new ProjectItemInstanceFactory(this),
buildParameters.ToolsetProvider,
directoryCacheFactory,
ProjectRootElementCache,
buildEventContext,
sdkResolverService ?? evaluationContext.SdkResolverService, /* Use override ISdkResolverService if specified */
Expand Down