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

[AndroidSdkWindows] Guard against exception checking registry. #79

Merged
merged 1 commit into from
Apr 16, 2020

Conversation

jpobst
Copy link
Contributor

@jpobst jpobst commented Jan 14, 2020

In https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html, the user is hitting an exception when checking for an executable from paths found in their registry.

If we hit an error we should just return false as there obviously isn't a valid install located there.

@jonpryor
Copy link
Member

In-progress commit message:

Fixes: https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html

Somehow, a registry key contains invalid path characters.
This causes an `ArgumentException` from
`AndroidSdkWindows.CheckRegistryKeyForExecutable()`:

	System.ArgumentException: Illegal characters in path.
	   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
	   at System.IO.Path.Combine(String path1, String path2)
	   at Xamarin.Android.Tools.AndroidSdkWindows.CheckRegistryKeyForExecutable(UIntPtr key, String subkey, String valueName, Wow64 wow64, String subdir, String exe)
	   at Xamarin.Android.Tools.AndroidSdkWindows.<GetAllAvailableAndroidSdks>d__37.MoveNext()
	   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
	   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
	   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
	   at Xamarin.Android.Tools.AndroidSdkBase.get_AllAndroidSdks()
	   at Xamarin.Android.Tools.AndroidSdkBase.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath)
	   at Xamarin.Android.Tools.AndroidSdkWindows.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath)
	   at Xamarin.Android.Tools.AndroidSdkInfo..ctor(Action`2 logger, String androidSdkPath, String androidNdkPath, String javaSdkPath)

Handle the `ArgumentException` by wrapping the body of
`CheckRegistryKeyForExecutable()` within a `try/catch` block and
catch all exceptions.  If an exception is thrown, there is no
executable of interest.

@jonpryor
Copy link
Member

Two questions:

  1. Should the try/catch block just wrap the Path.Combine() call? That's the only place in the linked-to bug that is throwing; ideally nothing else would throw (we hope?).
  2. Can we/should we log anything as part of the catch (Exception) block? This can be done by making CheckRegistryKeyForExecutable() an instance method, then using the Logger property.

@jpobst
Copy link
Contributor Author

jpobst commented Feb 26, 2020

It feels like this was super-unimportant. The registry is just one of many places we check, and if we can't find it there we move on to the others. However the one thing it shouldn't do is crash for any reason, because it completely blocks a user from being able to do anything, even if other detection methods would have succeeded.

@jonpryor jonpryor merged commit f473ff9 into master Apr 16, 2020
@jpobst jpobst deleted the windows-registry branch April 16, 2020 18:13
@brendanzagaeski
Copy link
Contributor

Draft release notes

Here is the Xamarin.Android release note I plan to include for this pull request:

### Issues fixed

#### Application and library build and deployment

  * [Developer Community](https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html):
    *System.ArgumentException: Illegal characters in path* could prevent
    successful automatic detection of the Android SDK location during builds in
    some cases if an `AndroidSdkDirectory` registry value was set for
    Xamarin.Android that contained unexpected characters.

To suggest any edits to this, feel free to reply in this PR. Thanks!

jonpryor pushed a commit that referenced this pull request Apr 22, 2020
Fixes: https://developercommunity.visualstudio.com/content/problem/883179/ilegal-characters-in-path-after-fresh-install.html

Somehow, a registry key contains invalid path characters.
This causes an `ArgumentException` from
`AndroidSdkWindows.CheckRegistryKeyForExecutable()`:

	System.ArgumentException: Illegal characters in path.
	   at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional)
	   at System.IO.Path.Combine(String path1, String path2)
	   at Xamarin.Android.Tools.AndroidSdkWindows.CheckRegistryKeyForExecutable(UIntPtr key, String subkey, String valueName, Wow64 wow64, String subdir, String exe)
	   at Xamarin.Android.Tools.AndroidSdkWindows.<GetAllAvailableAndroidSdks>d__37.MoveNext()
	   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
	   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
	   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
	   at Xamarin.Android.Tools.AndroidSdkBase.get_AllAndroidSdks()
	   at Xamarin.Android.Tools.AndroidSdkBase.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath)
	   at Xamarin.Android.Tools.AndroidSdkWindows.Initialize(String androidSdkPath, String androidNdkPath, String javaSdkPath)
	   at Xamarin.Android.Tools.AndroidSdkInfo..ctor(Action`2 logger, String androidSdkPath, String androidNdkPath, String javaSdkPath)

Handle the `ArgumentException` by wrapping the body of
`CheckRegistryKeyForExecutable()` within a `try/catch` block and
catch all exceptions.  If an exception is thrown, there is no
executable of interest.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants