razor
razor copied to clipboard
Rename namespace results in extra spaces for @Using directive when moving .razor files
Describe the bug: When you move files (i.e. Blazor/razor files) into a subfolder in Visual Studio, it asks whether you would like to rename the file namespace to match the folder structure. If you answer "yes", then whatever does the adjustment adds an extra tab after the "@using" statement at the top making it “@ using” and leading to a compilation issue. There are also reports this will freeze on large projects when, for example, you move a large folder of files.
Version used: e.g. VS2022 17.3 or 17.4
To reproduce: Steps to reproduce the behavior:
- Create a new Blazor solution
- Locate a .razor file that has a "@using" directive at the top
- Add a new folder named 'Whatever'
- Move the .razor file from its current folder into the new folder
- Respond 'Yes' when Visual Studio asks if you want to adjust the namespace to match the folder name
Expected behavior: The namespace adjusts, but the space between the "@" and "using" remains as-is
Actual behavior: The namespace adjusts, but space is inserted between the "@" and "using", resulting in a compiler error.
Additional context:
See attached image
This was also reported in https://developercommunity.visualstudio.com/t/VS2022-171-Blazor-moving-razor-files-fr/10011429 and https://developercommunity.visualstudio.com/t/Refactoring-namespaces-corrupts-Razor-so/10041958
I can repro this in latest VS
Not sure if this is the same as https://github.com/dotnet/razor-tooling/issues/6167? Seems like some of the duplicated reports for that issue, are actually this issue.
So we're getting a didChange notification, coming from Roslyn which ultimately comes from Project System which gets its list of changes from Roslyn!
So the next step is to see whether Roslyn is making strange changes (adding whitespace when it should be changing a using statement???) or whether its our source mapping service (which would be called by Roslyn to translate the changes to the .g.cs file to the .razor file) doing something odd.
I suspect nothing else in our system is relying on didChange notifications for .razor files, so we could potentially just (somehow) tell Roslyn not to apply workspace changes to our mapped documents and avoid the issue entirely for now, but obviously fixing things is better, because actually having the using statement change namespace would be magical.
Another report: https://developercommunity.visualstudio.com/t/In-Blazor-app-Razor-file-spaces-insert/1629654
This is because of formatting done by Roslyn. Razor emits code like
// some global usings
namespace MyRazorApp
{
// @using System
using System; // note indentation (or lack thereof)
}
So when the formatting pass happens it indents the using statement
Another report: https://developercommunity.visualstudio.com/t/Renaming-namespace-with-intellisense-sug/1669168
it is still happening in current version ;/
Correct. I got prioritized with other work but hoping to fix this in the 17.12 time frame with https://github.com/dotnet/razor/pull/10334