Skip to content

Compiler errors when using IAsyncEnumerable<T> with .NET Standard 2.1 enabled #5799

@JesseTG

Description

@JesseTG

Describe the bug

Any project that uses IAsyncEnumerable<T> with ML-Agents imported and .NET Standard 2.1 enabled will cause compiler errors.

Context

The type System.Collections.Generic.IAsyncEnumerable<T> is available in both the included System.Interactive.Async assembly (which is used by Grpc.Core) and in .NET Standard 2.1. Since around Unity 2021.3, Unity includes IAsyncEnumerable<T> as part of its .NET Standard 2.1 support; as a consequence, the compiler can't decide whether to import IAsyncEnumerable<T> from netstandard or from System.Interactive.Async.

This issue probably occurs with other types in System.Interactive.Async, but I only checked this one.

To Reproduce

  1. Install ML-Agents
  2. Write or install any code that uses IAsyncEnumerable<T> (in my case, this package)
  3. Let the Unity editor try to compile your project.

Console logs / stack traces

It depends on the exact library, but here's an example from Razensoft.Mapper:

[CompilerError] The type 'IAsyncEnumerable<T>' exists in both 'System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263' and 'netstandard, Version=2.1.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51'
Compiler Error at Library\PackageCache\[email protected]\Runtime\Extensions\MapEnumerableAsync.cs:26 column 13
24:   public static async IAsyncEnumerable<TDestination> MapEnumerableAsync<TSource, TDestination>(
25:       this IMapper<TSource, TDestination> mapper,
-->26:       IAsyncEnumerable<TSource> source,
27:       [EnumeratorCancellation] CancellationToken cancellationToken = default)
28:       where TDestination : new()

Environment:

  • Unity Version: 2022.1.19f1
  • OS + version: Windows 10
  • ML-Agents version: 2.3.0-exp.2
  • Torch version: Not relevant
  • Environment: Not relevant

Proposed Fix

I see a couple of possible fixes:

  • Add the define constraint !NET_STANDARD_2_1 to the included System.Interactive.Async assembly. This way, Grpc.Core will use netstandard when available.
  • Disable auto-referencing in System.Interactive.Async. Grpc.Core can continue to use that library this way, if you don't mind pulling in redundant library code.

Workaround

I see two workarounds:

  • Modify ML-Agents with the aforementioned proposed fix. (This worked for me.)
  • Manually override references in any assembly definition that uses APIs offered by System.Interactive.Async, such that these asmdefs don't seek it out. (This also worked, but you'd have to do it for every offending assembly.)

Metadata

Metadata

Labels

bugIssue describes a potential bug in ml-agents.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions