question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent behaviour with source generator results between unit testing and launch profile

See original GitHub issue

Version Used: Microsoft.CodeAnalysis.CSharp 4.2.0 Microsoft.CodeAnalysis.Analyzers 3.3.3 Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing.NUnit 1.1.1

Steps to Reproduce:

  1. Create net6.0 library project (Project A) with an attribute:
[AttributeUsage(AttributeTargets.Class, AllowMultiple = false)]
public sealed class TestAttAttribute : Attribute
{
    internal string Name { get; }

    public TestAttAttribute(string name)
    {
        Name = name;
    }
}
  1. Create another net6.0 project (Project B) with a class using the attribute:
[TestAtt("testString")]
public class Class1
{

}
  1. Create a source generator project in which we try to extract the attribute argument:
public void Execute(GeneratorExecutionContext context)
{
	// ....
    var attributeName = "TestAttAttribute";
    var attribute = classSymbol.GetAttributes().FirstOrDefault(a => a.AttributeClass.Name == attributeName);
    var attributeArgument = attribute?.ConstructorArguments[0].Value;  // "testString" 
}
  1. Create a test project that follows the recommended testing approach in the roslyn source generators cookbook. The unit test takes as an input only the file in Project B where Class1 is defined. The only difference with the cookbook is adding a reference in the test state to Project A, where the attribute resides:
TestState.AdditionalReferences.Add(typeof(TestAttAttribute).Assembly.Location);
  1. Setup “Roslyn Component” launch profile on the source generator project using Project B as the target project, in order to be able to debug the generation

Expected Behavior:

Debugging the source generator, both with the launch profile and the unit test (passing only the file with Class1 as input), I expect the variable attributeArgument to be "testString"

Actual Behavior:

When debugging with the launch profile attributeArgument = "testString" as expected. From the Immediate window we can see that ConstructorArguments has a length of 1:

8EF75736-428D-4B09-AF2A-0AC3A102265E

When debugging the unit test, instead, ConstructorArguments is empty. From the Immediate window we can see that ConstructorArguments has a length of 0:

5E7E4A02-8670-4A85-805A-EF78722251EF

Still while debugging the unit test it seems that the assembly of the attribute class is correctly recognised:

AD774C51-0248-4688-ACA0-FA0F4112E666

Is there anything that could cause this? Maybe I’m using the testing framework wrong? I have created a solution that reproduces the issue, I can eventually post it here if necessary.

Additional note I’ve noticed this while working on a much bigger source generator project. The result is the same, but in my original project when debugging the unit test I can see that the AttributeClass is of ErrorType, so I thought that this was the cause of the missing constructor arguments. This is the reason why I’ve made a separate solution trying to reproduce the issue. The issue is the same, but in this case AttributeClass is of type NamedType, so that seems to be correct.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
jaredparcommented, Aug 16, 2022

Thanks for the detailed repro @papafe. I hadn’t realized @Youssef1313 had already solved the issue and was debugging through your repro this morning. I came to the same conclusion as @youssef1313 did.

I went a slightly different route though. Generally when there is inconsistency like this it’s usually due to differing reference sets. I looked at context.Compilation.References under the debugger for both scenarios. Noticed in the unit tests that the main set of references were for netcoreapp3.1 while your lib was compiled for net6.0. As an experiment I changed your lib to target netcoreapp3.1 and verified that the error was fixed.

1reaction
Youssef1313commented, Aug 15, 2022

@papafe It looks like you’re not providing the source code of the attribute into the test input. I see now you do it in CSharpSourceGeneratorVerifier, sorry.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Live unit testing does not work with Source Generators
When starting Live Unit Testing it only discovers and runs tests from test project Stample.Working.Test. Tests from the project Sample.
Read more >
Unit Testing Source Generators - Build/Test Issues
Functional tests execute the source generator in roslyn during compilation. Units tests execute the source generator using Microsoft.CodeAnalysis.CSharp.
Read more >
Cannot debug net6.0-macos Apps - Developer Community
When I hit debug, the following error is shown in the Terminal: Possible reasons for this include: * You misspelled a built-in dotnet...
Read more >
Unit Test inconsistent pass or fail result
One of my unit tests seems to randomly pass or fail when I run it. The only thing that makes sense to me...
Read more >
Problems with .NET unit testing
We are continuously improving unit testing with JetBrains Rider, ... The following information is essential to determine the cause of the issue: Environment....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found