Visual Studio "sync namespaces" doesn't update ViewImports nor Blazor Imports files
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)
@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?
Will take a look and report back, but I don't remember doing anything special for razor when implementing this
@genlu don't suppose you got a chance to look at this?
No pressure, it just came up in weekly triage.
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?
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
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.
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?
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?
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 :)
Also reported in https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1522519
More reports:
- https://developercommunity.visualstudio.com/t/Refactoring-namespaces-corrupts-Razor-so/10041958
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 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