Version Used:
1.3.2, 2.0.0, 2.2.0
Steps to Reproduce:
Note:
Following is the .csproj in picture:
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{091EA627-E616-41FF-83C9-5C7964A5E379}</ProjectGuid>
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>SampleUI_Tester</RootNamespace>
<AssemblyName>SampleUI_Tester</AssemblyName>
<TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<SccProjectName>SAK</SccProjectName>
<SccLocalPath>SAK</SccLocalPath>
<SccAuxPath>SAK</SccAuxPath>
<SccProvider>SAK</SccProvider>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>S:\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>false</Optimize>
<OutputPath>S:\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xaml">
<RequiredTargetFramework>4.0</RequiredTargetFramework>
</Reference>
<Reference Include="WindowsBase" />
<Reference Include="PresentationCore" />
<Reference Include="PresentationFramework" />
</ItemGroup>
<ItemGroup>
<ApplicationDefinition Include="App.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="Model\ShiftingPanelModel.cs" />
<Compile Include="Model\SplasPanelModel.cs" />
<Compile Include="Model\SpsfsPanelModel.cs" />
<Compile Include="Utility\ChannelInfo.cs" />
<Compile Include="Model\CommonPanelModel.cs" />
<Compile Include="Utility\Parameter.cs" />
<Compile Include="Model\SpansPanelModel.cs" />
<Compile Include="Utility\UnitService.cs" />
<Compile Include="ViewModel\TestViewModelClasses.cs" />
<Page Include="SampleUI_Tester.xaml">
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Compile Include="App.xaml.cs">
<DependentUpon>App.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
<Compile Include="SampleUI_Tester.xaml.cs">
<DependentUpon>SampleUI_Tester.xaml</DependentUpon>
<SubType>Code</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs">
<SubType>Code</SubType>
</Compile>
<Compile Include="Properties\Resources.Designer.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="Properties\Settings.Designer.cs">
<AutoGen>True</AutoGen>
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<EmbeddedResource Include="Properties\Resources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>
</None>
<AppDesigner Include="Properties\" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Common\SampleUIComp\AplPresentation\AplPresentationCore.csproj">
<Project>{a1c968c9-b175-46a1-99f1-3e6ddca476d4}</Project>
<Name>AplPresentationCore</Name>
</ProjectReference>
<ProjectReference Include="..\..\Common\SampleCommonUI\SampleCommonUI.csproj">
<Project>{8eee87c7-50f6-4621-a47b-a76603901416}</Project>
<Name>SlimCommonUI</Name>
</ProjectReference>
<ProjectReference Include="..\Sample_ControlPanel\Sample_ControlPanel.csproj">
<Project>{82990b46-bb07-4485-9b33-b47f4cca7a54}</Project>
<Name>Sample_ControlPanel</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
<PropertyGroup>
<NoWin32Manifest>true</NoWin32Manifest>
</PropertyGroup>
</Project>
Expected Behavior:
Actual Behavior:
System.Threading.Tasks.TaskCanceledException: A task was canceled
Note: There is no further information which can be inferred from the exception dump of all the inner exceptions.
I created a solution with a single project with the contents of your .csproj above, and added a single file for the first <Compile tag in the csproj.
Then I created a project to try and reproduce this, but I was unable to. Please include a fully contained standalone repro to help us investigate this.
Here are the contents of my repro project.
``` C#
using System;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.MSBuild;
class Program
{
static void OnWorkspaceFailed(object sender, WorkspaceDiagnosticEventArgs e)
{
Console.WriteLine(e.Diagnostic.Message);
}
static void Main(string[] args)
{
Main().GetAwaiter().GetResult();
}
private static async Task Main()
{
var ws = MSBuildWorkspace.Create();
ws.WorkspaceFailed += OnWorkspaceFailed;
var sln = await ws.OpenSolutionAsync(@"D:\code\test\Sample.sln");
var p = sln.Projects.SingleOrDefault();
var d = p?.Documents.FirstOrDefault();
var s = await d?.GetSemanticModelAsync();
Console.WriteLine(s);
}
}
# app.config
```XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Tasks.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Utilities.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Engine" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Conversion.Core" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
<bindingRedirect oldVersion="0.0.0.0-99.9.9.9" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Activities.Build" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="15.0.0.0" />
<codeBase version="15.0.0.0" href=".\amd64\Microsoft.Activities.Build.dll" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="XamlBuildTask" culture="neutral" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="4.0.0.0" newVersion="15.0.0.0" />
<codeBase version="15.0.0.0" href=".\amd64\XamlBuildTask.dll" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{8C523DB6-D6D4-4B4C-B8F7-B2923C8E51B7}</ProjectGuid>
<OutputType>Exe</OutputType>
<RootNamespace>GetSemanticModelRepro</RootNamespace>
<AssemblyName>GetSemanticModelRepro</AssemblyName>
<TargetFrameworkVersion>v4.6.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="System.Threading.Tasks.Dataflow">
<Version>4.5.24</Version>
</PackageReference>
<Reference Include="System" />
<Reference Include="System.Core" />
<PackageReference Include="Microsoft.Build">
<Version>15.1.1012</Version>
</PackageReference>
<PackageReference Include="Microsoft.Build.Tasks.Core">
<Version>15.1.1012</Version>
</PackageReference>
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.2.0" />
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
Hello @Pilchie ,
I would like to reopen this issue. I have uploaded the required code at the following location:
https://www.dropbox.com/s/tt0uq2hzj1wnbth/Roslyn_Version_3_0_0_WithExe.zip?dl=0
We get the mentioned exception for project SPShifting_ControlPanel.
If we check out the .csproj, we will find duplicate entries for ProjectReference AplPresentationCore.csproj and ProjectReference AplPresentationCore.csproj.
Because we have these duplicate entries, I have added method RemoveDuplicateProjectReferences which removes the duplicate ProjectReference's.
But still we get the exception mentioned. Looking forward to further updates.
Hello @Pilchie ,
Can you please let me know the further updates on this email thread.
Looking forward to hear from you.
I haven't had a chance to look at this, but I wonder if #19620 will help identify the actual failure.
Thanks @Pilchie for pointing me out to #19620.
But I think though the broader scenario is the same, i.e. duplicate project references, I do not use the async await methodology directly. That is to say that I do not use the code 'var compilation = await project.GetCompilationAsync();`
Also, I am already removing the duplicate project references from a .csproj as mentioned in the code which I have shared.
Not sure how should i be using #19620.
Looking forward for further updates. I am completely stuck with this and the other issue which I have raised. It will be very helpful if you can lead me in some direction.
@abhijitparkhi1983 There was a bug in prior releases where failure of GetCompilationAsync was getting reported as cancellation, making it impossible to understand what went wrong. When you execute the same code against a version of Roslyn after this bug is fixed, you will still get an exception but it should now be an informative exception telling you about the problem that occurred.
@abhijitparkhi1983 Note that the fix for the issue will likely not appear before the 15.5 release. To find out what's going on using the currently-available tools, you can use a debugger to observe the exception which occurs prior to the cancellation exception, or you can attach a handler to AppDomain.FirstChanceException to log exception information. This information should help identify the underlying problem which led to the failure, and likely help us with reproducing it locally if it turns out to be a bug in Roslyn.
Thanks for a quick update @sharwell.
I will try attaching the handler to find if I am getting some useful info.
Can you please let me know the tentative date for 15.5 release.?
Here's the exception details:
System.InvalidCastException occurred
HResult=0x80004002
Message=Unable to cast object of type 'Microsoft.CodeAnalysis.BuildTasks.Csc' to type 'Microsoft.Build.Framework.ITask'.
Source=Microsoft.Build
StackTrace:
at Microsoft.Build.Shared.TaskLoader.CreateTask(LoadedType loadedType, String taskName, String taskLocation, Int32 taskLine, Int32 taskColumn, LogError logError, AppDomainSetup appDomainSetup, Boolean isOutOfProc, AppDomain& taskAppDomain)
After using the Exception Settings window in Visual Studio to break when InvalidCastException was thrown, I used the Immediate Window to evaluate a couple of expressions:
:bulb: I didn't actually type + but I'm using it to highlight the lines I typed. The responses are on the following lines.
+typeof(Microsoft.CodeAnalysis.BuildTasks.Csc).Assembly.FullName
"Microsoft.Build.Tasks.CodeAnalysis, Version=2.2.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
+typeof(Microsoft.CodeAnalysis.BuildTasks.Csc).Assembly.Location
"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Enterprise\\MSBuild\\15.0\\Bin\\Roslyn\\Microsoft.Build.Tasks.CodeAnalysis.dll"
+typeof(Microsoft.Build.Framework.ITask).Assembly.FullName
"Microsoft.Build.Framework, Version=15.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
+typeof(Microsoft.Build.Framework.ITask).Assembly.Location
"C:\\Users\\sam\\Downloads\\Roslyn_Version_3_0_0_WithExe\\Roslyn_Walker_Test\\bin\\Debug\\Microsoft.Build.Framework.dll"
+typeof(Microsoft.CodeAnalysis.BuildTasks.Csc).GetInterfaces()[0].FullName
"Microsoft.Build.Framework.ITask"
+typeof(Microsoft.CodeAnalysis.BuildTasks.Csc).GetInterfaces()[0].Assembly.FullName
"Microsoft.Build.Framework, Version=14.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
+typeof(Microsoft.CodeAnalysis.BuildTasks.Csc).GetInterfaces()[0].Assembly.Location
"C:\\WINDOWS\\Microsoft.Net\\assembly\\GAC_MSIL\\Microsoft.Build.Framework\\v4.0_14.0.0.0__b03f5f7f11d50a3a\\Microsoft.Build.Framework.dll"
I resolved this discrepancy by adding a few more binding redirects to the App.config file for Roslyn_Walker_Test:
This set of steps forced the Here's how I knew what to add (click to expand)...
Add-BindingRedirect commandletAdd-BindingRedirect to consider the additional assembly when it calculated the required redirections.
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Utilities.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Framework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Build.Tasks.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-15.1.0.0" newVersion="15.1.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Console" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.1.0" newVersion="4.0.1.0" />
</dependentAssembly>
The redirects fixed the InvalidCastException, but I was still seeing an ArgumentException:
System.ArgumentException occurred
HResult=0x80070057
Message=An item with the same key has already been added.
Source=mscorlib
StackTrace:
at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.CodeAnalysis.SolutionState.CompilationTracker.<FinalizeCompilationAsync>d__34.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
By looking at the Locals window in Visual Studio, I determined that SPShifting_ControlPanel was attempting to add a second reference to AplPresentationCore and SlimCommonUI (you knew this).
So I took a look at the RemoveDuplicateProjectReferences method you wrote and found a bug. Since Roslyn uses immutable objects, calls to Project.RemoveProjectReference will not affect the Solution containing the project. So I modified the RemoveDuplicateProjectReferences method like this and it worked great:
- private static void RemoveDuplicateProjectReferences(ref Project projectInstance)
+ private static void RemoveDuplicateProjectReferences(ref Solution solution, ref Project projectInstance)
{
var allProjectReference = projectInstance.AllProjectReferences;
var distinctProjectCount = allProjectReference.Distinct().Count();
if (allProjectReference.Count > distinctProjectCount)
{
List<ProjectReference> uniqueProjectReferencs = new List<ProjectReference>();
List<ProjectReference> duplicateProeProjectReferencs = new List<ProjectReference>();
foreach (var projectRef in projectInstance.AllProjectReferences)
{
if (!uniqueProjectReferencs.Contains(projectRef))
uniqueProjectReferencs.Add(projectRef);
else
duplicateProeProjectReferencs.Add(projectRef);
}
foreach (var dupProjRef in duplicateProeProjectReferencs)
{
- projectInstance = projectInstance.RemoveProjectReference(dupProjRef);
+ solution = solution.RemoveProjectReference(projectInstance.Id, dupProjRef);
}
}
+ projectInstance = solution.GetProject(projectInstance.Id);
}
Thanks for the investigation @sharwell.
@abhijitparkhi1983 Looks like there is nothing to fix here on the Roslyn side that isn't already tracked elsewhere.
Hello @sharwell
Thanks a lot for the very detailed description. I have modified my code accordingly and I am testing the output. But from the primary testing that I have done till now, I can say that the changes suggested by you are working good. Thanks a lot for this very very important input.
I am further testing this one because there are still few Document's which return empty References for the sementicModel.Compilation.References. Need to check the possible reasons for such a behavior.
But yes, I need to admit that the changes you have suggested has made my day. Thanks a lot again :)
@abhijitparkhi1983 Thank you that means a lot to me 馃槃