dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
19.09k stars 4.04k forks source link

Fix `Add import` is not suggested for extension methods defined in projects targeting a different framework than the project using the extension method #75991

Open just-ero opened 2 days ago

just-ero commented 2 days ago

Version Used

Compiler: 4.13.0-2.24565.3 (c267dc98)
.NET: 9.0.100
Visual Studio Code: 1.95.3, f1a4fb101478ce6ec82fe9627c43efbf9e98c813, x64
C# Extension: v2.57.28
C# Dev Kit Extension: v1.14.8

Steps to Reproduce

  1. Create or open a C# solution in Visual Studio Code (Visual Studio untested).
  2. Create two projects:
    • Project 1:
      <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
       <TargetFramework>net8.0</TargetFramework>
      </PropertyGroup>
      </Project>
    • Project 2:
      <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
       <TargetFramework>net9.0</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
       <ProjectReference Include="path/to/Project 1" />
      </ItemGroup>
      </Project>
  3. Add the following code wherever syntactically applicable in Project 1:

    namespace Project1;
    
    public class C;
    namespace Project1.Extensions;
    
    public static class CExtensions {
     public static void M(this C c) { }
    }
  4. Add the following code wherever syntactically applicable in Project 2:

    using Project1;
    
    namespace Project2;
    
    class Repro {
     void M(C c) {
       c.M();
     }
    }
  5. Attempt to apply Add import on c.M().

Expected Behavior

The fix is suggested.

Actual Behavior

It is not.

just-ero commented 2 days ago

Note: The extension method is picked up while typing (typing c.M will contain M from the extension namespace) and does add the required namespace upon accepting the suggestion.

However, this is also not always the case. I have not been able to figure out the conditions of this behavior.

CyrusNajmabadi commented 2 days ago

@genlu can you ptal.