/// <summary>
        /// Get warning message for missing minimum dependencies.
        /// </summary>
        public static RestoreLogMessage GetMissingLowerBoundMessage(ResolvedDependencyKey dependency, params string[] targetGraphs)
        {
            NuGetLogCode code;
            var          message         = string.Empty;
            var          parent          = DiagnosticUtility.FormatIdentity(dependency.Parent);
            var          dependencyRange = DiagnosticUtility.FormatDependency(dependency.Child.Name, dependency.Range);
            var          missingChild    = DiagnosticUtility.FormatExpectedIdentity(dependency.Child.Name, dependency.Range);
            var          resolvedChild   = DiagnosticUtility.FormatIdentity(dependency.Child);

            if (HasMissingLowerBound(dependency.Range))
            {
                // Range does not have a lower bound, the best match can only be approximate.
                message = string.Format(CultureInfo.CurrentCulture, Strings.Warning_MinVersionNonInclusive,
                                        parent,
                                        dependencyRange,
                                        resolvedChild);

                code = NuGetLogCode.NU1602;
            }
            else
            {
                // The minimum version does not exist.
                message = string.Format(CultureInfo.CurrentCulture, Strings.Warning_MinVersionDoesNotExist,
                                        parent,
                                        dependencyRange,
                                        missingChild,
                                        resolvedChild);

                code = NuGetLogCode.NU1603;
            }

            return(RestoreLogMessage.CreateWarning(code, message, dependency.Child.Name, targetGraphs));
        }
        /// <summary>
        /// Log upgrade warnings from the graphs.
        /// </summary>
        public static IEnumerable <RestoreLogMessage> GetDependenciesAboveUpperBounds(List <IndexedRestoreTargetGraph> graphs, ILogger logger)
        {
            var messages = new List <RestoreLogMessage>();

            foreach (var indexedGraph in graphs)
            {
                var graph = indexedGraph.Graph;

                foreach (var node in graph.Flattened)
                {
                    var dependencies = node.Data?.Dependencies ?? Enumerable.Empty <LibraryDependency>();

                    foreach (var dependency in dependencies)
                    {
                        // Check if the dependency has an upper bound
                        var dependencyRange = dependency.LibraryRange.VersionRange;
                        var upperBound      = dependencyRange?.MaxVersion;
                        if (upperBound != null)
                        {
                            var dependencyId = dependency.Name;

                            // If the version does not exist then it was not resolved or is a project and should be skipped.
                            var match = indexedGraph.GetItemById(dependencyId, LibraryType.Package);
                            if (match != null)
                            {
                                var actualVersion = match.Key.Version;

                                // If the upper bound is included then require that the version be higher than the upper bound to fail
                                // If the upper bound is not included, then an exact match on the upperbound is a failure
                                var compare = dependencyRange.IsMaxInclusive ? 1 : 0;

                                if (VersionComparer.VersionRelease.Compare(actualVersion, upperBound) >= compare)
                                {
                                    // True if the package already has an NU1107 error, NU1608 would be redundant here.
                                    if (!indexedGraph.HasErrors(dependencyId))
                                    {
                                        var parent = DiagnosticUtility.FormatIdentity(node.Key);
                                        var child  = DiagnosticUtility.FormatDependency(dependencyId, dependencyRange);
                                        var actual = DiagnosticUtility.FormatIdentity(match.Key);

                                        var message = string.Format(CultureInfo.CurrentCulture,
                                                                    Strings.Warning_VersionAboveUpperBound,
                                                                    parent,
                                                                    child,
                                                                    actual);

                                        messages.Add(RestoreLogMessage.CreateWarning(NuGetLogCode.NU1608, message, dependencyId, graph.TargetGraphName));
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Merge log messages
            return(DiagnosticUtility.MergeOnTargetGraph(messages));
        }
Exemple #3
0
 /// <summary>
 /// Warn for project dependencies that do not include a lower bound on the version range.
 /// </summary>
 public static IEnumerable <RestoreLogMessage> GetProjectDependenciesMissingLowerBounds(PackageSpec project)
 {
     return(project.GetAllPackageDependencies()
            .Where(e => HasMissingLowerBound(e.LibraryRange.VersionRange))
            .OrderBy(e => e.Name, StringComparer.OrdinalIgnoreCase)
            .Select(e => RestoreLogMessage.CreateWarning(
                        code: NuGetLogCode.NU1604,
                        message: string.Format(CultureInfo.CurrentCulture, Strings.Warning_ProjectDependencyMissingLowerBound,
                                               DiagnosticUtility.FormatDependency(e.Name, e.LibraryRange.VersionRange)),
                        libraryId: e.Name)));
 }