Error CS0104: 'Guid' is an ambiguous reference between 'System.Guid' and 'System.Guid'

Viewed 1475

On one of my projects I have enabled ImplicitUsings (C# 10) feature. The project is an Sdk project multi-targeting .NET 4.8 Framework and .NET 6. As of yesterday my builds started failing on Azure DevOps with the following error:

Error CS0104: 'Guid' is an ambiguous reference between 'System.Guid' and 'System.Guid'

Even a build of a previously known-good commit (day before yesterday) is now failing. The pipeline hasn't changed. Inspecting the agent's image yields the same version (20220207.1). The problem doesn't reproduce locally on the latest Visual Studio 2022 (17.1).

The problem occurs in files that include using System, such as EF Core migration files. I probably could resolve the problem by removing these "unused" usings, but the problem will reappear whenever a new migration is created. The generated usings (obj\Debug\net48\Project.GlobalUsings.g.cs) declare the following using global using global::System;. Using Guid in other files don't produce this error.

Update. The saga continues. I'm now also getting a build failure on a .NET 6 project referencing other .NET Standard 2 projects. The error lies in a generated source file (DragonFruit) with the line using System.Threading.Tasks. Builds fine locally, but fails on Azure DevOps with this error:

D:\a\1\s\XXX\obj\Debug\net6.0\XXX.g.cs(8,31): error CS0104: 'Task<>' is an ambiguous reference between 'System.Threading.Tasks.Task' and 'System.Threading.Tasks.Task' [D:\a\1\s\Source\XXX.csproj]

What could've changed that these usings are now causing build failures? And how can I resolve this warning without having the manually update the migrations?

2 Answers

Posted on the Sonar Community:

FYI for those not following the Roslyn bug #60259, Microsoft have identified an issue in the compiler that could cause the observed behaviour in some multi-threaded scenarios.

Update: ... and the issue has been fixed. The fix is currently linked to the v17.3 release i.e. it is not yet available.

So Visual Studio 17.3 (and related tooling) will contain a fix for this.

It can be bypassed removing unnecessary using statements, in all your files. See: removing unnecessary using statements.

For some reason, the SonarCloud extension is adding this unexpected behavior.

Related