dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.55k stars 10.05k forks source link

reference a sourcegenerator and use razor pages in same project ... Microsoft.CodeAnalysis.CSharp version error #39297

Closed aktxyz closed 2 years ago

aktxyz commented 2 years ago

I just updated from VS2022 preview to the final VS2022 (17.0.3) ... and some projects that have both razor pages AND reference a netstandard2.0 source generator project, now fail to compile with the error

Generator 'RazorSourceGenerator' failed to generate source. 
It will not contribute to the output and compilation errors may 
occur as a result. Exception was of type 'InvalidCastException' 
with message '[A]Microsoft.CodeAnalysis.CSharp.CSharpParseOptions 
cannot be cast to [B]Microsoft.CodeAnalysis.CSharp.CSharpParseOptions. 

Type A originates from 'Microsoft.CodeAnalysis.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 
in the context 'Default' at location 
'C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\Roslyn\Microsoft.CodeAnalysis.CSharp.dll'. 

Type B originates from 'Microsoft.CodeAnalysis.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' 
in the context 'LoadFrom' at location 
'C:\Users\<someuser>\.nuget\packages\microsoft.codeanalysis.csharp\4.0.1\lib\netstandard2.0\Microsoft.CodeAnalysis.CSharp.dll'.'

I have other projects that just reference this source generator project and DO NOT have any razor pages, and those projects compile fine. It seems like there is a conflict between what razor is using to code generate and what my own source generator uses.

Is there a known issue with this scenario?

aktxyz commented 2 years ago

I just found that by using a 3.11.0 version of Microsoft.CodeAnalysis.CSharp, the build errors are resolved.

So now using this

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="3.11.0" PrivateAssets="all" />

instead of this or this

<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.0" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" PrivateAssets="all" />
Pilchie commented 2 years ago

@jaredpar what is the guidance for how to have multiple Source Generators that reference different versions of Microsoft.CodeAnalysis.CSharp? Shouldn't the compiler server handle resolution and force it to unify?

jaredpar commented 2 years ago

Shouldn't the compiler server handle resolution and force it to unify?

Yes the compiler server will force it to unify.

Are we sure that is what is happening here? The customer "fixed" the problem by changing the set of <PackageReference> in the project. That should in no way impact how the compiler server resolves dependencies in generators.

Pilchie commented 2 years ago

It sure looks like it. @captainsafia / @pranavkm any other ideas?

jaredpar commented 2 years ago

Do we have a binlog of the failure or a crash dump to look at?

@chsienki, @RikkiGibson

pranavkm commented 2 years ago

@aktxyz could you share a minimal app that reproduces the problem? Alternatively, if you could share your binlogs with us that might be helpful.

ghost commented 2 years ago

Hi @aktxyz. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 2 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.

aktxyz commented 2 years ago

I am working on a simple vs solution example for this

ghost commented 2 years ago

Hi @aktxyz. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

ghost commented 2 years ago

This issue has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 4 days. It will be closed if no further activity occurs within 3 days of this comment. If it is closed, feel free to comment when you are able to provide the additional information and we will re-investigate.

See our Issue Management Policies for more information.