Add imports for child namespace references when moving types
Summary
Fixes Move to Namespace leaving partially qualified references broken when a moved type references a child namespace of its original namespace.
Problem
When Foo in namespace Tests references Inner.Data (meaning Tests.Inner.Data), moving Foo to Something.Else fails to compile because Inner is no longer in scope.
Expected: add using Tests.Inner; and simplify to Data.
Fix
In FixDeclarationDocumentAsync, scan the moving container for references whose containing namespace is a child of the old namespace and add the required imports before simplification.
Testing
- Added
MoveToNamespace_MoveType_AddsImportForChildNamespaceReferencetest for #50672 Microsoft.CodeAnalysis.CSharp.Featuresbuilds cleanly
Fixes #50672
Microsoft Reviewers: Open in CodeFlow
And oh: we should also add a test for VB since this code is shared across both. @KirtiRamchandani if that's not something you're familiar with I can take that bit.
A few questions but this is definitely on the right track. @KirtiRamchandani any chance you have some sense on whether we see any notable performance impact form this change? OK to just say "no". 😄
I think so it won't because this only runs on the moved container during Move to Namespace, not on normal editing paths, so yeah. :)
And oh: we should also add a test for VB since this code is shared across both. @KirtiRamchandani if that's not something you're familiar with I can take that bit.
Thank you I have given it a go, your feedback was really important especially because it was easy to understand. I have approached it the way you shared, fingers crossed it is good. Hehe.
Also we could skip special-casing child namespaces and instead speculatively apply the move, see what stops binding, and let the normal import fixer handle those spots, no?
The test failures do show failures in the new tests that were added.
@KirtiRamchandani Failures appear to be related to your change.
Finally all checks passed 🥹
@jasonmalinowski & @CyrusNajmabadi can you please have one more final review? Thank you very much for helping and guiding. I have squashed and made a single commit instead of having 7 as that looked like a little messy. Sorry again.
. I have squashed and made a single commit instead of having 7 as that looked like a little messy. Sorry again.
Thanks! While I appreciate the intent, that actually makes it much harder to review. As i cannot see what changed across versions. in the future, it would be appreciated if you did not do that once reviews have started. Thanks very much!
Thanks! While I appreciate the intent, that actually makes it much harder to review. As i cannot see what changed across versions. in the future, it would be appreciated if you did not do that once reviews have started. Thanks very much!
@CyrusNajmabadi Thank you and sorry I squashed in hurry to keep it neat and clean, I will make sure not to repeat it. Thank you for correcting.
@jasonmalinowski @CyrusNajmabadi , can we have last final review if this is looking good now to merge? Fingers crossed for lgtm :p
I think the right thing here may be to do the following:
When moving types, you move the existing imports, as well as synthesized imports for namespaces in scope (the oriignal containing namespaces from teh original code). You do this for all imports as long as a matching import is not already thre.
we then do a pass and ask which imports are unnecessary. and we remove any that are unnecessary that are also in the set that we added.
this way, we add only the imports we need to keep the moved code still binding.
I think that's all that is necessary here. Now there is no need to do any descendent walk/binding at all. The algorithm it simple and should almost always work out.
@jasonmalinowski what do you think of that algorithm? I think it also matches a bit how 'move type' already works when moving to a new file, so that the proper usings follow it. So this is just a small extension to that. Namely that we add imports for prior namespaces in scope so that types within those namespaces still bind.