Strange diagnostics errors, prefedined type System… is not defined or imported

时光总嘲笑我的痴心妄想 提交于 2019-12-19 11:21:22

问题


I am getting Roslyn diagnostics errors when parsing a very basic .NET 4.6 application. The solution files can be downloaded from there https://github.com/dotnet/roslyn/files/2393288/DemoSolution.zip

The dependency tree looks like this:

BLL -> DB

I am getting the following diagnostics errors in the BLL project:

The solution and projects build fine, still Roslyn gives these errors. Maybe the errors are misleading and I need to configure the projects in someway? Any idea how I can resolve these errors?

Here is the code used for parsing the files:

var properties = new Dictionary<string, string>
                {
                    ["DesignTimeBuild"] = "true",
                    ["CheckForSystemRuntimeDependency"] = "true"
                };
                var workspace = MSBuildWorkspace.Create(properties);

                workspace.WorkspaceFailed += (sender, args) =>
                {

                };
                workspace.LoadMetadataForReferencedProjects = true;

Solution solution = workspace.OpenSolutionAsync(SolutionFilePath).Result;               
                    foreach (var p in solution.Projects)
                    {   
                        foreach (var file in p.Documents)
                        {
      var semanticModel = file.GetSemanticModelAsync().Result;

                        var mscorlib = MetadataReference.CreateFromFile(file.FilePath);
                    var compilation = CSharpCompilation.Create("MyCompilation",
                        new[] { semanticModel.SyntaxTree }, new[] { mscorlib });

                    var model = compilation.GetSemanticModel(semanticModel.SyntaxTree);
                    var declarationDiagnistics = model.Compilation.GetDeclarationDiagnostics(CancellationToken.None);
                    var parseDiagnostics = model.Compilation.GetParseDiagnostics(CancellationToken.None);
                    var allDiagnostics = model.Compilation.GetDiagnostics(CancellationToken.None);
                    var methodBodyDiagnostics = model.Compilation.GetMethodBodyDiagnostics(CancellationToken.None);

                                  }
                }

Subscriping to the workspace.workspaceFailed event results in the following error:

Msbuild failed when processing the file 'MYPATH\BLL.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task could not be instantiated from the assembly "C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\Common7\IDE\CommonExtensions\Microsoft\NuGet\NuGet.Build.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'NuGet.Build.Tasks.GetReferenceNearestTargetFrameworkTask' to type 'Microsoft.Build.Framework.ITask'. C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1656, 5): The "GetReferenceNearestTargetFrameworkTask" task has been declared or used incorrectly, or failed during construction. Check the spelling of the task name and the assembly name.


回答1:


So this finally solved it:

  1. Added the microsoft.build redirects as suggested by @GeorgeAlexandria http://github.com/Microsoft/msbuild/issues/2369#issuecomment-353674937

  2. Cleared the Microsoft.Build.* from the output bin folder

  3. Added the Microsoft.Build.Locator as a reference

  4. Added the line MSBuildLocator.RegisterDefaults() above the workspace code.

Source: https://github.com/dotnet/roslyn/issues/26029#issuecomment-380164421



来源:https://stackoverflow.com/questions/52385005/strange-diagnostics-errors-prefedined-type-system-is-not-defined-or-imported

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!