Closed papafe closed 2 years ago
Do you have a repro project published anywhere that we can look at?
@jaredpar sure, I've just created a repo: https://github.com/papafe/source-gen-attribute. To reproduce, add a breakpoint here:
If you debug the single unit test in UnitTest1.cs
(project TestProject
), you can see that that line will throw an exception.
Instead if you debug the source generator using the "Roslyn component" profile (called Profile1
), there's no exception.
The solution should be working as-is, but let me know if you need additional info
@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.
@Youssef1313 I am adding a reference to the assembly containing the attribute in the CSharpSourceGeneratorVerifier
here:
@papafe You need to add ReferenceAssemblies = ReferenceAssemblies.Net.Net60;
in Test
constructor (below or above the line TestState.AdditionalReferences.Add(typeof(TestAttAttribute).Assembly.Location);
)
The assembly that contains TestAttAttribute
that you're adding is .NET 6. So, previously the compilation had this error:
error CS1705: Assembly 'MainLibrary' with identity 'MainLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' uses 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' which has a higher version than referenced assembly 'System.Runtime' with identity 'System.Runtime, Version=4.2.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
@Youssef1313 It works, thanks! For future reference, what do you mean by "previously the compilation had this error"? Is there a way to access the error from the verifier?
@papafe While debugging, you can see compilation errors via context.Compilation.GetDiagnostics()
(context
is the GeneratorExecutionContext
in Execute
method).
@Youssef1313 Thank you! That's definitely good to know 😄
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.
@jaredpar Thanks for the tip! It's definitely good to know
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:
Create net6.0 library project (Project A) with an attribute:
Create another net6.0 project (Project B) with a class using the attribute:
}
Class1
is defined. The only difference with the cookbook is adding a reference in the test state to Project A, where the attribute resides: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 variableattributeArgument
to be"testString"
Actual Behavior:
When debugging with the launch profile
attributeArgument = "testString"
as expected. From the Immediate window we can see thatConstructorArguments
has a length of 1:When debugging the unit test, instead,
ConstructorArguments
is empty. From the Immediate window we can see thatConstructorArguments
has a length of 0:Still while debugging the unit test it seems that the assembly of the attribute class is correctly recognised:
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 ofErrorType
, 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 caseAttributeClass
is of typeNamedType
, so that seems to be correct.