예제 #1
0
        public void ResolverUtility_GetDiagnosticMessageForIncompatibleTargetInstalledNoAllowedVersion()
        {
            // Install b 2.0.0 - a requires b 1.0.0

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "2.0.0"));

            var installed = new List <PackageReference>();

            installed.Add(new PackageReference(new PackageIdentity("b",
                                                                   NuGetVersion.Parse("2.0.0")),
                                               NuGetFramework.Parse("net45"),
                                               true, false, false, null));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "b" });

            // Assert
            Assert.Equal("Unable to resolve dependencies. 'b 2.0.0' is not compatible with 'a 1.0.0 constraint: b (= 1.0.0)'.", message);
        }
        public void ResolverUtility_GetDiagnosticMessageForMissingTargetDependency()
        {
            // Install b 1.1.0 - which requires d 1.0.0 which is missing from any source.

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("b", "1.1.0.0",
                                       new NuGet.Packaging.Core.PackageDependency("c", VersionRange.Parse("[2.0.0]")),
                                       new NuGet.Packaging.Core.PackageDependency("d", VersionRange.Parse("[1.0.0.0]")),
                                       new NuGet.Packaging.Core.PackageDependency("e", VersionRange.Parse("[1.0.0]"))));
            solution.Add(CreatePackage("c", "3.0.0"));
            solution.Add(CreatePackage("e", "1.0.0"));

            var installed = new List <PackageReference>();

            installed.Add(CreateInstalledPackage("c", "1.0.0"));

            var available = solution.ToList();

            available.Add(CreatePackage("c", "1.0.0"));
            available.Add(CreatePackage("c", "2.0.0"));

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "b" }, Enumerable.Empty <PackageSource>());

            // Assert
            Assert.Equal("Unable to find a version of 'd' that is compatible with 'b 1.1.0 constraint: d (= 1.0.0)'.", message);
        }
예제 #3
0
        public void ResolverUtility_GetDiagnosticMessageForIncompatibleTargetWithAllowedVersion()
        {
            // Install b 2.0.0 - a requires b 1.0.0

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "2.0.0"));

            var installed = new List <PackageReference>();

            installed.Add(new PackageReference(new PackageIdentity("b",
                                                                   NuGetVersion.Parse("2.0.0")),
                                               NuGetFramework.Parse("net45"),
                                               true, false, false, VersionRange.Parse("[2.0.0]")));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "b" });

            // Assert
            Assert.Equal("Unable to find a version of 'b' that is compatible with 'a 1.0.0 constraint: b (= 1.0.0)'. 'b' has an additional constraint (= 2.0.0) defined in packages.config.", message);
        }
        public void ResolverUtility_GetDiagnosticMessageForIncompatibleDependencyWithAllowedVersion()
        {
            // Install a 1.0.0 - which requires d 1.0.0 but d 2.0.0 is already installed.

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0-1111", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "1.0.0",
                                       new NuGet.Packaging.Core.PackageDependency("c", VersionRange.Parse("[1.0.0]")),
                                       new NuGet.Packaging.Core.PackageDependency("d", VersionRange.Parse("[1.0.0-1234]")),
                                       new NuGet.Packaging.Core.PackageDependency("e", VersionRange.Parse("[1.0.0]"))));
            solution.Add(CreatePackage("c", "2.0.0"));
            solution.Add(CreatePackage("d", "2.0.0"));
            solution.Add(CreatePackage("e", "1.0.0"));
            solution.Add(CreatePackage("f", "1.0.0", "d", "[2.0.0]"));

            var installed = new List <PackageReference>();

            installed.Add(CreateInstalledPackage("d", "2.0.0"));

            var available = solution.ToList();

            available.Add(CreatePackage("c", "1.0.0"));

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "a" }, Enumerable.Empty <PackageSource>());

            // Assert
            Assert.Equal("Unable to resolve dependencies. 'd 2.0.0' is not compatible with 'a 1.0.0 constraint: d (= 1.0.0)'.", message);
        }
