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

Improve string handling support #7084

Closed
333fred opened this issue Aug 2, 2022 · 30 comments · Fixed by #11086
Closed

Improve string handling support #7084

333fred opened this issue Aug 2, 2022 · 30 comments · Fixed by #11086
Assignees
Labels
area-compiler Umbrella for all compiler issues area-parsing bug Something isn't working
Milestone

Comments

@333fred
Copy link
Member

333fred commented Aug 2, 2022

There are a few issues around strings in the razor compiler currently:

  1. Interpolated strings that have " characters in interpolation holes are not handled correctly.
  2. Raw string literals from C# 11 are not supported correctly.

/cc @DamianEdwards.

@333fred 333fred self-assigned this Aug 2, 2022
@333fred 333fred added bug Something isn't working area-compiler Umbrella for all compiler issues labels Aug 2, 2022
@333fred 333fred added this to the 17.4 milestone Aug 2, 2022
@chsienki chsienki transferred this issue from dotnet/razor-compiler Oct 28, 2022
@ghost ghost added the untriaged label Oct 28, 2022
@V0ldek
Copy link

V0ldek commented Nov 30, 2022

Raw string literals would be really useful for including code snippets in Blazor pages, currently you have to use verbatim strings and excape all " while also making sure indentation matches.

@TimPurdum
Copy link

This is really annoying, I expect C# features to work in razor files and don't want to keep track of what features aren't implemented, like raw string literals. Please update soon!

@chsienki chsienki added this to the Backlog milestone Jun 1, 2023
@ghost ghost removed the untriaged label Jun 1, 2023
@lofcz
Copy link

lofcz commented Jul 25, 2023

Do we have an update on this? Would be nice if this could make it in .NET 8 GA release.

@JohnyL
Copy link

JohnyL commented Aug 15, 2023

Any news on this?

@achselschweisz
Copy link

My current workaround for this issue (raw string literals) is to

  • use the code-behind approach for razor components
  • use some other class reference

This, however, adds more clutter to an existing project when all I need is a tiny file for a simple component...

@V0ldek
Copy link

V0ldek commented Aug 16, 2023

My current workaround for this issue (raw string literals) is to

  • use the code-behind approach for razor components
  • use some other class reference

This is unacceptable for my use case. The whole point of passing raw string literals as arguments to a component is readability, the fact that I can read the cshtml top to bottom and see the entire content.

At that point I might just as well use regular verbatim strings and escape everything.

@gragra33
Copy link

gragra33 commented Aug 19, 2023

@V0ldek @achselschweisz @JohnyL @lofcz @TimPurdum @333fred

You can use string literals in your razor (.Net 7) - see below:

@{
    string literal = $@"""Value {Value}""";
    <div>@literal</div>
}

or, you can inline it:

@{<div>@($@"""Value {Value}""")</div>}

Here is another working example:

