lucenenet icon indicating copy to clipboard operation
lucenenet copied to clipboard

Lucene.Net.Util.SystemConsole throws not supported exception in .NET MAUI app running on android/iOS

Open JayOfemi opened this issue 1 year ago • 0 comments

Is there an existing issue for this?

  • [X] I have searched the existing issues

Describe the bug

I am simply trying to create a StandardQueryParser. I get the below exception only on Android and iOS. This makes sense as System.Console.In is not supported on these platforms, but I have no need for it in the context of our application. So, I think a way to ignore this would be very useful to prevent our app from crashing on these platforms.

Note that I am testing on Android and iOS in Android Studio and XCode respectively, not on physical devices. Though, I don't think this will make a difference.

It seems the issue is the way these properties are setup in Lucene.Net.Util.SystemConsole.

public static TextWriter Out { get; set; } = Console.Out;
public static TextWriter Error { get; set; } = Console.Error;
public static TextReader In { get; set; } = Console.In;

My application would work fine to simply return null here when Console.In is not supported.

I can't seem to find anything online about this regarding Lucene, so I think it is just specific to Lucene.NET and .NET MAUI. In any case, I believe it can possibly be addressed simply by doing the above.

From what I can tell, SystemConsole is only used in StandardSyntaxParserTokenManager to create the private debugStream property, which is then never used in this class. I believe this whole thing is the same when using the default QueryParser as well.

Expected Behavior

Perhaps instead of defaulting to Console.In, the property can be set in a way that if a non-supported or any other exceptions that can be ignored is caught, this property returns null or a bogus TextReader by default.

Steps To Reproduce

  • In a .NET MAUI sample app, create a Lucene.Net.QueryParsers.Flexible.Standard.StandardQueryParser using the constructor.
    • Specifically we created our MAUI app using the default template in Rider.
  • Run the app. on Windows and macOS, this will run fine. On Android and iOS, the app will crash with a System.PlatformNotSupportedException when it attempts to construct the StandardQueryParser.

Exceptions (if any)

Exception: System.TypeInitializationException: The type initializer for 'Lucene.Net.Util.SystemConsole' threw an exception.
 ---> System.PlatformNotSupportedException: Operation is not supported on this platform.
   at System.ConsolePal.GetOrCreateReader()
   at System.Console.<get_In>g__EnsureInitialized|14_0()
   at System.Console.get_In()
2024-04-29 15:55:32.051142-0700  at Lucene.Net.Util.SystemConsole..cctor()
   --- End of inner exception stack trace ---
   at Lucene.Net.QueryParsers.Flexible.Standard.Parser.StandardSyntaxParserTokenManager..ctor(ICharStream stream)
2024-04-29 15:55:32.051200-0700  at Lucene.Net.QueryParsers.Flexible.Standard.Parser.StandardSyntaxParser..ctor(ICharStream stream)
   at Lucene.Net.QueryParsers.Flexible.Standard.Parser.StandardSyntaxParser..ctor()
   at Lucene.Net.QueryParsers.Flexible.Standard.StandardQueryParser..ctor()
2024-04-29 15:55:32.051439-0700 at Lucene.Net.QueryParsers.Flexible.Standard.StandardQueryParser..ctor(Analyzer analyzer)

Lucene.NET Version

4.8

.NET Version

8.0.203

Operating System

Windows 11

Anything else?

No response

JayOfemi avatar May 01 '24 18:05 JayOfemi