예제 #5
0
        public void ResolverUtility_GetDiagnosticMessageNoPackages()
        {
            // Arrange
            var solution = new List <ResolverPackage>();

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, Enumerable.Empty <PackageReference>(), new string[] { });

            // Assert
            Assert.Equal("Unable to resolve dependencies.", message);
        }
예제 #6
0
        public void ResolverUtility_GetDiagnosticMessageForTargetMissingDependency()
        {
            // Install a, b cannot be found

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0, )"));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, Enumerable.Empty <PackageReference>(), new string[] { "a" });

            // Assert
            Assert.Equal("Unable to resolve dependency 'b'.", message);
        }
        public void ResolverUtility_GetDiagnosticMessageAllAbsentPackages()
        {
            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreateAbsentPackage("a"));
            solution.Add(CreateAbsentPackage("b"));
            solution.Add(CreateAbsentPackage("c"));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, Enumerable.Empty <PackageReference>(), new string[] { "a" }, Enumerable.Empty <PackageSource>());

            // Assert
            Assert.Equal("Unable to resolve dependencies.", message);
        }
예제 #8
0
        public void ResolverUtility_GetDiagnosticMessageForTargetAbsentDependency()
        {
            // Install a, b cannot be found

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0, )"));
            solution.Add(CreateAbsentPackage("b"));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, Enumerable.Empty <PackageReference>(), new string[] { "a" });

            // Assert
            Assert.Equal("Unable to find a version of 'b' that is compatible with 'a 1.0.0 constraint: b (\u2265 1.0.0)'.", message);
        }
예제 #9
0
        public void ResolverUtility_GetDiagnosticMessageForTargetIncompatibleDependency()
        {
            // Install a, b 1.0.0 cannot be found

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "2.0.0"));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, Enumerable.Empty <PackageReference>(), new string[] { "a" });

            // Assert
            Assert.Equal("Unable to resolve dependencies. 'b 2.0.0' is not compatible with 'a 1.0.0 constraint: b (= 1.0.0)'.", message);
        }
예제 #10
0
        public void ResolverUtility_GetDiagnosticMessageForIncompatibleTarget()
        {
            // Install b 2.0.0 - a requires b 1.0.0

            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "2.0.0"));

            var installed = new List <PackageReference>();

            installed.Add(CreateInstalledPackage("a", "1.0.0"));

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "b" });

            // Assert
            Assert.Equal("Unable to resolve dependencies. 'b 2.0.0' is not compatible with 'a 1.0.0 constraint: b (= 1.0.0)'.", message);
        }
예제 #11
0
        public void ResolverUtility_GetDiagnosticMessageVerifyDependencyDistanceIsUsed()
        {
            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0", "y", "[1.0.0]"));
            solution.Add(CreatePackage("y", "1.0.0", "z", "[1.0.0]"));
            solution.Add(CreateAbsentPackage("z"));
            solution.Add(CreatePackage("a", "1.0.0", "b", "[1.0.0]"));
            solution.Add(CreatePackage("b", "1.0.0", "c", "[1.0.0]"));
            solution.Add(CreateAbsentPackage("c"));

            var installed = new List <PackageReference>();

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "a" });

            // Assert
            Assert.Equal("Unable to find a version of 'z' that is compatible with 'y 1.0.0 constraint: z (= 1.0.0)'.", message);
        }
예제 #12
0
        public void ResolverUtility_GetDiagnosticMessageVerifyDiamondDependencySortsById()
        {
            // Arrange
            var solution = new List <ResolverPackage>();

            solution.Add(CreatePackage("a", "1.0.0",
                                       new NuGet.Packaging.Core.PackageDependency("b", VersionRange.Parse("[2.0.0]")),
                                       new NuGet.Packaging.Core.PackageDependency("c", VersionRange.Parse("[1.0.0]"))));
            solution.Add(CreatePackage("c", "1.0.0", "d", "[1.0.0]"));
            solution.Add(CreateAbsentPackage("d"));
            solution.Add(CreatePackage("b", "2.0.0", "d", "[1.0.0]"));

            var installed = new List <PackageReference>();

            var available = solution.ToList();

            // Act
            var message = ResolverUtility.GetDiagnosticMessage(solution, available, installed, new string[] { "a" });

            // Assert
            Assert.Equal("Unable to find a version of 'd' that is compatible with 'b 2.0.0 constraint: d (= 1.0.0)', 'c 1.0.0 constraint: d (= 1.0.0)'.", message);
        }
