roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Add imports for child namespace references when moving types

Open KirtiRamchandani opened this issue 5 months ago • 9 comments

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_AddsImportForChildNamespaceReference test for #50672
  • Microsoft.CodeAnalysis.CSharp.Features builds cleanly

Fixes #50672

Microsoft Reviewers: Open in CodeFlow

KirtiRamchandani avatar Jun 14 '26 09:06 KirtiRamchandani

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.

jasonmalinowski avatar Jun 16 '26 21:06 jasonmalinowski

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?

KirtiRamchandani avatar Jun 17 '26 00:06 KirtiRamchandani

The test failures do show failures in the new tests that were added.

jasonmalinowski avatar Jun 17 '26 18:06 jasonmalinowski

@KirtiRamchandani Failures appear to be related to your change.

jasonmalinowski avatar Jun 19 '26 18:06 jasonmalinowski

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.

KirtiRamchandani avatar Jun 20 '26 10:06 KirtiRamchandani

. 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!

CyrusNajmabadi avatar Jun 20 '26 10:06 CyrusNajmabadi

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

KirtiRamchandani avatar Jun 20 '26 12:06 KirtiRamchandani

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.

CyrusNajmabadi avatar Jun 23 '26 09:06 CyrusNajmabadi

@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.

CyrusNajmabadi avatar Jun 23 '26 10:06 CyrusNajmabadi