IEnumerable<ProjectReference> CreateProjectReferences (MonoDevelop.Projects.Project p, CancellationToken token)
		{
			foreach (var pr in p.GetReferencedItems (MonoDevelop.Projects.ConfigurationSelector.Default)) {
				if (token.IsCancellationRequested)
					yield break;
				var referencedProject = pr as MonoDevelop.Projects.DotNetProject;
				if (referencedProject == null)
					continue;
				if (TypeSystemService.IsOutputTrackedProject (referencedProject))
					continue;
				yield return new ProjectReference (GetOrCreateProjectId (referencedProject));
			}
		}
		static async Task<List<MetadataReference>> CreateMetadataReferences (MonoDevelop.Projects.DotNetProject netProject, ProjectId projectId, CancellationToken token)
		{
			List<MetadataReference> result = new List<MetadataReference> ();
			
			var configurationSelector = IdeApp.Workspace?.ActiveConfiguration ?? MonoDevelop.Projects.ConfigurationSelector.Default;
			var hashSet = new HashSet<string> (FilePath.PathComparer);

			try {
				foreach (string file in await netProject.GetReferencedAssemblies (configurationSelector, false).ConfigureAwait (false)) {
					if (token.IsCancellationRequested)
						return result;
					string fileName;
					if (!Path.IsPathRooted (file)) {
						fileName = Path.Combine (Path.GetDirectoryName (netProject.FileName), file);
					} else {
						fileName = Path.GetFullPath (file);
					}
					if (hashSet.Contains (fileName))
						continue;
					hashSet.Add (fileName);
					if (!File.Exists (fileName)) {
						LoggingService.LogError ("Error while getting referenced Assembly " + fileName + " for project " + netProject.Name + ": File doesn't exist"); 
						continue;
					}
					var metadataReference = MetadataReferenceCache.LoadReference (projectId, fileName);
					if (metadataReference == null)
						continue;
					result.Add (metadataReference);
				}
			} catch (Exception e) {
				LoggingService.LogError ("Error while getting referenced assemblies", e);
			}

			foreach (var pr in netProject.GetReferencedItems (configurationSelector)) {
				if (token.IsCancellationRequested)
					return result;
				var referencedProject = pr as MonoDevelop.Projects.DotNetProject;
				if (referencedProject == null)
					continue;
				if (TypeSystemService.IsOutputTrackedProject (referencedProject)) {
					var fileName = referencedProject.GetOutputFileName (configurationSelector);
					if (!File.Exists (fileName)) {
						LoggingService.LogError ("Error while getting project Reference (" + referencedProject.Name + ") " + fileName + " for project " + netProject.Name + ": File doesn't exist");
						continue;
					}
					var metadataReference = MetadataReferenceCache.LoadReference (projectId, fileName);
					if (metadataReference != null)
						result.Add (metadataReference);
				}
			}
			return result;
		}
		static async Task<List<MetadataReference>> CreateMetadataReferences (MonoDevelop.Projects.Project p, ProjectId projectId, CancellationToken token)
		{
			List<MetadataReference> result = new List<MetadataReference> ();

			var netProject = p as MonoDevelop.Projects.DotNetProject;
			if (netProject == null)
				return result;
			
			var configurationSelector = IdeApp.Workspace?.ActiveConfiguration ?? MonoDevelop.Projects.ConfigurationSelector.Default;
			var hashSet = new HashSet<string> (FilePath.PathComparer);

			bool addFacadeAssemblies = false;

			try {
				foreach (string file in await netProject.GetReferencedAssemblies (configurationSelector, false).ConfigureAwait (false)) {
					if (token.IsCancellationRequested)
						return result;
					string fileName;
					if (!Path.IsPathRooted (file)) {
						fileName = Path.Combine (Path.GetDirectoryName (netProject.FileName), file);
					} else {
						fileName = Path.GetFullPath (file);
					}
					if (hashSet.Contains (fileName))
						continue;
					hashSet.Add (fileName);
					if (!File.Exists (fileName))
						continue;
					result.Add (MetadataReferenceCache.LoadReference (projectId, fileName));
					addFacadeAssemblies |= MonoDevelop.Core.Assemblies.SystemAssemblyService.ContainsReferenceToSystemRuntime (fileName);
				}
			} catch (Exception e) {
				LoggingService.LogError ("Error while getting referenced assemblies", e);
			}
			// HACK: Facade assemblies should be added by the project system. Remove that when the project system can do that.
			if (addFacadeAssemblies) {
				if (netProject != null) {
					var runtime = netProject.TargetRuntime ?? MonoDevelop.Core.Runtime.SystemAssemblyService.DefaultRuntime;
					var facades = runtime.FindFacadeAssembliesForPCL (netProject.TargetFramework);
					foreach (var facade in facades) {
						if (!File.Exists (facade))
							continue;
						result.Add (MetadataReferenceCache.LoadReference (projectId, facade));
					}
				}
			}

			foreach (var pr in p.GetReferencedItems (configurationSelector)) {
				if (token.IsCancellationRequested)
					return result;
				var referencedProject = pr as MonoDevelop.Projects.DotNetProject;
				if (referencedProject == null)
					continue;
				if (TypeSystemService.IsOutputTrackedProject (referencedProject)) {
					var fileName = referencedProject.GetOutputFileName (configurationSelector);
					if (!File.Exists (fileName))
						continue;
					result.Add (MetadataReferenceCache.LoadReference (projectId, fileName));
				}
			}
			return result;
		}