8

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?

Bouke
  • 11,768
  • 7
  • 68
  • 102
  • Have you tried disabling implicit usings? Looks like they're somwhat causing the confusion. (Why this only happened as of yesterday, I don't know, though). – PMF Feb 18 '22 at 10:51
  • I'd need to reintroduce all implicit usings (make them explicit). Not something I fancy doing. Also I don't know why I would want to try that. I'm trying to figure out was caused this breakage and how to resolve it (if possible). – Bouke Feb 18 '22 at 12:18
  • Can you see which build is causing the issue, Net4.8 or Net6.0? This _is_ a weird thing, anyway, because `System.Guid` is always in a system dll and cannot come from a (potentially conflicting) reference. – PMF Feb 18 '22 at 13:27
  • 1
    For the multi-target build the .NET 4.8 build failed and .NET 6.0 succeeded. However another project that only targets .NET 6.0 has a similar build failure: `error CS0104: 'Task<>' is an ambiguous reference between 'System.Threading.Tasks.Task' and 'System.Threading.Tasks.Task'` – Bouke Feb 21 '22 at 10:46
  • Do you have any `` entries in your project files that are active when targeting .Net6.0? These should only be used for .NET framework builds. – PMF Feb 21 '22 at 12:06
  • There are no such references, the .NET 4.8 references are conditional on the target framework: ``. – Bouke Feb 21 '22 at 15:01
  • Could you please share your .csproj package references ? – Harshitha Veeramalla Feb 23 '22 at 03:56
  • @HarshithaVeeramalla-MT What are you looking for? The project is not open source and I'm inclined not to share any private code here. – Bouke Feb 28 '22 at 13:25
  • 1
    Some SonarCloud users have been having this problem too - https://community.sonarsource.com/t/timespan-is-an-ambiguous-reference-between-system-timespan-and-system-timespan/59245 – LeoD Mar 17 '22 at 21:39
  • possibly related to https://github.com/dotnet/roslyn/issues/59564 – Andrei Epure Mar 18 '22 at 15:31
  • @AndreiEpureishiring I don't think it is related. That issue is having to do with resolving ambiguity in types between namespaces. This is an apparent ambiguity in the of the same type in the same namespace. – Bouke Mar 22 '22 at 11:46
  • @LeoD interesting, this only happens with our Sonar build. – Bouke Mar 22 '22 at 11:47

3 Answers3

1

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.

Update: since updating to 17.3 I haven’t seen this error anymore.

Bouke
  • 11,768
  • 7
  • 68
  • 102
0

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.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
-1

Agree with Adrian and Wiliam...

The ambiguous references seems to be a result of a compiler bug or feature which gets confused when implicit using and unnecessary usings statements are used. May work for you, certain did for us.

SDK Default namespaces Microsoft.NET.Sdk

  • System
  • System.Collections.Generic
  • System.IO
  • System.Linq
  • System.Net.Http
  • System.Threading
  • System.Threading.Tasks

Microsoft.NET.Sdk.Web

  • System.Net.Http.Json
  • Microsoft.AspNetCore.Builder
  • Microsoft.AspNetCore.Hosting
  • Microsoft.AspNetCore.Http
  • Microsoft.AspNetCore.Routing
  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Logging

Microsoft.NET.Sdk.Worker

  • Microsoft.Extensions.Configuration
  • Microsoft.Extensions.DependencyInjection
  • Microsoft.Extensions.Hosting
  • Microsoft.Extensions.Logging
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
kirkpabk
  • 428
  • 4
  • 7