XamarinCommunityToolkit
XamarinCommunityToolkit copied to clipboard
[Bug] VS2019 Syntax/Code Highlighting for Custom Types (since v1.3.2)
Description
Since upgrading our NuGet packages over the years all our team member have the same issue: Sometimes our Visual Studio Code/Syntax highlighting isn't working. I've finally managed to reproduce this and track down what causes this: The NuGet package Xamarin.CommunityToolkit.
If I create a completely new (console) application everything works fine. I add a custom type and see that type highlighted (green for me) when I instantiate a variable/property with that type. See the image, in this case a custom enum (which is defined in CustomEnum.cs next to Program.cs).
But, as seen in the next image, as soon as I add NuGet package Xamarin.CommunityToolkit, the "CustomEnum" isn't green anymore.
If I remove the NuGet package, it works again.
Please note that "sometimes" it works for a short time. Then, all custom types have colour. But this colour is gone after "some" time, especially when scrolling.
This is just an example. We noticed this behaviour in our real Xamarin.Forms 5.0 application with Xamarin.CommunityToolkit and many other NuGet packages used. But, this is only the case for Xamarin.CommunityToolkit (if I remove all other packages, it still is not green and if I only remove Xamarin.CommunityToolkit it works).
I can not reproduce this in VS2022 but we need to be able to use VS2019. I've tested different versions:
- 1.3.0 -> It works, enum type is green.
- 1.3.1 -> It works, enum type is green.
- 1.3.2 -> It doesn't work, enum type is white (wrong).
- 2.0.0 -> It doesn't work, enum type is white (wrong).
Systems used:
- BAD: Windows 10 20H2, Visual Studio 2019 16.11.10, No Extensions enabled
- OK: Windows 10 20H2, Visual Studio 2022 17.1.0 Preview 3.0, No Extensions enabled
Steps to Reproduce
- Create a new (console, .NET5) application.
- Add a custom enum (CustomEnum).
- Add a property to the Program class that has the type of the added custom enum (CustomEnum).
- Add NuGetPackage Xamarin.CommunityToolkit v2.0.0.
Expected Behavior
The code/syntax highlighting must be correct for the type of the added property in Pogram.cs. It should be "green" (for dark mode, depending on colour settings).
Actual Behavior
The code/syntax highlighting isn't correct for the type of the added property in Program.cs. It's white/default coloured (for dark mode, depending on colour settings).
Basic Information
- Version with issue: 1.3.2, 2.0.0
- Last known good version: 1.3.1
- IDE: Visual Studio 2019 16.11.10
- Platform Target Frameworks: (None, Design-Time only)
- Nuget Packages: Xamarin.CommunityToolkit
- Affected Devices: Windows (Design-Time only)
Workaround
Only use Xamarin.CommunityToolkit <= v1.3.1
or use Visual Studio 2022.
Youi are right, I though I had broken some configuration when syntax highlighting worked differently between two of my projects. Downgrading XCT to 1.3.1 helped. Thank you!
@Countryen does it only show this behaviour after installing the package? If you close visual studio and reopen does it behave?
@pictos is there anyway the source generator could be upsetting VS? I know it struggles with new files being added like this sometimes
On the repro project used that shouldn't work (?) because Xamarin doesn't support net5, if you target netstandard do you get the same behavior?
@pictos is there anyway the source generator could be upsetting VS? I know it struggles with new files being added like this sometimes
Maybe... Could be some bug on VS tooling, I would say if that's the case this is more a VS or SourceGenerator bug than XCT bug.
@Countryen or @danielkraut could you attach a small repro?
@bijington @pictos Thanks for the replies. I want to add, that I don't think that not using the latest version should be promoted but currently that's just what is working (for both of us, it seems).
this is more a VS or SourceGenerator bug than XCT bug
Yes, but this is the only package that is affected and I can check but think some of our other packages already use SourceGenerators, too. I thought it's best to start here, but if you point me to the right place / forward this, I will be very grateful.
does it only show this behaviour after installing the package?
Yes. As seen in the picture above. I just go to my csproj-file and have the PackageReference commented out (so the package is not installed). At this time, the colours are correct. Then I go to the project file again and comment in the PackageReference (or install it via NuGet Package Manager) and save. VS updates and I can see the reference in the "References > Packages" almost immediately. As soon as I return to the Program.cs, the colours are gone.
If you close visual studio and reopen does it behave?
When I close VS and reopen - this is what happens (sometimes):
- It shows the colours
- It doesn't show the colours
- It does show the colours again
- When I scroll down -> The colours vanish
So it is kinda like blinking/flashing first. After the scrolling, the colours don't return until I reopen VS. It's the same every time and on multiple of our machines (all Windows 10).
To clarify, the problem occurs for my team in our library project which targets "netstandard2.0" (and is a dependency of our Xamarin.Forms 5.0 project using UWP/Android/etc.). Just for simplicity, I created a new console project to find the issue. There, the same behaviour is occuring (net5.0-windows).
I will attach a repo on ~Monday~ Tuesday for you.
Attached please find the reproducing sample. ConsoleApp2.zip (SHA-256: 49421ee9be551a64453ba71ab3f793a8b39521b3780f1cf43f574501e3275039)
- Download, extract and open the solution with Visual Studio 2019 (16.11.10).
- Go to Program.cs and make sure that the type name for "CustomEnum" is coloured correctly (depending on VS settings).
- Close Program.cs in VS.
- Go to ConsoleApp2 (project file) and uncomment this line
<!--<PackageReference Include="Xamarin.CommunityToolkit" Version="2.0.0" />-->
and save the file. (Or install via NuGet manually). - Compile/Build the project.
- Go to Program.cs again and make sure that the type name for "CustomEnum" isn't coloured anymore.
- Restart VS and observe the blinking/error.
Do the same with version 1.3.2 and 1.3.1 - with v1.3.1 it works correctly.
@Countryen as @pictos suggested this is expected to not work in a Console app. In fact I am surprised you are even allowed to add the package. Although I have tested and can confirm that VS/NuGet don't stop you. Admittedly it does report some issues though.
Following the behaviour you have mentioned in a clean Xamarin Forms application works fine here for me. I did hit some issues initially though... I had to update Xamarin.Forms (5.0.0.2337) to the latest before I could install the Xamarin.CommunityToolkit package.
Can you confirm if you are referencing the latest version of Xamarin.Forms or at least 5.0.0.2291? Also did you use the NuGet Package Manager inside Visual Studio to make the change to your projects or are you simply editing the project files directly?
I also encountered this issue with an existing project (just posted something in the discussion section on this very topic).
I already had the latest Xamarin.Forms (5.0.0.2337) when I installed the latest Xamarin.Community.Toolkit package (V2.0.0) when I noticed this issue as well. The project I have was building an app for Android as a target.
I'm running Visual Studio 2019 Professional (V16.11.10).
Perhaps it's a weird interaction with first installing Xamarin.Forms 5.0.0.2337 and then installing >= V1.3.2 of XCT?
EDIT: I did have the preview version for VS that allowed MAUI, including the preview of Visual Studio 2022, but wound up uninstalling everything except VS 2019.
@bijington I understand that it should not work in a console app project, that was just a simple example.
The behavior is the same when I add a new Xamarin.Forms project (blank app, only Android and UWP).
- I upgraded Xamarin.Forms to 5.0.0.2291 via NuGet Package Manager
- Added my CustomEnum.cs from before to the new shared project.
- Created the property as before in MainPage.xaml.cs instead of Program.cs.
- Colours are shown.
- Added the Xamarin.CommunityToolkit package v2.0.0 via NuGet Package Manager
- Colours are gone.
- When I downgrade the Xamarin.CommunityToolkit to v1.3.1 via PackageManager it works again.
I also have to highlight that I have the latest MAUI Preview installed (VS 22 Preview with .NET 6 Preview) and not removed it, yet. But @PrezSkroob has removed it. All of our team members have had one version of MAUI Preview installed once.
Maybe important to add: It only happens in the project that has the package reference. Even though the "UWP" project uses the "Shared" project as reference (includes it) and I can access the XCT stuff in the UWP project, here it works. It's only happening in the netstandard2.0 project for me.
(Note: I've created a separate CustomEnum in the UWP project for this).
I've now also tried using a different packages online, and I can't reproduce the error with other projects. I've tried this one: https://www.nuget.org/packages/AnyOf which uses a SourceGenerator, too.
When I checkout the tag 2.0.0 of this project and compile that on my machine, then use the compiled (Debug/Release) DLL file as reference instead of the NuGet Package everything is fine, it's only when I use the NuGet package.
Maybe it's related to installing MAUI and some weird conflict between the different Visual Studio versions, I will try with a machine that has not installed any MAUI preview so far.
It seems that it might be related to installing this toolkit package through the NuGet Package manager and having the preview version of MAUI installed, as that was my setup.
I'm in the process of getting a new SSD installed on my machine, and once I get Visual Studio re-installed, I will check the project I'm working with to see if the issue is still present.
Maybe important to add: It only happens in the project that has the package reference. Even though the "UWP" project uses the "Shared" project as reference (includes it) and I can access the XCT stuff in the UWP project, here it works. It's only happening in the netstandard2.0 project for me.
The project I'm working on also uses netstandard2.0, if that helps with duplicating/tracking this.
It seems that it might be related to installing this toolkit package through the NuGet Package manager and having the preview version of MAUI installed, as that was my setup.
I'm in the process of getting a new SSD installed on my machine, and once I get Visual Studio re-installed, I will check the project I'm working with to see if the issue is still present.
New SSD installed. Installed fresh installation of Visual Studio (first was Visual Studio 2022 due to having issues finding the 2019 installer, but that was eventually found and installed). Same issue occurs. Uninstalled Visual Studio 2022 and issue remains.
Really wish Visual Studio 2019 was found earlier because that may have helped more for finding the root cause of this issue.
When I checkout the tag 2.0.0 of this project and compile that on my machine, then use the compiled (Debug/Release) DLL file as reference instead of the NuGet Package everything is fine, it's only when I use the NuGet package.
Maybe it's related to installing MAUI and some weird conflict between the different Visual Studio versions, I will try with a machine that has not installed any MAUI preview so far.
Do we know if the binary image of the tagged V2.0.0 matches the binary image available through the NuGet package? I've encountered issues where changes were inadvertently made between tagged and released binaries (for other non-Visual Studio projects).
I've also managed to install the repro on a device without VS 2022 ever installed - same issue.
But, I first tried with my old version of VS 2019 (16.10.4) which worked! Then I update to the current version (16.11.10) and it stopped working.
So it could be a mix between XCT package and VS2021 new versions. I am not sure how to install/downgrade older versions of VS to test which version works and this was my private PC that I do not often use for development, so I will need to test further.
If you like @PrezSkroob, could you try downgrading or installing a previous version of VS to see for yourself?
I've also managed to install the repro on a device without VS 2022 ever installed - same issue.
But, I first tried with my old version of VS 2019 (16.10.4) which worked! Then I update to the current version (16.11.10) and it stopped working.
So it could be a mix between XCT package and VS2021 new versions. I am not sure how to install/downgrade older versions of VS to test which version works and this was my private PC that I do not often use for development, so I will need to test further.
If you like @PrezSkroob, could you try downgrading or installing a previous version of VS to see for yourself?
Very interesting. I will have to download an older version of Visual Studio and give it a try. If I can figure out how. During my reinstall, I was able to get the latest version (available here however, I haven't been successful in trying an older version.
I will look around and see if I can figure out how, and I'll post instructions here.
EDIT: Apparently, there are instructions here, and it also includes links to previous versions!
Uninstalling the current release
We update Visual Studio so that you get the latest bug fixes. If you need to go back to the previous release, you must uninstall your current installation and use the links below to revert your Visual Studio state. This article describes how to do so.
I haven't gotten around to trying to downgrade my version of Visual Studio, I did notice something interesting when using the pre-processor. (I'm using Visual Studio 2019 V16.11.11 now by the way)
In one of my source files, if I declare something like:
#define TESTING_SYNTAX_HIGHLIGHTING
#if TESTING_SYNTAX_HIGHLIGHTING
// insert various code here
#endif
Then the syntax/code highlighting works again for all code that is in the #if declaration. So somehow the pre-processor is able to correct whatever bug is causing it?
I too can confirm this issue. Installing any version of XCT after 1.3.1 causes class color highlighting to not work. It's an easy issue to reproduce for me. Just create a new Xamarin Forms App. Open the App.xaml.cs file and look at the code in the constructor.
Results without XCT installed OR XCT version 1.3.1 installed: WORKS
Results with XCT version 1.3.2 - 2.0.1 installed: NOT WORKING