dotnet / project-system

The .NET Project System for Visual Studio
MIT License
971 stars 388 forks source link

Unable to debug source generator: "The project doesn't know how to run the profile '<name>' with command 'DebugRoslynComponent'" #9547

Open IanKemp opened 1 month ago

IanKemp commented 1 month ago

Visual Studio Version

2022.17.11.4 (Community edition)

Summary

As title.

Suspect that the work done in https://github.com/dotnet/roslyn-sdk/pull/896 for https://github.com/dotnet/roslyn-sdk/issues/850 has either been reverted or broken.

Steps to Reproduce

  1. Clone https://github.com/riok/mapperly
  2. Attempt to debug source generator in Visual Studio by following https://mapperly.riok.app/docs/contributing/debugging/#integration-tests
  3. Get error image

Creating a new "Roslyn Component" launch profile is also not possible, as that option is missing from the "Launch Profiles" UI image

Expected Behavior

Source generator debugging works

Actual Behavior

Source generator debugging does not work

User Impact

Can't debug source generators

tmeschter commented 1 month ago

@IanKemp Do you have the ".NET Compiler Platform SDK" component installed in Visual Studio?

drewnoakes commented 1 month ago

I assume Ian worked this out based on https://github.com/dotnet/project-system/issues/9011#issuecomment-2395055168.

Ian, I'm taking a look at that issue and think I have a solution so that users are prompted to install this automatically once the IsRoslynComponent project property is set to true.

IanKemp commented 1 month ago

@IanKemp Do you have the ".NET Compiler Platform SDK" component installed in Visual Studio?

I do, and I could've sworn I posted a screenshot showing that.

drewnoakes commented 1 month ago

Confirmed that even with the .NET Compiler Platform SDK component installed, the launch profile fails to display in the editor, and the error is shown during run:

The project doesn't know how to run the profile with name 'IntegrationTests' and command 'DebugRoslynComponent'.

drewnoakes commented 1 month ago

Launch profiles having command name DebugRoslynComponent are handled by Roslyn.ComponentDebugger.DebugProfileProvider. The AppliesTo attribute on that MEF export means that the project must have the RoslynComponent ProjectCapability, however Riok.Mapperly doesn't appear to. Explicitly adding the capability fixes the issue, but that shouldn't be necessary.

Roslyn's Microsoft.Managed.Core.targets attempts to add the capability here in Microsoft.Managed.Core.targets, so that must not be part of evaluation (or is being overridden). I have some binlogs, but have a question for a colleague before continuing investigation.

I don't think this problem is specific to the Riok.Mapperly project/solution. I checked one of my own source generators, and that project also doesn't have the RoslynComponent project capability. I suspect something has changed in how we load targets for design-time builds.

drewnoakes commented 1 month ago

I don't think this problem is specific to the Riok.Mapperly project/solution. I checked one of my own source generators, and that project also doesn't have the RoslynComponent project capability. I suspect something has changed in how we load targets for design-time builds.

I was misunderstanding what I was seeing in the logs. Looking again today with fresh eyes, I see the problem,

Projects that contain source generators and/or analyzers (such as src/Riok.Mapperly/Riok.Mapperly.csproj) must specify the IsRoslynComponent property as true. i.e.:

<PropertyGroup>
  <IsRoslynComponent>true</IsRoslynComponent>
</PropertyGroup>

So I don't think there's a "bug" here. I'm not sure offhand how we could improve the experience though.

gentledepp commented 1 day ago

@drewnoakes you are my hero of the day! 🙂‍↕️