dotnet / razor

Compiler and tooling experience for Razor ASP.NET Core apps in Visual Studio, Visual Studio for Mac, and VS Code.
https://asp.net
MIT License
493 stars 191 forks source link

The name 'context' does not exist in the current context #10512

Closed mayur-ekbote closed 2 months ago

mayur-ekbote commented 3 months ago

Is there an existing issue for this?

Describe the bug

The compiler does not see the context in the templated it component. (See the video). IntelliSense recognises everything correctly yet, the build fails.

https://github.com/dotnet/aspnetcore/assets/40038634/b559a51a-da3f-47bb-8f57-76dfabef5454

This is the same as dotnet/aspnetcore#9642 dotnet/aspnetcore#7638

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

8.0.302

Anything else?

.NET SDK: Version: 8.0.302 Commit: ef14e02af8 Workload version: 8.0.300-manifests.6cb9d875 MSBuild version: 17.10.4+10fbfbf2e

Runtime Environment: OS Name: Windows OS Version: 10.0.22631 OS Platform: Windows RID: win-x64 Base Path: C:\Program Files\dotnet\sdk\8.0.302\

jjonescz commented 3 months ago

@mayur-ekbote thanks for reporting this issue. Can you post minimal example code that reproduces the problem? I just tried a simple example like

@typeparam T

@Item @ItemTemplate(Item)

@functions{
    [Parameter]
    public T Item { get; set; }

    [Parameter]
    public RenderFragment<T> ItemTemplate { get; set; }
}
<Component1 Item="new { test = 42 }">
    <ItemTemplate Context="symbol">
        <b>@symbol.test</b>
    </ItemTemplate>
</Component1>

and it builds and runs fine.

mayur-ekbote commented 3 months ago

Thanks for replying. Of course, it works for the sample project for me too. But there is no good reason why IntelliSense should see everything clearly but the compiler doesn't. Clearly there is some issue with either VS not recognising the component (cleaning bin/obj doesn't help) of a specific conf somewhere in the project. Official doc only refers to C# cases, not Blazor related cases:

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-messages/cs0103

If you can help me with the circumstances in which compiler emits this error, I will be able to repro this.

jjonescz commented 3 months ago

If you can share the source code where the build error can be reproduced, we can investigate. Can be done privately via https://developercommunity.visualstudio.com/.

mayur-ekbote commented 3 months ago

Faced the same issue with some other component as well. When I opened csproj files, there were just many entries for no reason (I didn't make any of these changes).

  <ItemGroup>
    <Content Remove="SharedComponents\Elements\BsDropdown.razor" />
  </ItemGroup>
  <ItemGroup>
    <None Include="SharedComponents\Elements\BsDropdown.razor" />
  </ItemGroup>

    <ItemGroup>
      <UpToDateCheckInput Remove="SharedComponents\Elements\BsDropdown.razor" />
    </ItemGroup>
    <ItemGroup>
      <_ContentIncludedByDefault Remove="SharedComponents\Elements\BsDropdown.razor" />
    </ItemGroup>

VS mysteriously adds these entries - I had raised another issue - and my hunch is that both are related. #52533 @phil-allen-msft

jjonescz commented 3 months ago

I assume that if you remove these entries from the csproj, the build succeeds, right? The problem is then that Visual Studio is adding these entries. To investigate that it would help us to know what action in Visual Studio you perform that causes these to get added.

Also, what is your Visual Studio version?

mayur-ekbote commented 3 months ago

Yes, the build succeeds. I have found at least one specific condition in which this happens.

  1. Open your project.
  2. Add a new razor component. This will add those unwanted entries.
  3. Now, if you add any new components in the same session, those entries won't be added.

https://github.com/dotnet/razor/assets/40038634/a7933b8d-99bb-48ab-a674-f171ef540374

In summary, it happens to the very first component you add in a VS session. Subsequent components are added just fine. Clearly, there is no user setting that is resulting in this behaviour.

VS Version:

Microsoft Visual Studio Community 2022 Version 17.10.3 VisualStudio.17.Release/17.10.3+35013.160 Microsoft .NET Framework Version 4.8.09032

Installed Version: Community

Visual C++ 2022 00482-90000-00000-AA298 Microsoft Visual C++ 2022

ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools

ASA Service Provider 1.0

ASP.NET and Web Tools 17.10.341.11210 ASP.NET and Web Tools

Azure App Service Tools v3.0.0 17.10.341.11210 Azure App Service Tools v3.0.0

Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio

Azure Functions and Web Jobs Tools 17.10.341.11210 Azure Functions and Web Jobs Tools

Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio

C# Tools 4.10.0-3.24312.19+771f269b3abcbbd991f05becf8fe5e991d24b0c1 C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Cookiecutter 17.0.24064.1 Provides tools for finding, instantiating and customizing templates in cookiecutter format.

Extensibility Message Bus 1.4.39 (main@e8108eb) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub Copilot 0.2.889.30432 GitHub Copilot is an AI pair programmer that helps you write code faster and with less work.

Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query

Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics

Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects

Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Mono Debugging for Visual Studio 17.10.8 (a565b86) Support for debugging Mono processes with Visual Studio.

Node.js Tools 1.5.60329.1 Commit Hash:15fce86a17f9f33e5bd7ca7ccb9b216aff149252 Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager 6.10.1 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

Python - Django support 17.0.24064.1 Provides templates and integration for the Django web framework.

Python with Pylance 17.0.24064.1 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers.

Razor (ASP.NET Core) 17.10.3.2427201+4f57d1de251e654812adde201c0265a8ca7ca31d Provides languages services for ASP.NET Core Razor.

SQL Server Data Tools 17.10.172.0 Microsoft SQL Server Data Tools

ToolWindowHostedEditor 1.0 Hosting json editor into a tool window

TypeScript Tools 17.0.30327.2001 TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools 4.10.0-3.24312.19+771f269b3abcbbd991f05becf8fe5e991d24b0c1 Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual F# Tools 17.10.0-beta.24228.1+dd749058c91585e9b5dae62b0f8df892429ee28f Microsoft Visual F# Tools

Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio.

VisualStudio.DeviceLog 1.0 Information about my package

VisualStudio.Mac 1.0 Mac Extension for Visual Studio

VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info

Xamarin 17.10.0.110 (main@cf2e960) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer 17.10.3.10 (remotes/origin/d17-10@3beef58f89) Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates 17.9.0 (38e87ba) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

phil-allen-msft commented 2 months ago

This appears to be duplicate of https://developercommunity.visualstudio.com/t/New-Razor-components-are-being-excluded-/10677550; please follow it for updates.