Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Razor Pages squigglies for internal PageModel implementations #9352

Open
lonix1 opened this issue Sep 29, 2023 · 5 comments
Open

Razor Pages squigglies for internal PageModel implementations #9352

lonix1 opened this issue Sep 29, 2023 · 5 comments
Assignees
Labels
area-compiler Umbrella for all compiler issues bug Something isn't working

Comments

@lonix1
Copy link

lonix1 commented Sep 29, 2023

Environment data

dotnet --info output: 7.0.401
VS Code version: 1.82.2
C# Extension version: 2.3.27

OmniSharp log

n/a

Steps to reproduce

Foo.cshtml.cs

internal sealed class FooModel : PageModel {                      // <--------- internal
  public string Name { get; set; } = "Mike";
  // ...
}

Foo.cshtml

@page /foo
@model FooModel
<div>
  Hello @Model.Name      @* red squigglies start at first invocation of Model, till end of document *@
  ...                    @* red squigglies *@
  ...                    @* red squigglies *@
</div>                   @* red squigglies *@

Expected behavior

No errors, no warnings, no squigglies.

Actual behavior

Many red squigglies in vscode. Example:

Inconsistent accessibility: property type 'IHtmlHelper<FooModel>' is less accessible than property 'Pages_Foo.Html' CS0053
Inconsistent accessibility: property type 'IHtmlHelper<FooModel>' is less accessible than property 'Pages_Foo.ViewData' CS0053
Inconsistent accessibility: property type 'IHtmlHelper<FooModel>' is less accessible than property 'Pages_Foo.Model' CS0053

Additional context

Even though reported as errors, the app works. It's perfectly valid code.

When I change internal to public, the squigglies disappear. But I don't want to do that, as the code I've written is best practice.

The above example is simplistic. For a real RP page, of non-trivial length, the entire editor is filled with red squigglies. It's completely unusable.

@lonix1 lonix1 changed the title RazorPages squigglies for internal PageModel implementations Razor Pages squigglies for internal PageModel implementations Sep 29, 2023
@dibarbet dibarbet added bug Something isn't working Razor labels Oct 3, 2023
@davidwengier
Copy link
Contributor

Thanks for reporting this issue. I'm going to move it to the Razor repo, as its a compiler issue. It seems at runtime the compiler is emitting an internal class for the generated code, so there is no error, but at design time it generates a public class. With a public class, and a public FooModel Model { get; set; } being generated, the error is expected, and can be seen in a regular C# class too:

image

The main issue here is the difference between designtime and runtime behaviour for Razor developers

@davidwengier davidwengier transferred this issue from dotnet/vscode-csharp Oct 3, 2023
@ghost ghost added the untriaged label Oct 3, 2023
@davidwengier davidwengier removed their assignment Oct 3, 2023
@davidwengier davidwengier added area-compiler Umbrella for all compiler issues and removed Razor labels Oct 3, 2023
@alexrp
Copy link

alexrp commented Oct 12, 2023

I believe there is a related issue in MVC views. Using dotnet new mvc and altering Views/Shared/Error.cshtml to:

@using mvc.Models

@model ErrorViewModel

And Models/ErrorViewModel.cs to:

namespace mvc.Models;

internal sealed class ErrorViewModel
{
}

I get:

image

This despite the fact that the generated page class is internal (dotnet build works fine).

@chsienki chsienki added this to the 17.9 Planning milestone Oct 31, 2023
@ghost ghost removed the untriaged label Oct 31, 2023
@chsienki chsienki self-assigned this Oct 31, 2023
@chsienki chsienki modified the milestones: 17.10 Planning, Backlog Jul 11, 2024
@migig
Copy link

migig commented Aug 22, 2024

+1 Thank you to report this.

It is sad because like you say it is "best practice" to using internal in professional coding, but we must dirty the code just to keep this tool from making errors. This is very bad.

I hope this to be investigated and fixed - thanks guys!

@lonix1
Copy link
Author

lonix1 commented Aug 22, 2024

Yep... We use public for everything because of this.

Also we've a number of RP-based libraries and it's a huge mess. If one uses internal then it must be internal "all the way down" - and since we can't use it on some stuff (i.e. PageModels), it means everything must be public. Really frustrating.

PS: don't spin your wheels, there are no workarounds (I've tried!) - just change everything to public for now.

@chsienki chsienki modified the milestones: Backlog, 17.12 Planning Aug 22, 2024
@chsienki
Copy link
Contributor

We're actively working on getting this fixed. Stay tuned!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-compiler Umbrella for all compiler issues bug Something isn't working
Projects
None yet
Development

No branches or pull requests

7 participants