C
C#โ€ข2y ago
QzL

โ” Incremental source generator not called with docker

Hello, I have 2 projects (simplified): - one sandbox .net6 - one SourceGenerator .netStandard2.0 In my SourceGenerator, I have a class called RestServicesSourceGenerator like this
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

[Generator]
public sealed class RestServicesSourceGenerator
: IIncrementalGenerator
{
public RestServicesSourceGenerator()
{
}

void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext context)
{
// processing code
}
}
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Syntax;

[Generator]
public sealed class RestServicesSourceGenerator
: IIncrementalGenerator
{
public RestServicesSourceGenerator()
{
}

void IIncrementalGenerator.Initialize(IncrementalGeneratorInitializationContext context)
{
// processing code
}
}
And in my sandbox project, I have a reference of my SourceGenerator
<ProjectReference Include="..\..\project.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
<ProjectReference Include="..\..\project.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="true" />
When i start building my sandbox (from VS or from command line dotnet build --no-incremental), the IIncrementalGenerator.Initialize is executed and i can see my generated files. When i start building from docker using this image mcr.microsoft.com/dotnet/sdk:6.0 nothing happens. ๐Ÿ˜ญ When i start building from azure pipepline using vmImage: 'ubuntu-latest' it's work ๐Ÿ˜… If someone have an idea ๐Ÿฅน Deps are :
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.5.0" PrivateAssets="all" />
<PackageReference Update="Microsoft.CodeAnalysis.Analyzers" Version="3.3.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.5.0" PrivateAssets="all" />
#rosyln
31 Replies
333fred
333fredโ€ข2y ago
Your generator is referencing workspaces? Also, MS.CA 4.5 corresponds to VS 17.5. That is not the version in .NET 6
QzL
QzLOPโ€ข2y ago
My generator reference some abstract projects for helping to generate or carry some custom attribute. Microsoft.CodeAnalysis.CSharp.Workspaces 4.5.0 can be used for .net6 app & netstandard2.0
333fred
333fredโ€ข2y ago
So, that doesn't mean what you think it means First and foremost: you need to not depend on workspaces You can only depend on MS.CA and MS.CA.CSharp/VB Workspaces APIs are not available to generators Second, yes MS.CA 4.5 supports being run on .NET 6. However, you don't control what version of MS.CA is used The compiler is the thing that loads you, not the other way around So you need to not depend on a newer version of MS.CA that could load you The best way to determine this is to look at the .NET 6 SDK and see what version the dlls are (iirc they're going to be either 4.0 or 4.1) You could also have the project consuming the generator depend on Microsoft.Net.Compilers.Toolset 4.5.0, which will cause the project to be built with the 4.5 version of MS.CA. However, the generator still won't work in VS less than 17.5
QzL
QzLOPโ€ข2y ago
Thanks, I downgraded Microsoft.CodeAnalysis.CSharp.Workspaces to 4.2.0 and Microsoft.CodeAnalysis.Analyzers to 3.3.3 and it seem to work
333fred
333fredโ€ข2y ago
You cannot use Workspaces You will, at some point, accidentally use an API that is not available to your generator and it will fail again You must use MS.CA.CSharp
i need this package Microsoft.CodeAnalysis.CSharp.Workspaces to reference IIncrementalGenerator
No, that comes from MS.CA MS.CA's packages are a series of layers. The generator base apis (such as IIncrementalGenerator) come from the base layer When you run a dotnet build, you have up to and including the MS.CA.CSharp and VB layers The Workspaces layer includes all of these, but more things Those more things do not exist and you will run into errors if you accidentally start depending on APIs in this layer
QzL
QzLOPโ€ข2y ago
Sorry, misreading. yeah i will try with this <PackageReference Include="Microsoft.CodeAnalysis.Common" Version="4.2.0" PrivateAssets="all"/> <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.2.0" PrivateAssets="all"/>
333fred
333fredโ€ข2y ago
You don't need the first, the second includes it as a transitive dependency. Otherwise, looks good
QzL
QzLOPโ€ข2y ago
I was some error without the Common
QzL
QzLOPโ€ข2y ago
333fred
333fredโ€ข2y ago
Your tests are depending on the wrong version still
QzL
QzLOPโ€ข2y ago
it ask for version 3.8.0 but on this version IIncrementalGeneratordoesn't exist, i should use ISourceGenerator
QzL
QzLOPโ€ข2y ago
333fred
333fredโ€ข2y ago
What asks for 3.8.0?
QzL
QzLOPโ€ข2y ago
Microsoft.CodeAnalysis.CSharp.Workspaces ask for Microsoft.CodeAnalysis.CSharp to be on version 3.8
333fred
333fredโ€ข2y ago
Wat Why are you referencing this package?
QzL
QzLOPโ€ข2y ago
I have only Microsoft.CodeAnalysis.CSharp on my deps
333fred
333fredโ€ข2y ago
Then what did you mean by this?
QzL
QzLOPโ€ข2y ago
333fred
333fredโ€ข2y ago
Well, first, you're depending on the CodeRefactoring unit testing framework. Why? And CodeFix Are you writing codefixers or refactorings?
QzL
QzLOPโ€ข2y ago
Only SourceGenerator
333fred
333fredโ€ข2y ago
Then only reference that unit testing framework
QzL
QzLOPโ€ข2y ago
it's global file props, i need to refactor this parts ๐Ÿ™‚
QzL
QzLOPโ€ข2y ago
Andrew Lock | .NET Escapades
Source generator updates: incremental generators: Exploring .NET 6 ...
In this post I look at the updates to the source generator API in .NET 6, why the changes were made, and how to update your source generators to use them.
QzL
QzLOPโ€ข2y ago
this is why i used Microsoft.CodeAnalysis.CSharp.Workspaces
333fred
333fredโ€ข2y ago
Well, I can't help bad articles ๐Ÿ™‚
QzL
QzLOPโ€ข2y ago
Hello, I'm still blocked with the Microsoft.CodeAnalysis.CSharp. I don't use Microsoft.CodeAnalysis.CSharp.Workspaces but CA.Csharp required Workspace on 3.8.0. I shouldn't add Workspace on reference.. but when i add it and run it from VS it's good If i build from docker with sdk7 it's also good but i can't run test because the dotnet 6 runtime isn't here If i build from docker with sdk6 it's going bad (can't build)
QzL
QzLOPโ€ข2y ago
GitHub
roslyn/NuGet-packages.md at main ยท dotnet/roslyn
The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs. - roslyn/NuGet-packages.md at main ยท dotnet/roslyn
333fred
333fredโ€ข2y ago
CA.Csharp does not require Workspaces. I'm not sure what is going on with the testing framework here though. @sharwell, why is the generator testing framework bringing in workspaces and ruining the package references here?
sharwell
sharwellโ€ข2y ago
The source generator references Microsoft.CodeAnalysis.CSharp. The test project needs to explicitly reference Microsoft.CodeAnalysis.Workspaces.CSharp of the same version.
QzL
QzLOPโ€ข2y ago
thanks you i have created a Dockerfile with sdk 6 & sdk7 and now it's work ๐Ÿ˜„
Accord
Accordโ€ข2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server