Skip to content

Unable to parse generic types with generic type arguments #728

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

Closed
thisisthekap opened this issue Sep 24, 2020 · 2 comments · Fixed by #729
Closed

Unable to parse generic types with generic type arguments #728

thisisthekap opened this issue Sep 24, 2020 · 2 comments · Fixed by #729
Labels
bug Component does not function as intended generator Issues binding a Java library (generator, class-parse, etc.)

Comments

@thisisthekap
Copy link

Reproduction hosted at https://github.com/thisisthekap/repro-ArgumentOutOfRangeException.

I am not entirely sure if this is the right project to post this issue. Please just tell me if this one should be posted elsewhere (e.g. at https://github.com/xamarin/xamarin-android).

Repro steps:

git clone https://github.com/thisisthekap/repro-ArgumentOutOfRangeException.git
cd repro-ArgumentOutOfRangeException
msbuild /t:Restore
cd purchases-core-common
msbuild /t:Build -verbosity:diagnostic

The build fails, now search the build output for System.ArgumentOutOfRangeException.

Exception shown in build output:

  [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Length cannot be less than zero. (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000: [ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentOutOfRangeException: Length cannot be less than zero. [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
  Parameter name: length (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000: Parameter name: length [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x00031] in <3f2977eb306b45388f224fc6cc4db2c4>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at System.String.Substring (System.Int32 startIndex, System.Int32 length) [0x00031] in <3f2977eb306b45388f224fc6cc4db2c4>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x0009c] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x0009c] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x000ca] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x000ca] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x00117] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerationOptions.GetOutputName (System.String s) [0x00117] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerationOptions.GetTypeReferenceName (MonoDroid.Generation.ReturnValue symbol) [0x00007] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerationOptions.GetTypeReferenceName (MonoDroid.Generation.ReturnValue symbol) [0x00007] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerator.WriteProperty (MonoDroid.Generation.Property property, MonoDroid.Generation.GenBase gen, System.String indent, System.Boolean with_callbacks, System.Boolean force_override) [0x00340] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerator.WriteProperty (MonoDroid.Generation.Property property, MonoDroid.Generation.GenBase gen, System.String indent, System.Boolean with_callbacks, System.Boolean force_override) [0x00340] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerator.WriteImplementedProperties (System.Collections.Generic.IEnumerable`1[T] targetProperties, System.String indent, System.Boolean isFinal, MonoDroid.Generation.GenBase gen) [0x00079] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerator.WriteImplementedProperties (System.Collections.Generic.IEnumerable`1[T] targetProperties, System.String indent, System.Boolean isFinal, MonoDroid.Generation.GenBase gen) [0x00079] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.CodeGenerator.WriteClass (MonoDroid.Generation.ClassGen class, System.String indent, MonoDroid.Generation.GenerationInfo gen_info) [0x004fa] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.CodeGenerator.WriteClass (MonoDroid.Generation.ClassGen class, System.String indent, MonoDroid.Generation.GenerationInfo gen_info) [0x004fa] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at MonoDroid.Generation.ClassGen.Generate (MonoDroid.Generation.CodeGenerationOptions opt, MonoDroid.Generation.GenerationInfo gen_info) [0x0007d] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at MonoDroid.Generation.ClassGen.Generate (MonoDroid.Generation.CodeGenerationOptions opt, MonoDroid.Generation.GenerationInfo gen_info) [0x0007d] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options, Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver) [0x005b6] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options, Java.Interop.Tools.Cecil.DirectoryAssemblyResolver resolver) [0x005b6] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options) [0x0001b] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at Xamarin.Android.Binder.CodeGenerator.Run (Xamarin.Android.Binder.CodeGeneratorOptions options) [0x0001b] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
    at Xamarin.Android.Binder.CodeGenerator.Main (System.String[] args) [0x0000c] in <598b9b8c977642d18bee713c178d3b71>:0  (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): message BG0000:   at Xamarin.Android.Binder.CodeGenerator.Main (System.String[] args) [0x0000c] in <598b9b8c977642d18bee713c178d3b71>:0  [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj] (TaskId:177)
/Library/Frameworks/Mono.framework/External/xbuild/Xamarin/Android/Xamarin.Android.Bindings.targets(372,5): error MSB6006: "generator.exe" exited with code 1. [/thepath/repro-ArgumentOutOfRangeException/purchases-core-common/purchases-core-common.csproj]
@jpobst
Copy link
Contributor

jpobst commented Sep 24, 2020

It looks like we are throwing the exception trying to parse generic types with generic types:

