razor icon indicating copy to clipboard operation
razor copied to clipboard

Visual Studio "sync namespaces" doesn't update ViewImports nor Blazor Imports files

Open vsfeedback opened this issue 3 years ago • 13 comments

This issue has been moved from a ticket on Developer Community.


When using the sync namespaces Visual Studio tool, it doesn't updates ViewImports.cshtml (ASP.NET Razor) nor Blazor Imports.razor


Original Comments

Feedback Bot on 3/3/2022, 06:23 PM:

We have directed your feedback to the appropriate engineering team for further evaluation. The team will review the feedback and notify you about the next steps.


Original Solutions

(no solutions)

vsfeedback avatar Mar 10 '22 22:03 vsfeedback

@genlu We have services in the Roslyn workspace that enable C# to map Razor content appropriately (including usings). Is there something perhaps missing on the C# front here or is this entirely in Razor's ballpark?

NTaylorMullen avatar Mar 10 '22 22:03 NTaylorMullen

Will take a look and report back, but I don't remember doing anything special for razor when implementing this

genlu avatar Mar 11 '22 18:03 genlu

@genlu don't suppose you got a chance to look at this?

No pressure, it just came up in weekly triage.

davidwengier avatar Mar 17 '22 21:03 davidwengier

Looks like we found the reference to the moved type in a .g.cs file and tries to add an import to the new namespace directly https://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs,657

Should we use the service you mentioned to handle it instead? how do I do that?

genlu avatar Mar 17 '22 22:03 genlu

Looks like we found the reference to the moved type in a .g.cs file and tries to add an import to the new namespace directly https://sourceroslyn.io/#Microsoft.CodeAnalysis.Features/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs,657

Should we use the service you mentioned to handle it instead? how do I do that?

oooo it tries to add the import not mutate an existing one? If we're in the business of adding new imports then we'd probably need a new API to handle that

NTaylorMullen avatar Mar 18 '22 17:03 NTaylorMullen

yea, also we just rely on the simplifier to remove the old using here if necessary, I'm not sure if this part work correctly in razor either.

genlu avatar Mar 18 '22 19:03 genlu

yea, also we just rely on the simplifier to remove the old using here if necessary, I'm not sure if this part work correctly in razor either.

Ya it definitely wouldn't. Is there a precedent at all for this in other languages or would this be new API territory?

NTaylorMullen avatar Mar 18 '22 21:03 NTaylorMullen

Is there a precedent at all for this in other languages

You mean simplifier working with non-CS/VB document? I don't think so.

Quick question: If we find a reference to moved type in a g.cs file from razor and added a using in that document, I assumed it will eventually be dropped when we actually apply that change in workspace, right? (since it's generator therefore non-editable). Is there a service that can detect such action at this step and map the change in .g.cs to corresponding razor content?

genlu avatar Mar 25 '22 00:03 genlu

Is there a service that can detect such action at this step and map the change in .g.cs to corresponding razor content?

Not today. We typically can only map "mappable" ranges where "mappable" is defined by edits influencing a #line based section. In the using scenario it's trying to add them to non-#line based areas so they eventually get dropped. We'll need to make that service :)

NTaylorMullen avatar Mar 25 '22 00:03 NTaylorMullen

Also reported in https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1522519

davidwengier avatar Apr 14 '22 21:04 davidwengier

More reports:

  • https://developercommunity.visualstudio.com/t/Refactoring-namespaces-corrupts-Razor-so/10041958

NTaylorMullen avatar May 19 '22 21:05 NTaylorMullen

Looks like we found the reference to the moved type in a .g.cs file and tries to add an import to the new namespace directly sourceroslyn.io/#Microsoft.CodeAnalysis.Features/CodeRefactorings/SyncNamespace/AbstractChangeNamespaceService.cs,657 Should we use the service you mentioned to handle it instead? how do I do that?

oooo it tries to add the import not mutate an existing one? If we're in the business of adding new imports then we'd probably need a new API to handle that

Looking at this, there are multiple reasons we can't just mutate the import. If the namespace is used for something else, for example, or if conflicts are introduced. Unfortunately it's a very complex thing. The way XAML handles this is through listening to IVSRefactorNotify, iirc. Since razor is a language server I doubt it would be possible. Are there ways for a client to notify a server of symbolic changes?

ryzngard avatar Aug 09 '22 20:08 ryzngard

@ryzngard not by default. We'd need to hook up that understanding to notify our language server 😢. That being said I did prototype an idea for inherently understanding it in our language server which I found kind of interesting. See the __SymbolHelpers code snippet: https://github.com/NTaylorMullen/LSPVirtualDocuments/blob/master/Documents/EmbeddedLanguageLSPExpansion.md#additiveInteractionExample

NTaylorMullen avatar Aug 10 '22 17:08 NTaylorMullen