Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
JesseTG opened this issue Oct 11, 2022 · 5 comments · Fixed by #5820 or #5951
Closed

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

JesseTG opened this issue Oct 11, 2022 · 5 comments · Fixed by #5820 or #5951
Assignees
Labels
bug Issue describes a potential bug in ml-agents.

Comments

@JesseTG
Copy link
Contributor

JesseTG commented Oct 11, 2022

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.)
@JesseTG JesseTG added the bug Issue describes a potential bug in ml-agents. label Oct 11, 2022
@Eelam
Copy link

Eelam commented Oct 18, 2022

Did you try ML-Agents version: 2.2.1-exp.1

@JesseTG
Copy link
Contributor Author

JesseTG commented Oct 18, 2022

@Eelam Yes, the error persists.

@JesseTG
Copy link
Contributor Author

JesseTG commented Nov 7, 2022

Any thoughts on this? This is making it difficult for me to use ML-Agents.

@miguelalonsojr miguelalonsojr self-assigned this Dec 5, 2022
miguelalonsojr pushed a commit that referenced this issue Jun 6, 2023
miguelalonsojr added a commit that referenced this issue Jun 6, 2023
This reverts commit 7a3c26a.
miguelalonsojr added a commit that referenced this issue Jun 6, 2023
@georgeAlexNicolae
Copy link

Hi, I have the same error "The type 'IAsyncEnumerable' exists in both 'System.Interactive.Async, Version=3.0.1000.0, Culture=neutral, PublicKeyToken=94bc3704cddfc263' and 'netstandard, Version=2.1.0.0"
I tried the reactive framework, adding alias for System.Interactive.Async, but Unity just resets them afterwards back to global, I tried to decompile the dll and add that constraint but I failed, I even went and copy pasted your System.Interactive.Async.dll file into my place but that didn't work either.
For context, I also have a ML Agents game in Unity version 2022.2.6f1, and now I tried adding a chat gpt NPC to start the game with, using OpenAI-API-dotnet from OkGoDoIt. Thanks so much for all the help!

@JesseTG JesseTG mentioned this issue Jul 21, 2023
10 tasks
@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Issue describes a potential bug in ml-agents.
Projects
None yet
4 participants