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

Line pragma assertions fail in design time #10981

Closed
333fred opened this issue Oct 8, 2024 · 3 comments
Closed

Line pragma assertions fail in design time #10981

333fred opened this issue Oct 8, 2024 · 3 comments
Assignees
Labels
area-compiler Umbrella for all compiler issues

Comments

@333fred
Copy link
Member

333fred commented Oct 8, 2024

Consider this test:

    [IntegrationTestFact]
    public void LinePragmas()
    {
        var generated = CompileToCSharp("""
            @{
            <div>test</div>
            }
            @{
            <div>test</div>
            }
            """);

        AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
        AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
        CompileToAssembly(generated);
    }

After generating the baselines, this test will fail to validate line pragmas; no line pragmas will generate for any of the 4 source mappings in design time. Given our move to FUSE, I'm unsure whether this is actually important to solve, or whether it indicates that we may have a test gap in our FUSE generation. @chsienki for thoughts.

@333fred
Copy link
Member Author

333fred commented Oct 8, 2024

No line pragma found for code at line 1.
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.RazorBaselineIntegrationTestBase.AssertLinePragmas(RazorCodeDocument codeDocument) in /home/fred/git/razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorBaselineIntegrationTestBase.cs:line 171
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.RazorBaselineIntegrationTestBase.AssertCSharpDocumentMatchesBaseline(RazorCodeDocument codeDocument, Boolean verifyLinePragmas, String testName) in /home/fred/git/razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorBaselineIntegrationTestBase.cs:line 145
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.ComponentPreprocessorDirectiveTest.LinePragmas() in /home/fred/git/razor/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentPreprocessorDirectiveTest.cs:line 257
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

@333fred 333fred added the area-compiler Umbrella for all compiler issues label Oct 8, 2024
@333fred
Copy link
Member Author

333fred commented Oct 8, 2024

Another example (with the changes in my directives branch):

    [IntegrationTestFact]
    public void AfterTag()
    {
        var generated = CompileToCSharp("""
            @{
                <div>
            #if true
                <div>
            }
            @{
            #endif
            }
            """);

        AssertDocumentNodeMatchesBaseline(generated.CodeDocument);
        if (!DesignTime)
        {
            // https://github.com/dotnet/razor/issues/10981
            AssertCSharpDocumentMatchesBaseline(generated.CodeDocument);
        }
        CompileToAssembly(generated,
            // x:\dir\subdir\Test\TestComponent.cshtml(7,1): error CS1028: Unexpected preprocessor directive
            // #endif
            Diagnostic(ErrorCode.ERR_UnexpectedDirective, "#endif").WithLocation(7, 1));
    }
No line pragma found for code at line 1.
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.RazorBaselineIntegrationTestBase.AssertLinePragmas(RazorCodeDocument codeDocument) in /home/fred/git/razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorBaselineIntegrationTestBase.cs:line 171
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.RazorBaselineIntegrationTestBase.AssertCSharpDocumentMatchesBaseline(RazorCodeDocument codeDocument, Boolean verifyLinePragmas, String testName) in /home/fred/git/razor/src/Shared/Microsoft.AspNetCore.Razor.Test.Common/Language/IntegrationTests/RazorBaselineIntegrationTestBase.cs:line 145
   at Microsoft.AspNetCore.Razor.Language.IntegrationTests.ComponentPreprocessorDirectiveTest.AfterTag() in /home/fred/git/razor/src/Compiler/Microsoft.AspNetCore.Razor.Language/test/IntegrationTests/ComponentPreprocessorDirectiveTest.cs:line 136
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
   at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

@chsienki
Copy link
Contributor

chsienki commented Oct 8, 2024

@333fred Yep, I've come accross this before. We actually added the assertion as part of fuse to make sure we didn't miss anything and at the time it held true for design time. As we've added tests we've started seeing places it no longer holds for design time. I don't think its worth altering the design time code to fix it.

In #10907 I added a flag that lets you skip these asserts for design time:

// Design time writer doesn't correctly emit pragmas for missing tokens, so don't validate them in design time
AssertCSharpDocumentMatchesBaseline(generated.CodeDocument, verifyLinePragmas: !DesignTime);

@333fred 333fred closed this as completed Oct 8, 2024
333fred added a commit that referenced this issue Oct 11, 2024
* Add new preprocessor tests for the old parser, and copy their baselines over to the new parser for comparison.

* Add line whitespace tracking for erroring when a preprocessor token isn't the first thing on a line.

* Add initial support for parsing directives in the new lexer.

* Initial work on plumbing through parser changes to correctly support directives. Many more tests are required.

* Add more tests for other control flow blocks

* Several misc refactors

* Error on def and undef

* Start testing beginning-of-line enforcement.

* Report a warning when we see potential misplaced directives in disabled text.

* Add more tests, include the correct location in warnings.

* Usings, recomment GenerateBaselines

* Nullable annotations

* Update baselines, skip design time verification where tracked by #10981.

* Feedback.

* More feedback.
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
Projects
None yet
Development

No branches or pull requests

2 participants