Skip to content

Conversation

@jjonescz
Copy link
Member

@jjonescz jjonescz commented Mar 13, 2024

@jjonescz jjonescz added the area-compiler Umbrella for all compiler issues label Mar 13, 2024
@jjonescz jjonescz force-pushed the 1954771-EmptyNamespace branch 2 times, most recently from 4521fef to 114bac6 Compare March 13, 2024 16:11
@jjonescz jjonescz force-pushed the 1954771-EmptyNamespace branch from 114bac6 to 9de3c17 Compare March 13, 2024 16:36
public (string Type, string Namespace) GetNames()
=> _names ??= (_type.ToDisplayString(), _type.ContainingNamespace.ToDisplayString());
=> _names ??= (_type.ToDisplayString(),
_type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_type.ContainingNamespace.ToDisplayString() would be "<global namespace>"; with SymbolExtensions.FullNameTypeDisplayFormat we get an empty string instead (this format was already used in other places, just not everywhere)

#nullable disable

public static bool TryComputeNamespace(this RazorCodeDocument document, bool fallbackToRootNamespace, out string @namespace)
=> TryComputeNamespace(document, fallbackToRootNamespace: fallbackToRootNamespace, allowEmptyRootNamespace: false, out @namespace);
Copy link
Member Author

@jjonescz jjonescz Mar 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MVC scenarios still call this overload. They already kinda work - you can see that in the added test - they use a constant fallback namespace name, but do not append hash to class name, so users could be referencing them. Razor components in global namespace, on the other hand, used also fallback class name with appended hash so it would be nearly impossible for users to reference them.

@jjonescz jjonescz marked this pull request as ready for review March 14, 2024 08:00
@jjonescz jjonescz requested review from a team as code owners March 14, 2024 08:00
@davidwengier
Copy link
Member

Not sure how common this is, but I would expect that formatting of Razor components in the global namespace would not work. With on-type formatting specifically, editing these files could be a pretty annoying experience.

@jjonescz
Copy link
Member Author

I would expect that formatting of Razor components in the global namespace would not work.

Is there something I should change to make it work?

@davidwengier
Copy link
Member

I think expecting you to fix the formatting engine is unreasonable. Maybe if we're really lucky, then we'd just need to change the numbers in this line from 2 and 3 to 1 and 2:
https://github.com/dotnet/razor/blob/main/src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Formatting/FormattingSpan.cs#L52

I guess it would be nice to ensure there is some way we can find out if a particular Razor document is in the global namespace? Either a bool, or maybe a "base indent" we can use? That way at least we're set up for success. Personally I don't expect this will be too common that I'd block merging it on formatting being correct. Though having said that, I've no idea what the experience for users would actually be like :)

@jjonescz
Copy link
Member Author

jjonescz commented Mar 19, 2024

Oh, so the concern is about indentation? Components in the global namespace are actually still indented as if they were emitted inside namespace block: https://github.com/dotnet/razor/pull/10086/files#diff-38349d3b6effd9eb09b5b2da2152117586045b6e2171fda332b593a705269ea5

@davidwengier
Copy link
Member

That's how they're indented before we ask Roslyn to format the generated code, and then interpret the results.

</boo>
""");
""",
inGlobalNamespace: inGlobalNamespace);
Copy link
Member Author

@jjonescz jjonescz Mar 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidwengier

I've tried testing formatting on component in global namespace (debugged through it to verify the generated C# code really doesn't contain the namespace block). It works. But I'm not sure if this is the correct place or correct way to test this.

I've also tried this in VS - and the design-time doc is using namespace __GeneratedComponent (my guess is RootNamespace is set to null) which is wrong but at least formatting should work. I will try to see if that wrong namespace can be fixed as well (without breaking formatting).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the test is generating the code you expect, then great. Perhaps my fears are unfounded, or its only certain edge cases that would be affected. Either way, adding the infrastructure for us to test it, like you have, will be very valuable so thank you very much for that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up doing what you suggested to fix formatting. Thanks!

@jjonescz jjonescz marked this pull request as draft March 20, 2024 14:47
@jjonescz jjonescz marked this pull request as ready for review March 21, 2024 11:26
@jjonescz
Copy link
Member Author

@dotnet/razor-compiler @dotnet/razor-tooling this is ready for review; thanks

Copy link
Member

@davidwengier davidwengier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tooling LGTM. Thanks for the extra test coverage.

@jjonescz
Copy link
Member Author

jjonescz commented Apr 8, 2024

@dotnet/razor-compiler for reviews, thanks

Copy link
Member

@333fred 333fred left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler changes LGTM. Didn't review the IDE layer changes.

Comment on lines 118 to 121
var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat);
var fullName = string.IsNullOrEmpty(containingNamespace)
? type.Name
: $"{containingNamespace}.{type.Name}";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would prefer if we were more direct with the condition here:

Suggested change
var containingNamespace = type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat);
var fullName = string.IsNullOrEmpty(containingNamespace)
? type.Name
: $"{containingNamespace}.{type.Name}";
var fullName = type.ContainingNamespace.IsGlobalNamespace
? type.Name
: $"{type.ContainingNamespace.ToDisplayString(SymbolExtensions.FullNameTypeDisplayFormat)}.{type.Name}";

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible to have a component in a nested type? If so, will this handle that correctly?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible to have a component in a nested type? If so, will this handle that correctly?

I don't think it's possible. (You've already asked me this previously in #9689 (comment) :D)

In any case, the previous code did "{namespace}.{typeName}" and I've just changed it to omit the "{namespace}." prefix when needed, so nothing should change at this front, right?

@jjonescz jjonescz enabled auto-merge (squash) May 13, 2024 15:08
@jjonescz jjonescz merged commit f239b13 into dotnet:main May 13, 2024
@jjonescz jjonescz deleted the 1954771-EmptyNamespace branch May 13, 2024 15:26
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone May 13, 2024
@Cosifne Cosifne modified the milestones: Next, 17.11 P2 May 28, 2024
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

Successfully merging this pull request may close these issues.

WASM Blazor Error: RAZORGENERATE : error RZ3008: Tag helpers cannot target tag name '<global namespace>.

5 participants