1
votes

The error above happens with VS 2017 15.5.2 in a test project while executing the following test. There are binding redirects in the test.vbproj file but nothing for msbuild.

    <TestMethod()> Public Sub ElementTypeUnitTestAsync()
       Dim RoslynPath As String = Path.Combine(RepoPath, SolutionPartialPath)

        Dim MS_Workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
        AddHandler MS_Workspace.WorkspaceFailed, Sub(sender As Object, e As WorkspaceDiagnosticEventArgs)
                                                     Debug.WriteLine(e.Diagnostic.ToString())
                                                 End Sub
        Dim NewSolution As Solution = MS_Workspace.OpenSolutionAsync(RoslynPath).Result
    End Sub

The whole message is

[Failure] Msbuild failed when processing the file 'C:\Repos\roslyn-master\src\Samples\CSharp\APISampleUnitTests\APISampleUnitTestsCS.csproj' with message: C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets: (1601, 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: (1601, 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
According to the error message, it seems that the reference Microsoft.Build.Framework assembly not being correct. Which version of Microsoft.Build.Framework you are using in the project? Have you try to use other version?Leo Liu-MSFT
I just reference Microsoft.CodeAnalysis.MSBuild version 2.6 or 2.6.1 in a test project. All the references come from C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\MSBuild\15.0\Bin\Microsoft.Common.CurrentVersion.targets which I don't control.Paul Cohen
Have you try to re-install the nugget package manager with installer? And have you check the binding redirect for Microsoft.Build.Framework? I could not reproduce this issue, if you can reproduce the problem stably, could you please share me a simple sample, so I could reproduce this issue on my side?Joy
Any news about this issue?Jonathas Costa
The failing code is posted above as a UnitTest. I don't see how to make it simpler. @Alex if you have binding redirects that make this work or a Test Project where this works, please post. The important part is that this is in a TEST Project.Paul Cohen

1 Answers

-1
votes

This code will allow selection of VS instance if there are more than 1

Imports Microsoft.Build.Locator

Public Class VS_Selector_Dialog1
    Private m_instance As VisualStudioInstance = Nothing
    Private SelectedRow As Integer = -1
    Private visualStudioInstances() As VisualStudioInstance = MSBuildLocator.QueryVisualStudioInstances().ToArray()
    Public ReadOnly Property MSBuildInstance As VisualStudioInstance
        Get
            Return m_instance
        End Get
    End Property

    Private Sub Cancel_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Cancel_Button.Click
        DialogResult = DialogResult.Cancel
        Close()
    End Sub

    Private Sub DataGridView1_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridView1.SelectionChanged
        m_instance = visualStudioInstances(DataGridView1.CurrentRow.Index)
    End Sub

    Private Sub OK_Button_Click(ByVal sender As Object, ByVal e As EventArgs) Handles OK_Button.Click
        If m_instance Is Nothing Then
            Exit Sub
        End If
        DialogResult = DialogResult.OK
        Close()
    End Sub

    Private Sub VS_Selector_Dialog1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim BestIndex As Integer = -1
        Text = "Multiple installs of MSBuild detected please select one:"
        Dim InstanceTable As New DataTable
        InstanceTable.Columns.Add("InstanceNumber")
        InstanceTable.Columns.Add("Edition")
        InstanceTable.Columns.Add("Version")
        InstanceTable.Columns.Add("MSBuildPath")
        For Index As Integer = 0 To visualStudioInstances.Length - 1
            If visualStudioInstances(Index).Name.Contains(" Build ") Then
                Continue For
            End If
            BestIndex = Index
            Dim rowToAdd As DataRow = InstanceTable.NewRow()
            rowToAdd("InstanceNumber") = Index + 1
            rowToAdd("Edition") = visualStudioInstances(Index).Name
            rowToAdd("Version") = visualStudioInstances(Index).Version
            rowToAdd("MSBuildPath") = visualStudioInstances(Index).MSBuildPath
            InstanceTable.Rows.Add(rowToAdd)
        Next
        DataGridView1.DataSource = InstanceTable
        If InstanceTable.Rows.Count = 1 Then
            m_instance = visualStudioInstances(BestIndex)
            DialogResult = DialogResult.OK
            Close()
        End If
     End Sub
End Class

Have the user select the instance, or you get select the "best"

If VS_Selector_Dialog1.ShowDialog(Me) <> DialogResult.OK Then
    Stop
End If

Once you have the correct instance MSBuild will work

Console.WriteLine($"Using MSBuild at '{VS_Selector_Dialog1.MSBuildInstance.MSBuildPath}' to load projects.")
' NOTE: Be sure to register an instance with the MSBuildLocator
'       before calling MSBuildWorkspace.Create()
'       otherwise, MSBuildWorkspace won't MEF compose.
MSBuildLocator.RegisterInstance(VS_Selector_Dialog1.MSBuildInstance)
Using Workspace As MSBuildWorkspace = MSBuildWorkspace.Create()
    AddHandler Workspace.WorkspaceFailed, AddressOf MSBuildWorkspaceFailed
    Dim currentProject As Project = Workspace.OpenProjectAsync(.FileName).Result
    Workspace.LoadMetadataForReferencedProjects = True
    If currentProject.HasDocuments Then
        For Each document As Document In currentProject.Documents