public async Task <IEnumerable <ProjectRestoreReference> > GetProjectReferencesAsync( Common.ILogger logger, CancellationToken _) { // DTE calls need to be done from the main thread await _threadingService.JoinableTaskFactory.SwitchToMainThreadAsync(); var results = new List <ProjectRestoreReference>(); var itemsFactory = ServiceLocator.GetInstance <IVsEnumHierarchyItemsFactory>(); // Verify ReferenceOutputAssembly var excludedProjects = GetExcludedReferences(itemsFactory, logger); var hasMissingReferences = false; // find all references in the project foreach (var childReference in GetVSProjectReferences()) { try { var reference3 = childReference as Reference3; // Verify that this is a project reference if (IsProjectReference(reference3, logger)) { // Verify that this is a valid and resolved project reference if (!IsReferenceResolved(reference3, logger)) { hasMissingReferences = true; continue; } if (EnvDTEProjectUtility.HasUnsupportedProjectCapability(reference3.SourceProject)) { // Skip this shared project continue; } var childProjectPath = EnvDTEProjectInfoUtility.GetFullProjectPath(reference3.SourceProject); // Skip projects which have ReferenceOutputAssembly=false if (!string.IsNullOrEmpty(childProjectPath) && !excludedProjects.Contains(childProjectPath, StringComparer.OrdinalIgnoreCase)) { var restoreReference = new ProjectRestoreReference() { ProjectPath = childProjectPath, ProjectUniqueName = childProjectPath }; results.Add(restoreReference); } } else { hasMissingReferences = true; } } catch (Exception ex) { // Exceptions are expected in some scenarios for native projects, // ignore them and show a warning hasMissingReferences = true; logger.LogDebug(ex.ToString()); Debug.Fail("Unable to find project dependencies: " + ex.ToString()); } } if (hasMissingReferences) { // Log a generic message once per project if any items could not be resolved. // In most cases this can be ignored, but in the rare case where the unresolved // item is actually a project the restore result will be incomplete. var message = string.Format( CultureInfo.CurrentCulture, Strings.UnresolvedItemDuringProjectClosureWalk, _vsProjectAdapter.UniqueName); logger.LogVerbose(message); } return(results); }
/// <summary> /// Get only the direct dependencies from a project /// </summary> public static async Task <IReadOnlyList <ProjectRestoreReference> > GetDirectProjectReferences( EnvDTEProject project, IEnumerable <string> resolvedProjects, ILogger log) { return(await NuGetUIThreadHelper.JoinableTaskFactory.RunAsync(async() => { // DTE calls need to be done from the main thread await NuGetUIThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); var results = new List <ProjectRestoreReference>(); var itemsFactory = ServiceLocator.GetInstance <IVsEnumHierarchyItemsFactory>(); // Verify ReferenceOutputAssembly var excludedProjects = GetExcludedReferences(project, itemsFactory); var hasMissingReferences = false; // find all references in the project foreach (var childReference in GetProjectReferences(project)) { try { var reference3 = childReference as Reference3; // check if deferred projects resolved this reference, which means this is still not loaded so simply continue // We'll get this reference from deferred projects later if (reference3 != null && resolvedProjects.Contains(reference3.Name, StringComparer.OrdinalIgnoreCase)) { continue; } // Set missing reference if // 1. reference is null OR // 2. reference is not resolved which means project is not loaded or assembly not found. else if (reference3 == null || !reference3.Resolved) { // Skip missing references and show a warning hasMissingReferences = true; continue; } // Skip missing references if (childReference.SourceProject != null) { if (EnvDTEProjectUtility.HasUnsupportedProjectCapability(childReference.SourceProject)) { // Skip this shared project continue; } var childProjectPath = EnvDTEProjectInfoUtility.GetFullProjectPath(childReference.SourceProject); // Skip projects which have ReferenceOutputAssembly=false if (!string.IsNullOrEmpty(childProjectPath) && !excludedProjects.Contains(childProjectPath, StringComparer.OrdinalIgnoreCase)) { var restoreReference = new ProjectRestoreReference() { ProjectPath = childProjectPath, ProjectUniqueName = childProjectPath }; results.Add(restoreReference); } } } catch (Exception ex) { // Exceptions are expected in some scenarios for native projects, // ignore them and show a warning hasMissingReferences = true; log.LogDebug(ex.ToString()); Debug.Fail("Unable to find project dependencies: " + ex.ToString()); } } if (hasMissingReferences) { // Log a generic message once per project if any items could not be resolved. // In most cases this can be ignored, but in the rare case where the unresolved // item is actually a project the restore result will be incomplete. var message = string.Format( CultureInfo.CurrentCulture, Strings.UnresolvedItemDuringProjectClosureWalk, EnvDTEProjectInfoUtility.GetUniqueName(project)); log.LogVerbose(message); } return results; })); }
/// <summary> /// Get only the direct dependencies from a project /// </summary> private DirectReferences GetDirectProjectReferences( EnvDTEProject project, string projectFileFullPath, ExternalProjectReferenceContext context, IVsEnumHierarchyItemsFactory itemsFactory, string rootProjectPath) { var logger = context.Logger; var cache = context.Cache; var result = new DirectReferences(); // Find a project.json in the project // This checks for files on disk to match how BuildIntegratedProjectSystem checks at creation time. // NuGet.exe also uses disk paths and not the project file. var projectName = Path.GetFileNameWithoutExtension(projectFileFullPath); var projectDirectory = Path.GetDirectoryName(projectFileFullPath); // Check for projectName.project.json and project.json string jsonConfigItem = ProjectJsonPathUtilities.GetProjectConfigPath( directoryPath: projectDirectory, projectName: projectName); var hasProjectJson = true; // Verify the file exists, otherwise clear it if (!File.Exists(jsonConfigItem)) { jsonConfigItem = null; hasProjectJson = false; } // Verify ReferenceOutputAssembly var excludedProjects = GetExcludedReferences(project, itemsFactory); var childReferences = new HashSet <string>(StringComparer.Ordinal); var hasMissingReferences = false; // find all references in the project foreach (var childReference in GetProjectReferences(project)) { try { var reference3 = childReference as Reference3; if (reference3 != null && !reference3.Resolved) { // Skip missing references and show a warning hasMissingReferences = true; continue; } // Skip missing references if (childReference.SourceProject != null) { if (EnvDTEProjectUtility.HasUnsupportedProjectCapability(childReference.SourceProject)) { // Skip this shared project continue; } var childName = EnvDTEProjectUtility.GetFullProjectPath(childReference.SourceProject); // Skip projects which have ReferenceOutputAssembly=false if (!string.IsNullOrEmpty(childName) && !excludedProjects.Contains(childName, StringComparer.OrdinalIgnoreCase)) { childReferences.Add(childName); result.ToProcess.Add(new DTEReference(childReference.SourceProject, childName)); } } else if (hasProjectJson) { // SDK references do not have a SourceProject or child references, // but they can contain project.json files, and should be part of the closure // SDKs are not projects, only the project.json name is checked here var possibleSdkPath = childReference.Path; if (!string.IsNullOrEmpty(possibleSdkPath) && !possibleSdkPath.EndsWith(".dll", StringComparison.OrdinalIgnoreCase) && Directory.Exists(possibleSdkPath)) { var possibleProjectJson = Path.Combine( possibleSdkPath, ProjectJsonPathUtilities.ProjectConfigFileName); if (File.Exists(possibleProjectJson)) { childReferences.Add(possibleProjectJson); // add the sdk to the results here result.Processed.Add(new ExternalProjectReference( possibleProjectJson, childReference.Name, possibleProjectJson, msbuildProjectPath: null, projectReferences: Enumerable.Empty <string>())); } } } } catch (Exception ex) { // Exceptions are expected in some scenarios for native projects, // ignore them and show a warning hasMissingReferences = true; logger.LogDebug(ex.ToString()); Debug.Fail("Unable to find project closure: " + ex.ToString()); } } if (hasMissingReferences) { // Log a warning message once per project // This warning contains only the names of the root project and the project with the // broken reference. Attempting to display more details on the actual reference // that has the problem may lead to another exception being thrown. var warning = string.Format( CultureInfo.CurrentCulture, Strings.Warning_ErrorDuringProjectClosureWalk, projectName, rootProjectPath); logger.LogWarning(warning); } // For the xproj -> xproj -> csproj scenario find all xproj-> xproj references. if (projectFileFullPath.EndsWith(XProjUtility.XProjExtension, StringComparison.OrdinalIgnoreCase)) { // All xproj paths, these are already checked for project.json var xprojFiles = XProjUtility.GetProjectReferences(projectFileFullPath); if (xprojFiles.Count > 0) { var pathToProject = GetPathToDTEProjectLookup(project); foreach (var xProjPath in xprojFiles) { // Only add projects that we can find in the solution, otherwise they will // end up causing failures. If this is an actual failure the resolver will // fail when resolving the dependency from project.json Project xProjDTE; if (pathToProject.TryGetValue(xProjPath, out xProjDTE)) { var xProjFullPath = EnvDTEProjectUtility.GetFullProjectPath(xProjDTE); if (!string.IsNullOrEmpty(xProjFullPath)) { childReferences.Add(xProjFullPath); // Continue walking this project if it has not been walked already result.ToProcess.Add(new DTEReference(xProjDTE, xProjFullPath)); } } } } } // Only set a package spec project name if a package spec exists var packageSpecProjectName = jsonConfigItem == null ? null : projectName; // Add the parent project to the results result.Processed.Add(new ExternalProjectReference( projectFileFullPath, packageSpecProjectName, jsonConfigItem, projectFileFullPath, childReferences)); return(result); }