Latest version (0.9.1) results in many compile errors
I updated a .Net 8 project that was using 0.8.1 to the latest 0.9.1, and when the project is compiled, there are a ton of errors of the kind:
error CS0103: The name 'Slices' does not exist in the current context
Sample line where the error is generated:
var slice = Slices.Rx.ExportPrescriptionPDF.Create(prescriptionDetail);
I am also seeing several warnings about analyzers and source generators:
CSC : warning CS9057: The analyzer assembly 'e:\.nuget\packages\razorslices\0.9.1\analyzers\dotnet\cs\RazorSlices.SourceGenerator.dll' references version '4.12.0.0' of the
compiler, which is newer than the currently running version '4.11.0.0'.
CSC : warning RSG002: TargetPath not specified for additional file: E:\source\repos\MyApp.DocGen\DocGen.Services\bin\Debug\net8.0\Slices\Rx\ExportPrescription PDF.cshtml.
The RSG002 looks to be every .cshtml file that is being used as a slice.
Backing down to 0.8.1 clears the errors.
Looks like the SourceGenerator isn't working? Also, this is using VSCode (with the C# Sdk). Opening the project using VS2022 did return the same errors, but after several clean/rebuild cycles, it seems to fix the problem. (Yes, I've done a dotnet clean dotnet build several times.
Thanks
I also have this issue after updating from 0.8.1 to 0.9.1 In my case several clean/rebuild cycles do not help. Only backing down to 0.8.1
Hmmm. Hard to diagnose what's going on here unless the compiler version being referenced is enough to disable the source generator running.
What version of the .NET SDK are you running?
dotnet --info
.NET SDK:
Version: 8.0.401
Commit: 811edcc344
Workload version: 8.0.400-manifests.b6724b7a
MSBuild version: 17.11.4+37eb419ad
Runtime Environment:
OS Name: Mac OS X
OS Version: 14.5
OS Platform: Darwin
RID: osx-arm64
Base Path: /usr/local/share/dotnet/sdk/8.0.401/
.NET workloads installed:
Configured to use loose manifests when installing new manifests.
There are no installed workloads to display.
Host:
Version: 8.0.8
Architecture: arm64
Commit: 08338fcaa5
.NET SDKs installed:
6.0.101 [/usr/local/share/dotnet/sdk]
7.0.100 [/usr/local/share/dotnet/sdk]
8.0.401 [/usr/local/share/dotnet/sdk]
.NET runtimes installed:
Microsoft.AspNetCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 7.0.0 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 8.0.8 [/usr/local/share/dotnet/shared/Microsoft.AspNetCore.App]
Microsoft.NETCore.App 6.0.1 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 7.0.0 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Microsoft.NETCore.App 8.0.8 [/usr/local/share/dotnet/shared/Microsoft.NETCore.App]
Other architectures found:
x64 [/usr/local/share/dotnet/x64]
registered at [/etc/dotnet/install_location_x64]
And a part of .csproj file:
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.45.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="7.0.5" />
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="7.0.20" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.StackExchangeRedis" Version="7.0.16" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.3" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.3">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="7.0.0" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.3" />
<PackageReference Include="RazorSlices" Version="0.8.1" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore.Newtonsoft" Version="6.2.3" />
</ItemGroup>
I have .Net 9 installed, but the project is .Net 8 (whatever the latest is). On Windows. The project has a global.json to fix it to 8 also.
I started seeing this too with just a minimal test app that pulled in 0.9.1 of RazorSlices and tried to render a simple slice. In particular, I would get the same CS9057 warning about the compiler version mismatch followed by various errors. After digging through the Roslyn repo, I found that recently they changed the warning text to be more like this (emphasis mine):
Analyzer assembly '{0}' cannot be used because it references version '{1}' of the compiler, which is newer than the currently running version '{2}'.
When tracing the PR and related issues that triggered that change, I found this comment from Jared Parsons on the C# compiler team. Quoting the relevant bit here:
That corresponds with the 4.12.0.0 version of the compiler. When your source generator references 4.12.0.0 then everything builds because it's also loaded into a 4.12.0.0 version of the compiler. When you change the source generator to reference 4.13.0.0 then it gets loaded into a 4.12.0.0 version of the compiler which doesn't work. The compiler will reject analyzers / generators that were compiled against newer versions of the compiler: that could be depending on APIs, types, etc ... that don't exist.
So I think the issue here is that 0.9.0 of RazorSlices updated the version of Microsoft.CodeAnalysis.CSharp that its source generator depends on to 4.12.0, which according to this table says the min VS Version is 17.12. And inferring from this table, VS 17.12 (Roslyn 4.12) support starts with the .NET 9 SDK.
I think to get things working as before, the dependency on Microsoft.CodeAnalysis.CSharp would have to be at most 4.11.0 so that it will be compatible with the version of the compiler used in the .NET 8 SDK.
i am happpily using the 0.9.1 on .net 9 (latest). had to tweak the package refs for the right compiler version. this helped:
<ItemGroup Label="set the compiler toolset to a specific version"> <PackageVersion Include="Microsoft.Net.Compilers.Toolset" Version="4.14.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.14.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.Common" Version="4.14.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" /> <PackageVersion Include="Microsoft.CodeAnalysis.Workspaces.MSBuild" Version="4.14.0" /> </ItemGroup>
@DamianEdwards I just updated my project to 0.9.1 and am still getting lots of errors related to generated files
e:\source\repos\App.DocGen\DocGen.Services\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\DocGen.Services.RazorSliceProxies.g.cs(264,43): error CS1022: Type or namespace definition, or end-of-file expected [e:\source\repos\App.DocGen\DocGen.Services\DocGen.Services.csproj]
e:\source\repos\App.DocGen\DocGen.Services\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\DocGen.Services.RazorSliceProxies.g.cs(265,1): error CS1022: Type or namespace definition, or end-of-file expected [e:\source\repos\App.DocGen\DocGen.Services\DocGen.Services.csproj]
and
e:\source\repos\app.DocGen\DocGen.Services\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\DocGen.Services.RazorSliceProxies.g.cs(264,51): error CS0116: A namespace cannot directly contain members such as fields, methods or statements [e:\source\repos\app.DocGen\DocGen.Services\DocGen.Services.csproj]
I've done a dotnet clean and removed the bin and obj folders. Oddly, when I do a dotnet build, these errors do not appear, but when I attempt to run the app, they appear.
This app is targeting .Net 8, btw.
FYI.. the above fix from @vip32 does not help.
Try updating to 0.9.3 and LMK if that helps. I'm not able to reproduce this.
Try updating to 0.9.3 and LMK if that helps. I'm not able to reproduce this.
That did it! Thanks!