예제 #13
0
        /// <summary>
        /// Resolve a package closure
        /// </summary>
        public IEnumerable <SourcePackageDependencyInfo> Resolve(PackageResolverContext context, CancellationToken token)
        {
            var stopWatch = new Stopwatch();

            token.ThrowIfCancellationRequested();

            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            // validation
            foreach (var requiredId in context.RequiredPackageIds)
            {
                if (!context.AvailablePackages.Any(p => StringComparer.OrdinalIgnoreCase.Equals(p.Id, requiredId)))
                {
                    throw new NuGetResolverInputException(string.Format(CultureInfo.CurrentCulture, "Unable to find package '{0}'. Existing packages must be restored before performing an install or update.", requiredId));
                }
            }

            var invalidExistingPackages = new List <string>();

            var installedPackages = context.PackagesConfig.Select(p => p.PackageIdentity).ToArray();

            // validate existing package.config for any invalid dependency
            foreach (var package in installedPackages)
            {
                var existingPackage =
                    context.AvailablePackages.FirstOrDefault(
                        p =>
                        StringComparer.OrdinalIgnoreCase.Equals(p.Id, package.Id) &&
                        p.Version.Equals(package.Version));

                if (existingPackage is not null)
                {
                    // check if each dependency can be satisfied with existing packages
                    var brokenDependencies = GetBrokenDependencies(existingPackage, installedPackages);

                    if (brokenDependencies is not null && brokenDependencies.Any())
                    {
                        invalidExistingPackages.AddRange(brokenDependencies.Select(dependency => FormatDependencyConstraint(existingPackage, dependency)));
                    }
                }
                else
                {
                    // check same package is being updated and we've a higher version then
                    // ignore logging warning for that.
                    existingPackage =
                        context.AvailablePackages.FirstOrDefault(
                            p =>
                            StringComparer.OrdinalIgnoreCase.Equals(p.Id, package.Id) &&
                            VersionComparer.Default.Compare(p.Version, package.Version) > 0);

                    if (existingPackage is null)
                    {
                        var packageString = $"'{package.Id} {package.Version.ToNormalizedString()}'";
                        invalidExistingPackages.Add(packageString);
                    }
                }
            }
            // log warning message for all the invalid package dependencies
            if (invalidExistingPackages.Count > 0)
            {
                context.Log.LogWarning(
                    string.Format(
                        CultureInfo.CurrentCulture, "One or more unresolved package dependency constraints detected in the existing packages.config file. All dependency constraints must be resolved to add or update packages. If these packages are being updated this message may be ignored, if not the following error(s) may be blocking the current package operation: {0}",
                        string.Join(", ", invalidExistingPackages)));
            }

            // convert the available packages into ResolverPackages
            var resolverPackages = new List <ResolverPackage>();

            // pre-process the available packages to remove any packages that can't possibly form part of a solution
            var availablePackages = RemoveImpossiblePackages(context.AvailablePackages, context.RequiredPackageIds);

            foreach (var package in availablePackages)
            {
                IEnumerable <PackageDependency> dependencies = null;

                // clear out the dependencies if the behavior is set to ignore
                if (context.DependencyBehavior == DependencyBehavior.Ignore)
                {
                    dependencies = Enumerable.Empty <PackageDependency>();
                }
                else
                {
                    dependencies = package.Dependencies ?? Enumerable.Empty <PackageDependency>();
                }

                resolverPackages.Add(new ResolverPackage(package.Id, package.Version, dependencies, package.Listed, false));
            }

            // Sort the packages to make this process as deterministic as possible
            resolverPackages.Sort(PackageIdentityComparer.Default);

            // Keep track of the ids we have added
            var groupsAdded = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            var grouped     = new List <List <ResolverPackage> >();

            // group the packages by id
            foreach (var group in resolverPackages.GroupBy(e => e.Id, StringComparer.OrdinalIgnoreCase))
            {
                groupsAdded.Add(group.Key);

                var curSet = group.ToList();

                // add an absent package for non-targets
                // being absent allows the resolver to throw it out if it is not needed
                if (!context.RequiredPackageIds.Contains(group.Key, StringComparer.OrdinalIgnoreCase))
                {
                    curSet.Add(new ResolverPackage(id: group.Key, version: null, dependencies: null, listed: true, absent: true));
                }

                grouped.Add(curSet);
            }

            // find all needed dependencies, excluding manually ignored dependencies
            var dependencyIds = resolverPackages.Where(e => e.Dependencies is not null)
                                .SelectMany(e => e.Dependencies.Select(d => d.Id)).Distinct(StringComparer.OrdinalIgnoreCase);

            //var ignoredDependencyIds = dependencyIds.

            foreach (string depId in dependencyIds)
            {
                // packages which are unavailable need to be added as absent packages
                // ex: if A -> B  and B is not found anywhere in the source repositories we add B as absent
                if (!groupsAdded.Contains(depId))
                {
                    groupsAdded.Add(depId);
                    grouped.Add(new List <ResolverPackage>()
                    {
                        new ResolverPackage(id: depId, version: null, dependencies: null, listed: true, absent: true)
                    });
                }
            }

            token.ThrowIfCancellationRequested();

            // keep track of the best partial solution
            var bestSolution = Enumerable.Empty <ResolverPackage>();

            Action <IEnumerable <ResolverPackage> > diagnosticOutput = (partialSolution) =>
            {
                // store each solution as they pass through.
                // the combination solver verifies that the last one returned is the best
                bestSolution = partialSolution;
            };

            // Run solver
            var comparer = new ResolverComparer(context.DependencyBehavior, context.PreferredVersions, context.TargetIds);

            var sortedGroups = ResolverInputSort.TreeFlatten(grouped, context);

            var solution = CombinationSolver <ResolverPackage> .FindSolution(
                groupedItems : sortedGroups,
                itemSorter : comparer,
                shouldRejectPairFunc : ShouldRejectPackagePair,
                diagnosticOutput : diagnosticOutput);

            // check if a solution was found
            if (solution is not null)
            {
                var nonAbsentCandidates = solution.Where(c => !c.Absent);

                if (nonAbsentCandidates.Any())
                {
                    // topologically sort non absent packages
                    var sortedSolution = ResolverUtility.TopologicalSort(nonAbsentCandidates);

                    // Find circular dependency for topologically sorted non absent packages since it will help maintain cache of
                    // already processed packages
                    var circularReferences = ResolverUtility.FindFirstCircularDependency(sortedSolution);

                    if (circularReferences.Any())
                    {
                        // the resolver is able to handle circular dependencies, however we should throw here to keep these from happening
                        throw new NuGetResolverConstraintException(
                                  string.Format(CultureInfo.CurrentCulture, "Circular dependency detected '{0}'.",
                                                string.Join(" => ", circularReferences.Select(package => $"{package.Id} {package.Version.ToNormalizedString()}"))));
                    }

                    // solution found!
                    stopWatch.Stop();
                    context.Log.LogMinimal(
                        string.Format("Resolving dependency information took {0}", DatetimeUtility.ToReadableTimeFormat(stopWatch.Elapsed)));
                    return(sortedSolution.Where(x => !context.IgnoredIds.Contains(x.Id)).ToArray());
                }
            }

            // no solution was found, throw an error with a diagnostic message
            var message = ResolverUtility.GetDiagnosticMessage(bestSolution, context.AvailablePackages, context.PackagesConfig, context.TargetIds, context.PackageSources);

            throw new NuGetResolverConstraintException(message);
        }