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

Handle changes to ExcludeFromCurrentConfigurationProperty #9287

Merged
merged 1 commit 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
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ private void ApplyChangesToContext(IWorkspaceProjectContext context, IProjectCha

foreach (string includePath in difference.ChangedItems)
{
UpdateInContextIfPresent(context, includePath, previousMetadata, currentMetadata, isActiveContext, logger);

// TODO: Check for changes in the metadata indicating if we should ignore the file
// in the current configuration.
HandleEvaluationMetadataChange(context, includePath, previousMetadata, currentMetadata, isActiveContext, logger);
Copy link
Contributor Author

@tmeschter tmeschter Oct 17, 2023

Choose a reason for hiding this comment

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

Note that UpdateInContextIfPresent has been renamed to HandleEvaluationMetadataChange to reflect that it does more than update already-present items.

}
}

Expand Down Expand Up @@ -223,16 +220,38 @@ private void AddToContextIfNotPresent(IWorkspaceProjectContext context, string i
}
}

private void UpdateInContextIfPresent(IWorkspaceProjectContext context, string includePath, IImmutableDictionary<string, IImmutableDictionary<string, string>> previousMetadata, IImmutableDictionary<string, IImmutableDictionary<string, string>> currentMetadata, bool isActiveContext, IManagedProjectDiagnosticOutputService logger)
/// <remarks>
/// This should only be called for evaluation changes. The items we get from design-time builds represent
/// command line arguments and won't have metadata.
/// </remarks>
private void HandleEvaluationMetadataChange(IWorkspaceProjectContext context, string includePath, IImmutableDictionary<string, IImmutableDictionary<string, string>> previousMetadata, IImmutableDictionary<string, IImmutableDictionary<string, string>> currentMetadata, bool isActiveContext, IManagedProjectDiagnosticOutputService logger)
{
string fullPath = _project.MakeRooted(includePath);

if (_paths.Contains(fullPath))
// A change in ExcludeFromCurrentConfiguration metadata needs to be processed as an add or remove rather
// than an update, so check for that first.
bool previouslyIncluded = IsItemInCurrentConfiguration(includePath, previousMetadata);
bool currentlyIncluded = IsItemInCurrentConfiguration(includePath, currentMetadata);

if (previouslyIncluded && !currentlyIncluded)
{
RemoveFromContextIfPresent(context, includePath, logger);
}
else if (!previouslyIncluded && currentlyIncluded)
{
IImmutableDictionary<string, string> previousItemMetadata = previousMetadata.GetValueOrDefault(includePath, ImmutableStringDictionary<string>.EmptyOrdinal);
IImmutableDictionary<string, string> currentItemMetadata = currentMetadata.GetValueOrDefault(includePath, ImmutableStringDictionary<string>.EmptyOrdinal);
AddToContextIfNotPresent(context, includePath, currentMetadata, isActiveContext, logger);
}
else
{
// No change to ExcludeFromCurrentConfiguration; handle as an update.

string fullPath = _project.MakeRooted(includePath);

UpdateInContext(context, fullPath, previousItemMetadata, currentItemMetadata, isActiveContext, logger);
if (_paths.Contains(fullPath))
{
IImmutableDictionary<string, string> previousItemMetadata = previousMetadata.GetValueOrDefault(includePath, ImmutableStringDictionary<string>.EmptyOrdinal);
IImmutableDictionary<string, string> currentItemMetadata = currentMetadata.GetValueOrDefault(includePath, ImmutableStringDictionary<string>.EmptyOrdinal);

UpdateInContext(context, fullPath, previousItemMetadata, currentItemMetadata, isActiveContext, logger);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ public void AddEvaluationChanges_CanAddItemWithMetadata()
var difference = IProjectChangeDiffFactory.WithAddedItems("A.cs");
var metadata = MetadataFactory.Create("A.cs", ("Name", "Value"));

ApplyProjectEvaluation(context, handler, 1, difference, metadata);
ApplyProjectEvaluation(context, handler, 1, difference, metadata: metadata);

var result = handler.Files[@"C:\Project\A.cs"];

Expand All @@ -117,7 +117,7 @@ public void AddEvaluationChanges_ItemsWithExclusionMetadataAreIgnored()
.Add("B.cs", ("ExcludeFromCurrentConfiguration", "false"));


ApplyProjectEvaluation(context, handler, 1, difference, metadata);
ApplyProjectEvaluation(context, handler, 1, difference, metadata: metadata);

string[] expectedFiles = new[] { @"C:\Project\B.cs", @"C:\Project\C.cs" };
Assert.Equal(expectedFiles.OrderBy(f => f), handler.FileNames.OrderBy(f => f));
Expand Down Expand Up @@ -311,7 +311,7 @@ public void ApplyProjectEvaluationChanges_WithExistingEvaluationChanges_CanAddCh
var difference = IProjectChangeDiffFactory.WithChangedItems(file);
var metadata = MetadataFactory.Create(file, ("Name", "Value"));

ApplyProjectEvaluation(context, handler, 2, difference, metadata);
ApplyProjectEvaluation(context, handler, 2, difference, metadata: metadata);

var result = handler.Files[@"C:\Project\A.cs"];

Expand Down Expand Up @@ -372,10 +372,56 @@ public void ApplyProjectBuild_WhenOlderEvaluationChangesWithRemovedConflict_Desi
Assert.Single(handler.FileNames, @"C:\Project\Source.cs");
}

private static void ApplyProjectEvaluation(IWorkspaceProjectContext context, AbstractEvaluationCommandLineHandler handler, IComparable version, IProjectChangeDiff difference, IImmutableDictionary<string, IImmutableDictionary<string, string>>? metadata = null)
[Fact]
public void ApplyProjectEvaluation_ChangingExclusionMetadata_IncludesFile()
{
var handler = CreateInstance(@"C:\Project\Project.csproj");
var context = IWorkspaceProjectContextMockFactory.Create();

var metadata = MetadataFactory.Create("Source.cs", ("ExcludeFromCurrentConfiguration", "true"));

ApplyProjectEvaluation(context, handler, version: 0, IProjectChangeDiffFactory.WithAddedItems("Source.cs"), metadata: metadata);

Assert.Empty(handler.FileNames);

var previousMetadata = metadata;
metadata = MetadataFactory.Create("Source.cs", ("ExcludeFromCurrentConfiguration", "false"));

ApplyProjectEvaluation(context, handler, version: 1, IProjectChangeDiffFactory.WithChangedItems("Source.cs"), previousMetadata, metadata);

Assert.Single(handler.FileNames, @"C:\Project\Source.cs");
}

[Fact]
public void ApplyProjectEvaluation_ChangingExclusionMetadata_ExcludesFile()
{
var handler = CreateInstance(@"C:\Project\Project.csproj");
var context = IWorkspaceProjectContextMockFactory.Create();

var metadata = MetadataFactory.Create("Source.cs", ("ExcludeFromCurrentConfiguration", "false"));

ApplyProjectEvaluation(context, handler, version: 0, IProjectChangeDiffFactory.WithAddedItems("Source.cs"), metadata: metadata);

Assert.Single(handler.FileNames, @"C:\Project\Source.cs");

var previousMetadata = metadata;
metadata = MetadataFactory.Create("Source.cs", ("ExcludeFromCurrentConfiguration", "true"));

ApplyProjectEvaluation(context, handler, version: 1, IProjectChangeDiffFactory.WithChangedItems("Source.cs"), previousMetadata, metadata);

Assert.Empty(handler.FileNames);
}

private static void ApplyProjectEvaluation(
IWorkspaceProjectContext context,
AbstractEvaluationCommandLineHandler handler,
IComparable version,
IProjectChangeDiff difference,
IImmutableDictionary<string, IImmutableDictionary<string, string>>? previousMetadata = null,
IImmutableDictionary<string, IImmutableDictionary<string, string>>? metadata = null)
{
metadata ??= ImmutableDictionary<string, IImmutableDictionary<string, string>>.Empty;
var previousMetadata = ImmutableDictionary<string, IImmutableDictionary<string, string>>.Empty;
previousMetadata ??= ImmutableDictionary<string, IImmutableDictionary<string, string>>.Empty;
bool isActiveContext = true;
var logger = IManagedProjectDiagnosticOutputServiceFactory.Create();

Expand Down