Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

2.0 TagHelper error: A local or parameter named '__model' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter #1614

Closed
NTaylorMullen opened this issue Aug 14, 2017 · 16 comments
Assignees

Comments

@NTaylorMullen
Copy link
Contributor

From @GarageWeb on August 13, 2017 18:57

Starting with a default core 2.0 project, you get an error if you render a section from a layout in a view with an InputTagHelper. If you RenderBody in the layout instead, no issue.

Here is a super bare bones project illustrating the issue: https://github.com/GarageWeb/ModelErrorExample

error: A local or parameter named '__model' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

#pragma checksum "C:\Users\mrfle\source\repos\ModelErrorExample\ModelErrorExample\Views\Home\Index.cshtml" "{ff1816ec-aa5e-4d10-87f7-6f4963833460}" "1e27cfed358ff1c6a2d2bf68cb93678d6cdc3273"
// <auto-generated/>
#pragma warning disable 1591
[assembly:global::Microsoft.AspNetCore.Mvc.Razor.Compilation.RazorViewAttribute(@"/Views/Home/Index.cshtml", typeof(AspNetCore._Views_Home_Index_cshtml))]
namespace AspNetCore
{
    #line hidden
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Rendering;
    using Microsoft.AspNetCore.Mvc.ViewFeatures;
#line 1 "C:\Users\mrfle\source\repos\ModelErrorExample\ModelErrorExample\Views\_ViewImports.cshtml"
using ModelErrorExample;

#line default
#line hidden
#line 2 "C:\Users\mrfle\source\repos\ModelErrorExample\ModelErrorExample\Views\_ViewImports.cshtml"
using ModelErrorExample.Models;

#line default
#line hidden
    public class _Views_Home_Index_cshtml : global::Microsoft.AspNetCore.Mvc.Razor.RazorPage<PasswordNewViewData>
    {
        private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_0 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("action", new global::Microsoft.AspNetCore.Html.HtmlString("/"), global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
        private static readonly global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute __tagHelperAttribute_1 = new global::Microsoft.AspNetCore.Razor.TagHelpers.TagHelperAttribute("method", "post", global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
        #line hidden
        #pragma warning disable 0169
        private string __tagHelperStringValueBuffer;
        #pragma warning restore 0169
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperExecutionContext __tagHelperExecutionContext;
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner __tagHelperRunner = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperRunner();
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __backed__tagHelperScopeManager = null;
        private global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager __tagHelperScopeManager
        {
            get
            {
                if (__backed__tagHelperScopeManager == null)
                {
                    __backed__tagHelperScopeManager = new global::Microsoft.AspNetCore.Razor.Runtime.TagHelpers.TagHelperScopeManager(StartTagHelperWritingScope, EndTagHelperWritingScope);
                }
                return __backed__tagHelperScopeManager;
            }
        }
        private global::Microsoft.AspNetCore.Mvc.TagHelpers.FormTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_FormTagHelper;
        private global::Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_RenderAtEndOfFormTagHelper;
        private global::Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper __Microsoft_AspNetCore_Mvc_TagHelpers_InputTagHelper;
        #pragma warning disable 1998
        public async override global::System.Threading.Tasks.Task ExecuteAsync()
        {
#line 2 "C:\Users\mrfle\source\repos\ModelErrorExample\ModelErrorExample\Views\Home\Index.cshtml"
  
    Layout = "~/Views/Shared/_Layout_Minimal.cshtml";

#line default
#line hidden
            DefineSection("TestSection", async() => {
                BeginContext(111, 8, true);
                WriteLiteral(" \r\n\r\n   ");
                EndContext();
                BeginContext(119, 126, false);
                __tagHelperExecutionContext = __tagHelperScopeManager.Begin("form", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.StartTagAndEndTag, "678168d95b154e489a7e6e885b456a3a", async() => {
                    BeginContext(150, 9, true);
                    BeginContext(150, 9, true);
                    WriteLiteral("\r\n       ");
                    EndContext();
                    EndContext();
                    BeginContext(159, 32, false);
                    BeginContext(159, 32, false);
                    __tagHelperExecutionContext = __tagHelperScopeManager.Begin("input", global::Microsoft.AspNetCore.Razor.TagHelpers.TagMode.SelfClosing, "d5fa4ad0555f43318a097b11cb90a438", async() => {
                    }
                    );
                    __Microsoft_AspNetCore_Mvc_TagHelpers_InputTagHelper = CreateTagHelper<global::Microsoft.AspNetCore.Mvc.TagHelpers.InputTagHelper>();
                    __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_TagHelpers_InputTagHelper);
#line 8 "C:\Users\mrfle\source\repos\ModelErrorExample\ModelErrorExample\Views\Home\Index.cshtml"
__Microsoft_AspNetCore_Mvc_TagHelpers_InputTagHelper.For = ModelExpressionProvider.CreateModelExpression(ViewData, __model => ModelExpressionProvider.CreateModelExpression(ViewData, __model => __model.EmailAddress));

#line default
#line hidden
                    __tagHelperExecutionContext.AddTagHelperAttribute("asp-for", __Microsoft_AspNetCore_Mvc_TagHelpers_InputTagHelper.For, global::Microsoft.AspNetCore.Razor.TagHelpers.HtmlAttributeValueStyle.DoubleQuotes);
                    await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
                    if (!__tagHelperExecutionContext.Output.IsContentModified)
                    {
                        await __tagHelperExecutionContext.SetOutputContentAsync();
                    }
                    Write(__tagHelperExecutionContext.Output);
                    __tagHelperExecutionContext = __tagHelperScopeManager.End();
                    EndContext();
                    EndContext();
                    BeginContext(191, 47, true);
                    BeginContext(191, 47, true);
                    WriteLiteral("\r\n        <button type=\"submit\"></button>\r\n    ");
                    EndContext();
                    EndContext();
                }
                );
                __Microsoft_AspNetCore_Mvc_TagHelpers_FormTagHelper = CreateTagHelper<global::Microsoft.AspNetCore.Mvc.TagHelpers.FormTagHelper>();
                __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_TagHelpers_FormTagHelper);
                __Microsoft_AspNetCore_Mvc_TagHelpers_RenderAtEndOfFormTagHelper = CreateTagHelper<global::Microsoft.AspNetCore.Mvc.TagHelpers.RenderAtEndOfFormTagHelper>();
                __tagHelperExecutionContext.Add(__Microsoft_AspNetCore_Mvc_TagHelpers_RenderAtEndOfFormTagHelper);
                __tagHelperExecutionContext.AddHtmlAttribute(__tagHelperAttribute_0);
                __Microsoft_AspNetCore_Mvc_TagHelpers_FormTagHelper.Method = (string)__tagHelperAttribute_1.Value;
                __tagHelperExecutionContext.AddTagHelperAttribute(__tagHelperAttribute_1);
                await __tagHelperRunner.RunAsync(__tagHelperExecutionContext);
                if (!__tagHelperExecutionContext.Output.IsContentModified)
                {
                    await __tagHelperExecutionContext.SetOutputContentAsync();
                }
                Write(__tagHelperExecutionContext.Output);
                __tagHelperExecutionContext = __tagHelperScopeManager.End();
                EndContext();
                BeginContext(245, 4, true);
                WriteLiteral("\r\n\r\n");
                EndContext();
            }
            );
            BeginContext(252, 2, true);
            WriteLiteral("\r\n");
            EndContext();
        }
        #pragma warning restore 1998
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.ViewFeatures.IModelExpressionProvider ModelExpressionProvider { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.IUrlHelper Url { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.IViewComponentHelper Component { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.Rendering.IJsonHelper Json { get; private set; }
        [global::Microsoft.AspNetCore.Mvc.Razor.Internal.RazorInjectAttribute]
        public global::Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper<PasswordNewViewData> Html { get; private set; }
    }
}
#pragma warning restore 1591

Copied from original issue: aspnet/Mvc#6652

@NTaylorMullen
Copy link
Contributor Author

From @GarageWeb on August 14, 2017 0:39

Also, adding RenderBody() in addition to the section does not change the outcome.

@NTaylorMullen
Copy link
Contributor Author

From @pranavkm on August 14, 2017 16:16

cc @NTaylorMullen \ @ajaybhargavb

@NTaylorMullen
Copy link
Contributor Author

Investigating.

@NTaylorMullen
Copy link
Contributor Author

@GarageWeb thank you for the report. This is definitely a bug on our end!

@NTaylorMullen
Copy link
Contributor Author

After investigating; the reason why this occurs is due to when our ModelExpressionPass runs. It runs prior to our DirectiveRemovalOptimizationPass which results in it seeing the DirectiveIntermediateNode for the section directive AND the SectionIntermediateNode. Since both of those nodes have a node reference to the underlying ModelExpression, it prepends the ModelExpression logic twice.

@NTaylorMullen
Copy link
Contributor Author

@danroth27 could you include this in known 2.0.0 issues: "Using asp-for MVC TagHelpers in @sections breaks Razor compilation."

@NTaylorMullen NTaylorMullen self-assigned this Aug 14, 2017
@NTaylorMullen NTaylorMullen added this to the 2.1.0 milestone Aug 14, 2017
@rynowak
Copy link
Member

rynowak commented Aug 14, 2017

@Eilon - patch candiate?

Using asp-for MVC TagHelpers in @section breaks Razor compilation."

NTaylorMullen added a commit that referenced this issue Aug 14, 2017
- The `DirectiveRemovalPass` runs at `DefaultFeatureOrder + 50` in order to allow the design time directive tokens pass to consume directive intermediate nodes; however, this has a side effect of leaving around the original `@section` nodes a bit too long. The `ModelExpressionPass` would see two references to the sections body (`SectionIntermediateNode` and `DirectiveIntermediateNode`) and then do its work twice. To combat this behavior I bumped the ModelExpressionPass order to be 50 points higher than the directive removal pass.
- Added MVC tests for current and 1_X extensions.

#1614
@Eilon
Copy link
Member

Eilon commented Aug 15, 2017

@rynowak definitely.

@Eilon
Copy link
Member

Eilon commented Aug 15, 2017

Logged #1618 to track porting to a 2.0.1 patch.

NTaylorMullen added a commit that referenced this issue Aug 16, 2017
- Changed `SectionDirectivePass` to move non-token body nodes from the original `DirectiveIntermediateNode` to the `SectionIntermediateNode`. By doing this there's no longer dual references of `SectionIntermediateNode` bodies.
- Added MVC tests for current and 1_X extensions.

#1614
NTaylorMullen added a commit that referenced this issue Aug 16, 2017
- Changed `SectionDirectivePass` to move non-token body nodes from the original `DirectiveIntermediateNode` to the `SectionIntermediateNode`. By doing this there's no longer dual references of `SectionIntermediateNode` bodies.
- Added MVC tests for current and 1_X extensions.

#1614
NTaylorMullen added a commit that referenced this issue Aug 16, 2017
- Changed `SectionDirectivePass` to move non-token body nodes from the original `DirectiveIntermediateNode` to the `SectionIntermediateNode`. By doing this there's no longer dual references of `SectionIntermediateNode` bodies.
- Added MVC tests for current and 1_X extensions.

#1614
@NTaylorMullen
Copy link
Contributor Author

db805eb

@Gaelmart
Copy link

Is there a solution for this or do I abandon all work for 6 months and go on welfare.?

@Eilon
Copy link
Member

Eilon commented Aug 23, 2017

@Gaelmart we have it fixed in the 2.1.0 pre-release branch, and we're also working on fixing this in the 2.0.1 patch.

@Gaelmart
Copy link

How do I get the 2.1.0 pre-release branch?

@Eilon
Copy link
Member

Eilon commented Aug 23, 2017

@Gaelmart the dev branch (default branch) is the 2.1.0 pre-release branch. More info on using the latest builds is here: https://github.com/aspnet/Home#builds

@poke
Copy link

poke commented Sep 1, 2017

Is there any ETA on 2.0.1? I cannot switch to the MyGet feeds for this, and this issue is breaking the 2.0 upgrade for me right now.

@Eilon
Copy link
Member

Eilon commented Sep 1, 2017

@poke no ETA right now; we're wrapping up our work on the 1.0.x/1.1.x patches, and then we'll switch over to the 2.0.x patches. I'll post an update to the 2.0.1 issue at #1618 when we have a feed that has those pre-release bits.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants