dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.92k stars 4.02k forks source link

Workspace.TryApplyChanges(solution) does not apply RemoveMetadataReference. #12161

Open malylemire1 opened 8 years ago

malylemire1 commented 8 years ago

Version Used: Roslyn version is 1.2.2 on vs2015

Steps to Reproduce:

  1. Get VisualStudioWorkspace in a Visual Studio Package :
IComponentModel componentModel = (IComponentModel)Package.GetGlobalService(typeof(SComponentModel));
VisualStudioWorkspace workspace = componentModel.GetService<VisualStudioWorkspace>();
  1. Remove the metadata references :
Solution solution = workspace.CurrentSolution;

foreach (ProjectId projectId in solution.ProjectIds)
{
      Project project = solution.GetProject(projectId);
      foreach (MetadataReference reference in project.MetadataReferences.Where(m => m.Display == "ReferenceFilePath").ToList())
      {
           project = project.RemoveMetadataReference(reference);
      }

      solution = project.Solution;
}

workspace.TryApplyChanges(solution);

Expected Behavior:

Changes to solution should be applied.

Actual Behavior:

solution.GetChanges(workspace.CurrentSolution).GetProjectChanges().Select(x => x.GetRemovedMetadataReferences()); Returns the actual changes before the call to TryApplyChanges.

workspace.CanApplyChange(ApplyChangesKind.RemoveMetadataReference); Return true.

Changes to solution are not applied.

malylemire1 commented 8 years ago

Just to mention that the same code with AddMetadataReference works fine.

mattwar commented 7 years ago

We are removing the reference using the VSProject API, but are not receiving callbacks on any of the VS/project-system hooks for watching imports being changed. Still investigating.