-
Notifications
You must be signed in to change notification settings - Fork 30
[Xamarin.Android.Tools.AndroidSdk] Attributes can be null! #158
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
Conversation
Context: dotnet#157 Context: 2d3690e Commit 2d3690e added Nullable Reference Type support to `Xamarin.Android.Tools.AndroidSdk.dll`. Unfortunately, it was largely "surface level", updating *public* API, but not all method implementations were appropriately updated. Case in point: if `$HOME/.config/xbuild/monodroid-config.xml` contains *no* value in `<java-sdk/>`, e.g. <monodroid> <android-sdk path="/path/to/android/sdk" /> <java-sdk /> <!-- empty! --> </monodroid> then Visual Studio for Mac may report a first-chance exception (only reported when debugging Visual Studio for Mac, as the exception is caught internally): TODO: exception + stack trace A major reason to adopt Nullable Reference Types is to *prevent* the occurrence of `NullReferenceException`s, so what went wrong? What went wrong with 2d3690e is that when XML attributes don't exist, [`XElement.Attribute()`][0] will return `null`, and most of our `XElement.Attribute()` invocations cast the `XAttribute` return value to `string`, "asserting" that a *non-`null`* is returned. Review the codebase for all `XElement.Attribute()` invocations, and update all casts from `(string)` to instead cast to `(string?)`. This ensures that we don't circumvent the C# compilers Nullable Reference Type checks, catches the circumvention which was present in `JdkLocations.GetUnixConfiguredJdkPaths()`, and thus avoids the first-chance exception that VSMac could see. [0]: https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xelement.attribute?view=net-6.0 Co-Authored by: @KirillOsenkov
c5f5575
to
30b0b2c
Compare
The exception we see in VSMac is:
Full stack:
|
Thanks for doing the proper fix here. |
var path = (string) java_sdk.Attribute ("path"); | ||
yield return path; | ||
var path = (string?) java_sdk.Attribute ("path"); | ||
if (path != null && !string.IsNullOrEmpty (path)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the path != null
check is redundant, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You'd think so, but… Different .NET profiles have different NRT metadata, and iirc in .NET Standard 2.0, string.IsNullOrEmpty()
does not have [NotNullWhen(false)]
on the parameter, which results in warnings if it isn't explicitly checked. :-(
JdkLocations.MacOS.cs(26,19): warning CS8603: Possible null reference return.
Context: #157
Context: 2d3690e
Commit 2d3690e added Nullable Reference Type support to
Xamarin.Android.Tools.AndroidSdk.dll
. Unfortunately, it waslargely "surface level", updating public API, but not all method
implementations were appropriately updated.
Case in point: if
$HOME/.config/xbuild/monodroid-config.xml
contains no value in
<java-sdk/>
, e.g.then Visual Studio for Mac may report a first-chance exception
(only reported when debugging Visual Studio for Mac, as the exception
is caught internally):
A major reason to adopt Nullable Reference Types is to prevent the
occurrence of
NullReferenceException
s, so what went wrong?What went wrong with 2d3690e is that when XML attributes don't
exist,
XElement.Attribute()
will returnnull
, and most ofour
XElement.Attribute()
invocations cast theXAttribute
returnvalue to
string
, "asserting" that a non-null
is returned.Review the codebase for all
XElement.Attribute()
invocations, andupdate all casts from
(string)
to instead cast to(string?)
.This ensures that we don't circumvent the C# compilers Nullable
Reference Type checks, catches the circumvention which was present
in
JdkLocations.GetUnixConfiguredJdkPaths()
, and thus avoids thefirst-chance exception that VSMac could see.
Co-Authored by: @KirillOsenkov