Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging a Source Generator when added to an xUnit testing project does not work #972

Open
JoanComasFdz opened this issue Mar 1, 2022 · 4 comments
Labels
Area-Source Generators SDK support for source generators untriaged

Comments

@JoanComasFdz
Copy link

JoanComasFdz commented Mar 1, 2022

Version Used: VS2022 17.1.0, xUnit project in .NET6, SourceGenerator project in NetStandard2.0

Steps to Reproduce:

  1. Create a blank solution
  2. Add a class library project targeting NetStandard2.0 for the Source Generator:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <Nullable>enable</Nullable>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
    <IsRoslynComponent>true</IsRoslynComponent>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.1.0" PrivateAssets="all" />
    <PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
  </ItemGroup>

  <ItemGroup>
    <None Include="$(OutputPath)\$(AssemblyName).dll" Pack="true" PackagePath="analyzers/dotnet/cs" Visible="false" />
  </ItemGroup>

</Project>
  1. Set as Startup project
  2. Add a Source Generator:
public class TheSourceGenerator : ISourceGenerator
    {
        public TheSourceGenerator()
        {
            Debug.WriteLine("Constructing TheSourceGenerator...");
        }
        public void Initialize(GeneratorInitializationContext context)
        {
            Debug.WriteLine("Initializing TheSourceGenerator...");
        }

        public void Execute(GeneratorExecutionContext context)
        {
            Debug.WriteLine("Executing TheSourceGenerator...");
        }
    }
  1. Add a breakpoint en each Debug line.
  2. Add an xUnit testing project targeting .NET6: referencing the Source Generator project
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <Nullable>enable</Nullable>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
    <PackageReference Include="xunit" Version="2.4.1" />
    <PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <PackageReference Include="coverlet.collector" Version="3.1.2">
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
      <PrivateAssets>all</PrivateAssets>
    </PackageReference>
    <ProjectReference
      Include="..\MySourceGenerator\MySourceGenerator.csproj"
      OutputItemType="Analyzer"
      ReferenceOutputAssembly="false"/>
  </ItemGroup>

</Project>
  1. Right click on the Source Generator project / Properties / Debug / Launch options UI.
  2. Delete profile
  3. Add Roslyn component
  4. Select tests project.
  5. Restart VS2022
  6. Select the Source Generator project in the debug dropdown.
  7. The Play button says "Profile1".
  8. Click Play button.
  9. A Console App is launched and closed.

Expected Behavior: Once the Console App is launched, the breakpoints are hit and the Source Generator project can be debugged.

Actual Behavior: The Console App is launched and closed. No breakpoint is hit. Debugging the unit tests does not hit the breakpoint either

Notes:
Yes, I have the SDK installed and can debug Source Generators for a Console App and a Blazor App with no issues.

Also I tried:

  • Specifying the Startup object in the xUnit project. Does not work.
  • Creating a Program class with a static void Main(string[] args) method and setting it as Startup object in the xUnit project. Does not work.

My end goal is to be able to add a source generator to a xUnit project that references another project (the project under test), and be able to analyze the types of the referenced project and create some extra classes.

@jinujoseph jinujoseph transferred this issue from dotnet/roslyn Mar 2, 2022
@sharwell sharwell added Area-Source Generators SDK support for source generators and removed Area-Interactive labels Mar 3, 2022
@JoanComasFdz
Copy link
Author

JoanComasFdz commented Mar 5, 2022

I tried adding the generated files to the source control as explained here, by adding the following lines to the xUnit project .csproj file:

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

And the folder and files are not being generated. So I guess the issue is not really debugging, but a lack of support for xUnit?

I am trying to make this work in this repo, maybe it helps identifying the issue:
https://github.com/JoanComasFdz/dotnet-scenario-unittesting

There, the project ExampleApplicationWithConsole.Tests is not a xUnit project, is just a regular Console Application with ".Tests" suffix in the name. When building it, the Generated folder with the generated files appear.

In contrast, in the project ExampleApplicationWithxUnit.Tests which is actually a xUnit project, building it does not generate the folder and files.

@sharwell
Copy link
Member

sharwell commented Mar 7, 2022

I've never noticed a problem debugging via the following steps:

  1. Create an xunit test project, and add a source generator test to the project
  2. Right click the test in Test Explorer, and click Debug Test(s)

In reviewing the steps by @JoanComasFdz above, I notice the following differences:

  • In Step 2, I do not have any of these lines in the source generator project:

    -<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    -<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
    -<IsRoslynComponent>true</IsRoslynComponent>
  • Omit Step 3 (do not set source generator as startup project)

  • Omit Steps 6-14. Instead, create an xunit test based on Microsoft.CodeAnalysis.Testing example here, and debug the test via Test Explorer.

@JoanComasFdz
Copy link
Author

JoanComasFdz commented Mar 14, 2022

Thanks for the feedback @sharwell.

Just to make sure we are talking about the same thing:

  • I am not trying to create and debug a unit test for my source generator.
  • I am trying to create and debug a source generator to be referenced by xUnit test projects.

I mention this because of your sentences "add a source generator test to the project" and "debug the test via Test Explorer".

Does the setup you propose allow to debug a source generator referenced in a xUnit project?

@sharwell
Copy link
Member

Yes, but debugging that source generator would involve creating a second test project specifically for testing (and not using) that source generator.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-Source Generators SDK support for source generators untriaged
Projects
None yet
Development

No branches or pull requests

2 participants