DamianEdwards / RazorSlices

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.
MIT License
294 stars 12 forks source link

Latest version - problems with new source generator? #49

Open jtsom opened 3 weeks ago

jtsom commented 3 weeks ago

I updated a test app with the latest version.

The first thing I found was I had to change the way rendering to a string was done. I have a Slices folder with a Hello.cshtml file. I have a simple class to pass in some data:

namespace Net8Test.Data;

public class TestClass
{
    public string Url { get; set; } = null!;
}

To get html, in the previous version, all I did was: image

Now, with the latest changes I need to do: image

Note - in line 36, I have to use a lower-case version of the app class - net8test vs Net8Test. Net8Test does not have the .Slices member image

Once built I tried a test, but an exception was thrown, pointing to a source generator problem:

System.TypeInitializationException: The type initializer for 'net8test.Slices.Hello' threw an exception.
 ---> System.InvalidOperationException: Razor view type 'AspNetCoreGeneratedDocument.Slices_Hello, net8test' was not found. This is likely a bug in the RazorSlices source generator.
   at net8test.Slices.Hello..cctor() in E:\source\net8test\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\net8test.RazorSliceProxies.g.cs:line 37
   --- End of inner exception stack trace ---
   at net8test.Slices.Hello.global::RazorSlices.IRazorSliceProxy.CreateSlice[TModel](TModel model) in E:\source\net8test\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\net8test.RazorSliceProxies.g.cs:line 57
   at net8test.RazorSlicesGenericFactory.CreateSlice[TProxy,TModel](TModel model) in E:\source\net8test\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\net8test.RazorSliceProxies.g.cs:line 24
   at net8test.Slices.Hello.Create[TModel](TModel model) in E:\source\net8test\obj\Debug\net8.0\RazorSlices.SourceGenerator\RazorSlices.SourceGenerator.RazorSliceProxyGenerator\net8test.RazorSliceProxies.g.cs:line 51
   at Program.<>c.<<<Main>$>b__0_0>d.MoveNext() in E:\source\net8test\Program.cs:line 36
--- End of stack trace from previous location ---
   at Microsoft.AspNetCore.Http.RequestDelegateFactory.ExecuteTaskResult[T](Task`1 task, HttpContext httpContext)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

HEADERS
=======
Accept: */*
Host: localhost:7004
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/5

37.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0
Accept-Encoding: gzip, deflate, br, zstd
Accept-Language: en-US,en;q=0.9
Cookie: ai_user=ymDB2Fw1m1ILpD/FdRkQxG|2023-10-10T20:56:58.529Z; ASP.NET_SessionId=pa2esmn4ubrp25xzxrjfgitb; DigitalOptometrics.WebUI.Startup=kxUtd8UWy0_P2f3UEbYa942AK9ni8MyE5ztnZjXbk-IwUxLm1BwxffMzNvFNjLp7hcguCpTqesDN0Z3Gq1agcppAjj2-tZd59md0X16F51yV2-M6D7-zsGu6sfbg8vR5s4CaueOW9yqEge8DnoAf8syErI5MsYiAx91AGF9FqB9_VP_ZDJykFsiIfNhg0Lum3x_0tVzS2wQ34tcfraM4qNtTzkr-uI4iAOJ4vvJH2V3ZxDvFIq2WhQFex8AgUGpMyrdqcEu03WNwBdAPoP5rPkAsr1V5GloSul8yMvjsH_ZXmLXyDkbnu0k4livWQH5NYv_JRD41qtueAcr3OaQHbSwRVhvmggh-tuBDGvI3R4XZ-zG0lsmRonGhH6mpbYI1qya0RLJsVUmQrkOik_5818j-rxnb--VQKJh6L8XET5hHJ-f9UALS0YNTyRud9McPFcJLjWLn66oeQDM5j09pi4XxtQ4g574rsfw_V5ZqW4qjHbo5yewbD4CHY8OZEkVICf8AsodfrXx3_r46lxEg-d9FnJtCR5pMZU0-F5fjYJIan8ozDG47BiWeGP2b1Ew4vMIxiJtWOkrwOm9esIwWErjmIBh5teDWtA9EfGfxgQrq_T6eaVUeXHRBovHAiDZ7AvE2oUtTuYGfNnZbX2uifoHw_8Baeq5BKH4M3ZxLpRT4398uvQSNU1AIxAuvh_G3UJccSTp1PD8YwJiSJ2JQcQ
Referer: https://localhost:7004/swagger/index.html
sec-ch-ua: "Microsoft Edge";v="125", "Chromium";v="125", "Not.A/Brand";v="24"
sec-ch-ua-mobile: ?0
sec-ch-ua-platform: "Windows"
sec-fetch-site: same-origin
sec-fetch-mode: cors
sec-fetch-dest: empty
priority: u=1, i

net8test (2).zip

jtsom commented 3 weeks ago

Quick followup, adding a new endpoint:

app.MapGet("/test", () => Results.Extensions.RazorSlice<net8test.Slices.Hello, TestClass>(new TestClass()));

Does build and work without exception. but again, I had to add the net8test namespace to get the .Slices.

DamianEdwards commented 3 weeks ago

Hmmm could be an issue with project directory name casing. Can you create a minimal repro project?

jtsom commented 3 weeks ago

Hmmm could be an issue with project directory name casing. Can you create a minimal repro project?

There is one in the original report. I thought that also. The original .csproj file was all lower case, I renamed it, but it didn't make a difference.

jtsom commented 3 weeks ago

After renaming the enclosing folder, and anything else in the project that was lower case, and cleaning and rebuilding a couple times, it looks like it's doing the right thing now.

Even thought VSCode still things there's an error: image

Everything builds and runs successfully. Quitting and relaunching VSCode didn't help. I blame the C# dev kit...