Local packages break Visual Studio Refactoring features like "Move type to"

Whenever I’m developing a Unity package I use local packages as outlined in the documentation: Unity - Manual: Creating custom packages

Sometimes I want to extract a class into it’s own file using the Visual Studio refactoring quick action ‘Move type to … .cs’: Move type to matching file refactoring - Visual Studio (Windows) | Microsoft Learn

But when doing this on a class in a local package this fails. The stack trace shows

System.IO.DirectoryNotFoundException : Could not find a part of the path 'G:\Unity\com.company.my-package\projects\MyProject\Runtime\TestClass.cs'.

with a path that’s obviously wrong and does not point to the actual package location.

Does anyone know if there is any way around this? (I don’t really want to use an embedded package which doesn’t have this issue)

That’s a bit strange. I know that Unity does some shennanigans to modify paths to packages, so that might be what’s kicking in, see here.

That might be the problem, or it might be something else. This is probably an issue on Visual Studio’s side, I just tried in Rider, and the corresponding refactoring there (“Move to Another File”) works just as expected in a local package.

1 Like

Yeah, I was hoping this would count as a bug for the Visual Studio Editor package. At least the .csproj generation resolves the paths for the source files fine. But I guess since the csproj is inside the project directory, not the package directory, that’s what VS uses for these features.

Interesting that Rider does this correctly. Would be curious how a csproj looks like when using Rider (I assume these IDE integration packages are responsible for the generation).

But you’re right this is more likely a Visual Studio issue then. Still, if someone knows of a way to override something for VS to use the right path, let me know.

Nope, Unity does that! Unity doesn’t use them itself (though that will change in the future), but they’re generated for the use of IDEs. You can set what gets generated under Preferences/External tools.

That made me realize that that might be the issue. Open that and make sure that all the packages are checked in that list, and hit “Regenerate project files”. It might be that that makes the information VS needs be included.

Mine looks like this:
image

Enabling all of them didn’t help. Also tried with a Unity 6 preview version without success.