razor icon indicating copy to clipboard operation
razor copied to clipboard

Rename namespace results in extra spaces for @Using directive when moving .razor files

Open paul-schroeder-msft opened this issue 2 years ago • 11 comments

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:

  1. Create a new Blazor solution
  2. Locate a .razor file that has a "@using" directive at the top
  3. Add a new folder named 'Whatever'
  4. Move the .razor file from its current folder into the new folder
  5. 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 Razor file using directive gets extra spaces

paul-schroeder-msft avatar Sep 13 '22 23:09 paul-schroeder-msft

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

davidwengier avatar Sep 14 '22 00:09 davidwengier

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.

davidwengier avatar Sep 14 '22 00:09 davidwengier

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.

davidwengier avatar Sep 14 '22 07:09 davidwengier

Another report: https://developercommunity.visualstudio.com/t/In-Blazor-app-Razor-file-spaces-insert/1629654

davidwengier avatar Oct 09 '22 21:10 davidwengier

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

ryzngard avatar Mar 02 '23 21:03 ryzngard

Another report: https://developercommunity.visualstudio.com/t/Renaming-namespace-with-intellisense-sug/1669168

davidwengier avatar May 25 '23 22:05 davidwengier

it is still happening in current version ;/

d00lar avatar Jul 03 '24 09:07 d00lar

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

ryzngard avatar Jul 03 '24 20:07 ryzngard