@{<pre>@(@"
{
    ""Id"": null,
    ""FName"": ""create"",
    ""LName"": ""Denny"",
    ""DateofJoining"": null,
    ""Date"": ""2023-08-01T00:00:00"",
    ""CreatedBy"": ""ICC0000389"",
    ""levels"": {
        ""RoleId"": ""0"",
        ""RoleName"": ""select"",
        ""IsActive"": false,
        ""CreatedDate"": null,
        ""CreatedBy"": ""Admin""
    },
    ""functionalities"": [{
            ""levelID"": 0,
            ""levelName"": ""create""
        }
    ],
    ""functionalityList"": []
}")</pre>}

It may not be ideal, however, it's an extra 3 characters for a working solution until a better solution is available.

However, this fails:

@{<pre>@($$"""
           {
              "Id": null,
              "FName": "create",
              "LName": "Denny",
              "DateofJoining": null,
              "Date": "2023-08-01T00:00:00",
              "CreatedBy": "ICC0000389",
              "levels": {
                  "RoleId": "0",
                  "RoleName": "select",
                  "IsActive": false,
                  "CreatedDate": null,
                  "CreatedBy": "Admin"
              },
              "functionalities": [{
                      "levelID": 0,
                      "levelName": "create"
                  }
              ],
              "functionalityList": []
           }
           """
         )</pre>}

with the following compile error:

RZ1000	Unterminated string literal.  Strings that start with a quotation mark (") must be terminated before the end of the line.  However, strings that start with @ and a quotation mark (@") can span multiple lines.

@achselschweisz
Copy link

@gragra33 I think we all want raw string literals spread over several lines to work in razor components. All workarounds, including the one I posted myself, are basically suboptimal.
Having to duplicate quotes and whatnot defeats the purpose of this excellent C#11 feature, I am afraid.

@gragra33
Copy link

@achselschweisz I don't disagree however, for now, there is a working solution.

@V0ldek
Copy link

V0ldek commented Aug 25, 2023

@achselschweisz I don't disagree however, for now, there is a working solution.

Depends on what you mean by "solution". The problem here is that we want raw string literals, which are only materially different from other string tokens when they're multiline. Your comment is basically "but you can use normal strings just fine". Sure, not what we're asking for, though.

@cameronbell97
Copy link

Is there any idea on when this might be implemented? When this issue was opened the feature was still only in preview/rc .NET builds but we're fast approaching the 1-year mark of this feature being in the official latest .NET release. It would be great to have this available for razor files particularly for Blazor development

@PoseidonEnergy
Copy link

PoseidonEnergy commented Feb 2, 2024

I'm wondering if this is related to dotnet/aspnetcore#4976 - String interpolation with nested quotes breaks Razor blocks.

@333fred
Copy link
Member Author

333fred commented Feb 2, 2024

Correct. When we start taking advantage of Roslyn's lexer in Razor, that issue should also be resolved.

I know you all have been patiently waiting for progress here, and this is next on my list of issues to work on. I don't realistically expect that I'll get it in for 17.10, but we know how we're going to approach this now and I expect that it'll be in 17.11.

@Tragen
Copy link

Tragen commented Mar 1, 2024

When you star with Roslyn's lexer, will this also solve the problem that I cannot use underscores in numbers?
@page "/customer/{id:int:min(1):max(2_000_000_000)}"
this doesn't work.

@333fred
Copy link
Member Author

333fred commented Mar 3, 2024

Yes. My prototype also supports binary literals as well.

@codymullins
Copy link

I seem to be encountering some variation of this issue. With feedback from Egil Hanson and Peter Morris, I noticed you can get this "working" using four quotation marks:

<PureCode Language="@PureCode.Razor">
    @(""""
<PureBanner Accent="Accent.Brand">
   This is a branded banner.
</PureBanner>
"""")
</PureCode>

However, after changing the file a bit more, this breaks. Re-adding them one at a time seems to work temporarily.
Screenshot 2024-04-09 at 10 35 19 AM

@NoahS2003
Copy link

Looks like this issue is finally being resolved! Is there a release soon that this fix will be tied to?

@333fred
Copy link
Member Author

333fred commented Aug 21, 2024

We expect that you'll be able to opt-in to the new lexer during the initial 9.0 release. Based on feedback from that, we'll look at turning it on by default in a later SDK minor update.

333fred added a commit that referenced this issue Oct 25, 2024
This adds the new roslyn-based tokenizer, off by default, which uses
Roslyn, instead of a native tokenizer, to tokenize C# code. This is a
major change and will be enabled by putting
`<Features>use-roslyn-tokenizer</Features>` in the project file.

Closes #10737
Closes #10568
Closes #7084
@JohnyL
Copy link

JohnyL commented Nov 3, 2024

As of .NET 9 RC2, raw string literals do work, but... only in the case of separate model class.
If you use @functions, they do not work:

Image

@jjonescz
Copy link
Member

jjonescz commented Nov 4, 2024

This is an opt-in feature. You need to add something like <Features>use-roslyn-tokenizer=true</Features> to your csproj.

@JohnyL
Copy link

JohnyL commented Nov 4, 2024

@jjonescz I've added this line, but still same error.

Image

@jjonescz
Copy link
Member

jjonescz commented Nov 4, 2024

Sorry @JohnyL, it looks like the PR adding the new tokenizer didn't make it into .NET 9.0.1xx SDK, you could try a (not officially released) 9.0.2xx SDK from https://github.com/dotnet/sdk/blob/main/documentation/package-table.md

@333fred
Copy link
Member Author

333fred commented Nov 4, 2024

We will be posting a blog about this change and documenting how to enable it for testing soon; as Jan said, it didn't quite make 9.0.1xx, but will be available in 9.0.2xx. It'll be off by default first, as it's a large change and we want to make sure that it's working well for all users before turning it on by default.

@codymullins
Copy link

@jjonescz can this <Features>use-roslyn-tokenizer=true</Features> be added to Directory.build.props to enable in all projects?

@333fred
Copy link
Member Author

333fred commented Nov 5, 2024

Certainly.

@JohnyL
Copy link

JohnyL commented Nov 10, 2024

@jjonescz Thanks for a tip! Still doesn't work:

Image
Image

@333fred
Copy link
Member Author

333fred commented Nov 10, 2024

@JohnyL you will need VS 17.13p2 to see it live in the ide.

@JohnyL
Copy link

JohnyL commented Nov 10, 2024

@333fred Beg your pardon, but does such version exists for now? I've got 17.12.p5.

@333fred
Copy link
Member Author

333fred commented Nov 10, 2024

@333fred Beg your pardon, but does such version exists for now? I've got 17.12.p5.

Not yet, no. As I said, we'll have a blog post out with instructions on how to try it out when it's available.

@jjonescz
Copy link
Member

jjonescz commented Nov 11, 2024

@jjonescz Thanks for a tip! Still doesn't work:

Building the solution or running dotnet build from the command line should work with that SDK. But yeah, IDE will show errors as Fred says.

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 area-parsing bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.