dotnet / vscode-csharp

Official C# support for Visual Studio Code
MIT License
2.86k stars 672 forks source link

CodeLens not appearing for empty classes #5466

Open DFVSQY opened 1 year ago

DFVSQY commented 1 year ago

Environment data

dotnet --info output:

.NET SDK (反映任何 global.json):
 Version:   6.0.300
 Commit:    8473146e7d

运行时环境:
 OS Name:     Windows
 OS Version:  10.0.17763
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\6.0.300\

global.json file:
  Not found

Host:
  Version:      6.0.10
  Architecture: x64
  Commit:       5a400c212a

.NET SDKs installed:
  2.2.401 [C:\Program Files\dotnet\sdk]
  3.1.403 [C:\Program Files\dotnet\sdk]
  5.0.413 [C:\Program Files\dotnet\sdk]
  6.0.300 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.All 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 2.2.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.30 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.9 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.30 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Download .NET:
  https://aka.ms/dotnet-download

Learn about .NET Runtimes and SDKs:
  https://aka.ms/dotnet/runtimes-sdk-info

VS Code version:

Version: 1.73.1 (system setup)
Commit: 6261075646f055b99068d3688932416f2346dd3b
Date: 2022-11-09T04:27:29.066Z
Electron: 19.0.17
Chromium: 102.0.5005.167
Node.js: 16.14.2
V8: 10.2.154.15-electron.0
OS: Windows_NT x64 10.0.17763
Sandboxed: No

C# Extension version: v1.25.2

OmniSharp log

Starting OmniSharp server at 2022/11/17 19:34:45
    Target: c:\Users\tengzhaoju\Desktop\Test

OmniSharp server started with .NET 6.0.300
.
    Path: c:\Users\tengzhaoju\.vscode\extensions\ms-dotnettools.csharp-1.25.2-win32-x64\.omnisharp\1.39.2-net6.0\OmniSharp.dll
    PID: 17736

[info]: OmniSharp.Stdio.Host
        Starting OmniSharp on Windows 10.0.17763.0 (x64)
[info]: OmniSharp.Services.DotNetCliService
        Checking the 'DOTNET_ROOT' environment variable to find a .NET SDK
[info]: OmniSharp.Services.DotNetCliService
        Using the 'dotnet' on the PATH.
[info]: OmniSharp.Services.DotNetCliService
        DotNetPath set to dotnet
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Located 1 MSBuild instance(s)
            1: .NET Core SDK 6.0.300 17.2.0 - "C:\Program Files\dotnet\sdk\6.0.300\"
[info]: OmniSharp.MSBuild.Discovery.MSBuildLocator
        Registered MSBuild instance: .NET Core SDK 6.0.300 17.2.0 - "C:\Program Files\dotnet\sdk\6.0.300\"
[info]: OmniSharp.WorkspaceInitializer
        Invoking Workspace Options Provider: OmniSharp.Roslyn.CSharp.Services.CSharpFormattingWorkspaceOptionsProvider, Order: 0
[info]: OmniSharp.Cake.CakeProjectSystem
        Detecting Cake files in 'c:\Users\tengzhaoju\Desktop\Test'.
[info]: OmniSharp.Cake.CakeProjectSystem
        Did not find any Cake files
[info]: OmniSharp.MSBuild.ProjectSystem
        No solution files found in 'c:\Users\tengzhaoju\Desktop\Test'
[info]: OmniSharp.MSBuild.ProjectManager
        Queue project update for 'c:\Users\tengzhaoju\Desktop\Test\Test.csproj'
[info]: OmniSharp.Script.ScriptProjectSystem
        Detecting CSX files in 'c:\Users\tengzhaoju\Desktop\Test'.
[info]: OmniSharp.Script.ScriptProjectSystem
        Did not find any CSX files
[info]: OmniSharp.WorkspaceInitializer
        Configuration finished.
[info]: OmniSharp.Stdio.Host
        Omnisharp server running using Stdio at location 'c:\Users\tengzhaoju\Desktop\Test' on host 6872.
[info]: OmniSharp.MSBuild.ProjectManager
        Loading project: c:\Users\tengzhaoju\Desktop\Test\Test.csproj
[info]: OmniSharp.MSBuild.ProjectManager
        Successfully loaded project file 'c:\Users\tengzhaoju\Desktop\Test\Test.csproj'.
[info]: OmniSharp.MSBuild.ProjectManager
        Adding project 'c:\Users\tengzhaoju\Desktop\Test\Test.csproj'
[info]: OmniSharp.MSBuild.ProjectManager
        Update project: Test
[info]: OmniSharp.Roslyn.CSharp.Services.Diagnostics.CSharpDiagnosticWorkerWithAnalyzers
        Solution initialized -> queue all documents for code analysis. Initial document count: 4.
Received response for /v2/getcodeactions but could not find request.

Steps to reproduce

This bug exists in v1.25.2 and v1.25.1, It works well in v1.25.0.

filipw commented 1 year ago

I suspect this is related to https://github.com/OmniSharp/omnisharp-vscode/pull/5400 because it refactored some stuff around codelens provider - cc @willl @dibarbet @JoeRobich I can see this happening for any new class (not even nested) just making class Foo {} does not trigger code lens (no outgoing request to server) until I, for example, add a constructor.

Nov-17-2022 16-40-28

leidegre commented 1 year ago

This is also a problem for any file containing any code which isn't detected as a test class.

This is fine

[TestClass]
public class ClassTest
{
  [TestMethod]
  public async Task MethodTest()
  {
  }
}

The code below doesn't work

[TestClass]
public class ClassTest
{
  [TestMethod]
  public async Task MethodTest()
  {
  }
}

[TestClass]
public class Class2Test
{
// because there's no test method here the test code lens stops showing
}

The code below doesn't work

[TestClass]
public class ClassTest
{
  // nested classes break code lens support as well
  // this is where we put test releated stuff
  class Utilties {} 

  [TestMethod]
  public async Task MethodTest()
  {
  }
}

I have to create additional files and spread out my test code to work around this issue. It's very problematic. Please make this a priority.

DFVSQY commented 1 year ago

@filipw This bug is important, please fix it quickly, thank you very much!

leidegre commented 1 year ago

Found out some more about this bizarre behavior.

Here we see code lens working

image

And if we add a nested class Z it is no longer working

image

But if we add a method W to the nested class Z it starts working again.

image

So apart from being really confusing this isn't that big of a deal if all you need to do is to add a method.

This patterns also appears to repeat itself. Code lens will stop working again if we add a another empty nested class and then start working again if we add a method to that empty class. It also doesn't matter where in the file this empty class is located. An empty class is what breaks the testing code lens. Adding anything to the class, field, method, constructor makes the testing code lens come back.

DFVSQY commented 1 year ago

Is there a plan to fix this bug? @filipw

leidegre commented 1 year ago

@DFVSQY FYI You can work around this by putting a static constructor that does nothing inside any member that doesn't have any other members.

But yes... this is annoying...