Bug 26205

Summary: System.IO.Package.LoadRelationships throws null reference for some NuGet packages with PCLs generated on Windows
Product: [Mono] Class Libraries Reporter: Matt Ward <matt.ward>
Component: WindowsBaseAssignee: João Matos <joao.matos>
Status: VERIFIED FIXED    
Severity: normal CC: janmaltedittmer, joao.matos, kumpera, mono-bugs+mono, pj.beaman, shrutis
Priority: ---    
Version: 3.12.0   
Target Milestone: 4.0.3 (C5SR3)   
Hardware: PC   
OS: Mac OS   
Tags: Is this bug a regression?: ---
Last known good build:

Description Matt Ward 2015-01-20 12:30:56 UTC
Trying to retrieve the relationships from certain NuGet packages (such as Mono.Data.Sqlite.Portable v1.0.3.3) throws a null reference exception.

'NuGet pack my.nuspec' on Windows will generate directories for PCLs that have the dashes replaced with %2B which seem to be the cause of the null reference exception on Mono. If the %2B parts are replaced with the dash character then the exception does not occur. Also if you remove the Xamarin frameworks from the directory but leave the %2B parts the exception also does not occur. So a NuGet package with a directory of the following would fail:

portable-net4%2Bsl5%2Bwp8%2Bwin%2BMonoAndroid%2BMonoTouch%2BMonoMac%2BXamariniOS%2BXamarinMac

But one with the following would work:

portable-net4%2Bsl5%2Bwp8%2Bwin%2BMonoAndroid%2BMonoTouch%2BMonoMac

To reproduce:

Sample NUnit test project to reproduce the problem:

https://github.com/mrward/MonoPackageGetRelationshipsByTypeTest

[TestFixture]
public class PackageTest
{
	Stream OpenNuGetPackage ()
	{
		string directory = Path.GetDirectoryName (typeof(PackageTest).Assembly.Location);
		string fileName = Path.Combine (directory, "Mono.Data.Sqlite.Portable.1.0.3.3.nupkg");
		return File.OpenRead (fileName);
	}

	internal const string PackageRelationshipNamespace = "http://schemas.microsoft.com/packaging/2010/07/";
	internal const string ManifestRelationType = "manifest";

	[Test]
	public void GetRelationshipsByTypeFromNuGetPackage()
	{
		using (Stream stream = OpenNuGetPackage ()) {
			Package package = Package.Open (stream);
			PackageRelationship relationshipType = package.GetRelationshipsByType (
				PackageRelationshipNamespace + ManifestRelationType).SingleOrDefault ();

			Assert.AreEqual ("/Mono.Data.Sqlite.Portable.nuspec", relationshipType.TargetUri.ToString ());
		}
	}
}

The above test works on Windows.

Running it on 3.12.0 ((detached/a813491 Thu Dec 11 12:24:44 EST 2014) it fails with:

1) MonoPackageGetRelationshipsByTypeTest.PackageTest.GetRelationshipsByTypeFromNuGetPackage : System.NullReferenceException : Object reference not set to an instance of an object
at System.IO.Packaging.Package.LoadRelationships () [0x001e9] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/WindowsBase/System.IO.Packaging/Package.cs:338
at System.IO.Packaging.Package.get_Relationships () [0x0000b] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/WindowsBase/System.IO.Packaging/Package.cs:86
at System.IO.Packaging.Package.GetRelationshipsByType (System.String relationshipType) [0x00006] in /private/tmp/source-mono-mac-3.12.0-branch-32/bockbuild-mono-3.12.0-branch/profiles/mono-mac-xamarin/build-root/mono-3.12.0/mcs/class/WindowsBase/System.IO.Packaging/Package.cs:293
at MonoPackageGetRelationshipsByTypeTest.PackageTest.GetRelationshipsByTypeFromNuGetPackage () [0x00016] in tests/MonoPackageGetRelationshipsByTypeTest/src/PackageTest.cs:53
Comment 1 Rodrigo Kumpera 2015-06-16 14:21:53 UTC
Hey João,

Can you work on this one? It would be nice to get this fix on stable 4.0 too as it affects Xamarin.Forms.
Comment 2 João Matos 2015-06-16 14:29:23 UTC
https://github.com/mono/mono/commit/0d5437f
Comment 3 Shruti 2015-06-30 06:17:02 UTC
First, I have reproduced this issue with Mono 4.0.2.5 via following steps given in comment 0. Please refer to the screencast for the same: http://www.screencast.com/t/rUDtFS9CJa5q

Then, I have verified this issue with master mono 4.3.0.328. Test case written in comment(0) is passed. Please refer to the screencast regarding same: http://www.screencast.com/t/afhydQi6ko

Environment Info:
=== Xamarin Studio ===

Version 5.9.4 (build 5)
Installation UUID: 67eaf3b4-f8a7-4ab4-a9df-3197350ca5dc
Runtime:
	Mono 4.3.0 (master/2221dfc)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 403000328

=== Apple Developer Tools ===

Xcode 6.3 (7569)
Build 6D570

=== Xamarin.iOS ===

Version: 8.10.2.39 (Enterprise Edition)
Hash: 4e8928a
Branch: master
Build date: 2015-06-29 11:03:42-0400

=== Xamarin.Mac ===

Version: 2.0.2.39 (Enterprise Edition)

=== Build Information ===

Release ID: 509040005
Git revision: 6a0d9705ef8f73ff262c40692bda63b0c622f824
Build date: 2015-06-15 09:53:12-04
Xamarin addins: 7e93e9c3503f28770f23ce1b7eafd829919f18e8

=== Operating System ===

Mac OS X 10.10.3
Darwin XamnewiMac.local 14.3.0 Darwin Kernel Version 14.3.0
    Mon Mar 23 11:59:05 PDT 2015
    root:xnu-2782.20.48~5/RELEASE_X86_64 x86_64
Comment 4 Shruti 2015-07-20 02:22:21 UTC
This issue is working fine with C5SR3 MonoFramework-MDK-4.0.3.17.