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.

[BUG] CS0436 compiler warning because of source generators when using InternalsVisibleTo

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the “Reporting a bug” section on Contributing file?

Current Behavior

The source generators of the CommunityToolkit.Maui generate compiler warnings in our solution.

We use the [assembly: InternalsVisibleTo(..] attribute so that we can access internal members within unit tests. But the source generators create the same internal classes in all projects. So the compiler warns about:

Warning CS0436: The type 'ColorAnimationExtensions_TimePicker' in 'CommunityToolkit.Maui.SourceGenerators\CommunityToolkit.Maui.SourceGenerators.Generators.TextColorToGenerator\TimePickerTextColorTo.g.shared.cs' conflicts with the imported type...

Complete warning text: [warning]D:\7\s\...\Foo.Test\CommunityToolkit.Maui.SourceGenerators\CommunityToolkit.Maui.SourceGenerators.Generators.TextColorToGenerator\TimePickerTextColorTo.g.shared.cs(63,89): Warning CS0436: The type 'ColorAnimationExtensions_TimePicker' in 'CommunityToolkit.Maui.SourceGenerators\CommunityToolkit.Maui.SourceGenerators.Generators.TextColorToGenerator\TimePickerTextColorTo.g.shared.cs' conflicts with the imported type 'ColorAnimationExtensions_TimePicker' in 'FooLib, Version=3.0.400.0, Culture=neutral, PublicKeyToken=null'. Using the type defined in 'CommunityToolkit.Maui.SourceGenerators\CommunityToolkit.Maui.SourceGenerators.Generators.TextColorToGenerator\TimePickerTextColorTo.g.shared.cs'.

Expected Behavior

no compiler warnings

Workaround

I have added <NoWarn>1701;1702;CS0436</NoWarn> to the unit test project file. This suppress the compiler warning.

Steps To Reproduce

  • Just add a C# project (e.g. unit test project) that references a project which includes the CommunityToolkti.Maui.
  • Add InternalsVisibleTo attribute so that the newly added project can access the internal members of the referenced project.
  • Build and see the compiler warnings

Link to public reproduction project repository

Environment

- .NET MAUI CommunityToolkit:5.0.0

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 4 months ago
  • Comments:11 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
VladislavAntonyukcommented, May 22, 2023

You should not include analyzers/source generators etc in test project:

<PackageReference Include="CommunityToolkit.Maui" Version="5.1.0">
	<PrivateAssets>all</PrivateAssets>
</PackageReference>

Exclude them to remove warnings: image

0reactions
pictoscommented, May 22, 2023

@omghb, What I posted isn’t a workaround, it’s how you can exclude SG from the build. Your sample project doesn’t reflect the reality of your project, since you just added a Maui app and Unit test, there are no external libs there… We can just provide options for what we see

Read more comments on GitHub >

github_iconTop Results From Across the Web

Solving the source generator 'marker attribute' problem
In this post I describe a problem I've been wrestling with around source generators: where to put the 'marker attributes' that drive the ......
Read more >
Why does the C# compiler generate CS0436 warnings for ...
I had the same problem because in the References folder there was a reference to the same project. ie: my project name was...
Read more >
Compiler Warning (level 2) CS0436
Using the type defined in 'assembly'. This warning is issued when a type in a source file (file_2) conflicts with an imported type...
Read more >
Question - Source Generator generates code multiple times
I was able to make my source generator work in Unity 2021.3, but there's one problem - it generates code multiple times -...
Read more >
StronglyTypedId.Attributes 1.0.0-beta04
Error CS0436 and [InternalsVisibleTo]. The StronglyTypedId generator automatically adds the [StronglyTypedId] attributes to your compilation ...
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