System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, System.Collections.Generic.IList<Kotlin.Pair>>

I will work on fixing our parser to support this.

The only workaround I can think of is to remove the methods that are trying to use this type:

<remove-node path="/api/package[@name='com.revenuecat.purchases.common']/class[@name='Backend']/method[@name='getCallbacks']" />
<remove-node path="/api/package[@name='com.revenuecat.purchases.common']/class[@name='Backend']/method[@name='setCallbacks']" />
<remove-node path="/api/package[@name='com.revenuecat.purchases.common']/class[@name='Backend']/method[@name='getPostReceiptCallbacks']" />
<remove-node path="/api/package[@name='com.revenuecat.purchases.common']/class[@name='Backend']/method[@name='setPostReceiptCallbacks']" />

@jpobst jpobst changed the title MSB6006: "generator.exe" exited with code 1: ArgumentOutOfRangeException "Length cannot be less than zero" Unable to parse generic types with generic type arguments Sep 24, 2020
@jpobst jpobst added bug Component does not function as intended generator Issues binding a Java library (generator, class-parse, etc.) labels Sep 24, 2020
@thisisthekap
Copy link
Author

@jpobst Thank you very much for the fast analysis!

Can you already provide a rough estimate when this issue is going to be fixed?

jonpryor pushed a commit that referenced this issue Sep 25, 2020
Fixes: #728

Context: https://xamarin.github.io/bugzilla-archives/12/12468/bug.html#c1

In order to avoid CS0234 errors, we always write non-intrinsic types
prefixed with `global::`.  For example, instead of

	System.Collections.Generic.List<Java.Lang.Object>

`generator` emits

	global::System.Collections.Generic.List<global::Java.Lang.Object>

To do this, we have to parse a type into its component types so we can
add `global::` to each part.  However, our parsing algorithm struggles
with generic types with multiple type arguments.

Thus, this:

	System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, Kotlin.Pair>

loses the second template parameter, becoming:

	global::System.Collections.Generic.IDictionary<global::System.Collections.Generic.IList<string>>

while this:

	System.Collections.Generic.IDictionary<System.Collections.Generic.IList<string>, System.Collections.Generic.IList<Kotlin.Pair>>

throws an `ArgumentOutOfRangeException`, which is the cause of #728.

Fix the `ArgumentOutOfRangeException` by introducing a new recursive
parser that better understands generic types.
jonpryor pushed a commit to dotnet/android that referenced this issue Oct 20, 2020
Fixes: dotnet/java-interop#461
Fixes: dotnet/java-interop#682
Fixes: dotnet/java-interop#717
Fixes: dotnet/java-interop#719
Fixes: dotnet/java-interop#728

Changes: dotnet/java-interop@ac914ce...b991bb8

  * dotnet/java-interop@b991bb86: [generator] Revert change to use auto-properties in EventArgs classes (#736)
  * dotnet/java-interop@ee50d89b: Bump to xamarin/xamarin-android-tools/master@f2af06f2 (#733)
  * dotnet/java-interop@a0b895c1: [build] Suppress NuGet warnings (#730)
  * dotnet/java-interop@8b1b0507: [generator] Fix parsing of complex generic types (#729)
  * dotnet/java-interop@ee7afeed: [generator] Prevent generating duplicate EventArgs classes (#726)
  * dotnet/java-interop@1f21f38c: [generator] Use GC.KeepAlive for reference type method parameters. (#725)
  * dotnet/java-interop@5136ef98: [Xamarin.Android.Tools.Bytecode] Hide Kotlin nested types inside (#723)
  * dotnet/java-interop@53d60513: [jnimarshalmethod-gen] Fix registration on Windows (#721)
  * dotnet/java-interop@5a834d42: [jnimarshalmethod-gen] Avoid creating AppDomains (#720)
  * dotnet/java-interop@a76edb8c: [Xamarin.Android.Tools.ApiXmlAdjuster] Find app.android.IntentService (#718)
  * dotnet/java-interop@6cde0877: [Java.Interop] Emit a reference assembly for Java.Interop.dll (#716)
  * dotnet/java-interop@b858dc59: [generator] Provide line/col numbers for api.xml warnings (#715)
  * dotnet/java-interop@9be92a04: [ci] Don't kick off CI for documentation only changes. (#712)
  * dotnet/java-interop@03c22722: [jnimarshalmethod-gen] Fix type resolution crash (#706)
@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Component does not function as intended generator Issues binding a Java library (generator, class-parse, etc.)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants