0

We are using Roslyn code to autogenerate some .cs file in an asp.net core mvc project using Visual Studio 2017 15.4

After some recent changes to the branch I am getting this error

Type: System.InvalidOperationException Message: Cannot modify an evaluated object originating in an imported file "C:\Program Files\dotnet\sdk\2.0.3\Sdks\Microsoft.NET.Sdk\build\Microsoft.NET.Sdk.DefaultItems.props". Stack: at Microsoft.Build.Shared.ErrorUtilities.ThrowInvalidOperation(StringresourceName, Object[] args) at Microsoft.Build.Shared.ErrorUtilities.VerifyThrowInvalidOperation(Boolean condition, String resourceName, Object arg0) at Microsoft.Build.Evaluation.Project.VerifyThrowInvalidOperationNotImported(ProjectRootElement otherXml) at Microsoft.Build.Evaluation.Project.RemoveItemHelper(ProjectItem item) at Microsoft.Build.Evaluation.Project.RemoveItem(ProjectItem item)
at Microsoft.CodeAnalysis.MSBuild.ProjectFile.RemoveDocument(String filePath)

at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.ApplyDocumentRemoved(DocumentId documentId) at Microsoft.CodeAnalysis.Workspace.ApplyProjectChanges(ProjectChanges projectChanges) at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.ApplyProjectChanges(ProjectChanges projectChanges) at Microsoft.CodeAnalysis.Workspace.TryApplyChanges(Solution newSolution, IProgressTracker progressTracker) at Microsoft.CodeAnalysis.MSBuild.MSBuildWorkspace.TryApplyChanges(Solution newSolution, IProgressTracker progressTracker)

    try
    {
        var solutionPath = dataAccessGeneratorConfig.Solution;

        using (var workspace = MSBuildWorkspace.Create())
        {
            var solution = workspace.OpenSolutionAsync(solutionPath).Result;

            foreach (var dataAccessSource in dataAccessGeneratorConfig.DataSources)
            {
                // Add/remove documents to the project
            }
            workspace.TryApplyChanges(solution);
            workspace.CloseSolution();
    }       
    catch (Exception ex)
    {
        // Exception Handling
    }

I have consulted other such thread and the github page but it doesn't help. Any clues?

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Rwiti
  • 1,056
  • 1
  • 13
  • 31
  • What are you removing? – SLaks Dec 12 '17 at 15:24
  • @SLaks, so the process is I remove the .cs files first and then regenerate them. We've been doing it for more than a year and there is no problem with that logic/process. – Rwiti Dec 12 '17 at 16:02
  • Did you check what you are trying to remove in your loop? Seems like you are trying to remove something that was brought in via one of project imports and you should not touch that. – nejcs Dec 12 '17 at 17:00

1 Answers1

1

It seems I am stepping over myself. I posted another question a few days back and the root cause of this problem is the solution I suggested there.

For this very issue as soon as I put this line in the .csproj file

<PropertyGroup>
    <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
</PropertyGroup>

everything started working as expected.

If I just have the above line the type system seems to be unaware of the classes in the project. An excellent Explanation is given by Matt Ward on this thread.

So, the proper solution is to have these two lines.

<PropertyGroup>
  <EnableDefaultCompileItems>false</EnableDefaultCompileItems>
  <EnableDefaultItems>false</EnableDefaultItems>
</PropertyGroup>
Rwiti
  • 1,056
  • 1
  • 13
  • 31