Skip to content

Commit

Permalink
First attempt on solving the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
forki committed Dec 16, 2019
1 parent 6e5e067 commit f340ca9
Show file tree
Hide file tree
Showing 9 changed files with 185 additions and 16 deletions.
49 changes: 38 additions & 11 deletions src/Paket.Core/Dependencies/PackageResolver.fs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ type GetPackageDetailsParameters =
GetPackageDetailsParameters.ofParamsEx false sources groupName packageName version

type GetPackageVersionsParameters =
{ Package : SourcePackageInfo }
static member ofParams sources groupName packageName =
{ Package : SourcePackageInfo
MinVersion : SemVerInfo option }
static member ofParams sources groupName packageName minVersion =
SourcePackageInfo.ofParams sources groupName packageName
|> fun p -> { Package = p }
|> fun p -> { Package = p; MinVersion = minVersion }

type PackageDetailsFunc = GetPackageDetailsParameters -> Async<PackageDetails>
type PackageDetailsSyncFunc = GetPackageDetailsParameters -> PackageDetails
Expand Down Expand Up @@ -585,7 +586,8 @@ let private getCompatibleVersions
let resolverStrategy = getResolverStrategy globalStrategyForDirectDependencies globalStrategyForTransitives rootDependencies allRequirementsOfCurrentPackage currentRequirement

let allVersions =
getVersionsF resolverStrategy (GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName currentRequirement.Name)
let versionParams = GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName currentRequirement.Name currentRequirement.MinVersionSetting
getVersionsF resolverStrategy versionParams
|> Seq.map (fun (v, sources) -> VersionCache.ofParams v sources false)

match currentRequirement.VersionRequirement.Range with
Expand Down Expand Up @@ -1079,8 +1081,26 @@ let Resolve (getVersionsRaw : PackageVersionsFunc, getPreferredVersionsRaw : Pre
let sorted =
match resolverStrategy with
| ResolverStrategy.Max -> List.sortDescending versions
| ResolverStrategy.LatestPatch -> List.sortDescending versions
| ResolverStrategy.LatestMinor -> List.sortDescending versions
| ResolverStrategy.LatestPatch ->
match versionParams.MinVersion with
| Some minVersion ->
let preferred =
versions
|> List.filter (fun (v,s) -> v.Major = minVersion.Major && v.Minor = minVersion.Minor)
|> List.sortDescending

preferred @ List.sortDescending versions
| None -> List.sortDescending versions
| ResolverStrategy.LatestMinor ->
match versionParams.MinVersion with
| Some minVersion ->
let preferred =
versions
|> List.filter (fun (v,s) -> v.Major = minVersion.Major)
|> List.sortDescending

preferred @ List.sortDescending versions
| None -> List.sortDescending versions
| ResolverStrategy.Min -> List.sort versions

yield! sorted }
Expand Down Expand Up @@ -1176,7 +1196,8 @@ let Resolve (getVersionsRaw : PackageVersionsFunc, getPreferredVersionsRaw : Pre

let currentConflict =
let getVersionsF packName =
getVersionsBlock ResolverStrategy.Max (GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName) currentStep
let versionParameters = GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName currentRequirement.MinVersionSetting
getVersionsBlock ResolverStrategy.Max versionParameters currentStep

if Seq.isEmpty conflicts then
{ currentConflict with
Expand Down Expand Up @@ -1298,7 +1319,8 @@ let Resolve (getVersionsRaw : PackageVersionsFunc, getPreferredVersionsRaw : Pre
if not alreadyExplored then
for (pack,verReq,restr) in exploredPackage.Dependencies do
async {
let requestVersions = startRequestGetVersions (GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName pack)
let versionParameters = GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName pack currentRequirement.MinVersionSetting
let requestVersions = startRequestGetVersions versionParameters
requestVersions.Work.TryReprioritize true WorkPriority.LikelyRequired
let! versions = (requestVersions).Work.Task |> Async.AwaitTask
// Preload the first version in range of this requirement
Expand Down Expand Up @@ -1371,7 +1393,8 @@ let Resolve (getVersionsRaw : PackageVersionsFunc, getPreferredVersionsRaw : Pre
step (Step((currentConflict,nextStep,currentRequirement), (currentConflict,currentStep,currentRequirement,compatibleVersions,flags)::priorConflictSteps)) stackpack currentConflict.VersionsToExplore flags
else
let getVersionsF packName =
getVersionsBlock ResolverStrategy.Max (GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName) currentStep
let versionParameters = GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName currentRequirement.MinVersionSetting
getVersionsBlock ResolverStrategy.Max versionParameters currentStep

let conflictingPackageName,vr =
match Seq.tryHead conflictingResolvedPackages with
Expand Down Expand Up @@ -1401,13 +1424,17 @@ let Resolve (getVersionsRaw : PackageVersionsFunc, getPreferredVersionsRaw : Pre
}

for openReq in startingStep.OpenRequirements do
startRequestGetVersions (GetPackageVersionsParameters.ofParams openReq.Sources groupName openReq.Name)
let versionParamters = GetPackageVersionsParameters.ofParams openReq.Sources groupName openReq.Name openReq.MinVersionSetting
startRequestGetVersions versionParamters
|> ignore

let currentRequirement = getCurrentRequirement packageFilter startingStep.OpenRequirements (Dictionary())

let status =
let getVersionsF packName = getVersionsBlock ResolverStrategy.Max (GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName) startingStep
let getVersionsF packName =
let versionParameters = GetPackageVersionsParameters.ofParams currentRequirement.Sources groupName packName currentRequirement.MinVersionSetting
getVersionsBlock ResolverStrategy.Max versionParameters startingStep

ResolutionRaw.ConflictRaw { ResolveStep = startingStep; RequirementSet = Set.empty; Requirement = currentRequirement; GetPackageVersions = getVersionsF }


Expand Down
3 changes: 2 additions & 1 deletion src/Paket.Core/PublicAPI.fs
Original file line number Diff line number Diff line change
Expand Up @@ -720,7 +720,8 @@ type Dependencies(dependenciesFileName: string) =
|> List.distinct

let versions =
NuGet.GetVersions true alternativeProjectRoot root (GetPackageVersionsParameters.ofParams sources (GroupName "") (PackageName name))
let versionParameters = GetPackageVersionsParameters.ofParams sources (GroupName "") (PackageName name) None
NuGet.GetVersions true alternativeProjectRoot root versionParameters
|> Async.RunSynchronously
|> List.map (fun (v,_) -> v.ToString())
|> List.toArray
Expand Down
12 changes: 12 additions & 0 deletions src/Paket.Core/Versioning/Requirements.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1220,6 +1220,18 @@ type PackageRequirement =

member this.Depth = this.Graph.Count

member this.MinVersionSetting =
match this.VersionRequirement with
| VersionRequirement(v,_) ->
match v with
| Minimum v -> Some v
| GreaterThan _ -> None
| Maximum _ -> None
| LessThan _ -> None
| Specific _ -> None
| OverrideAll _ -> None
| Range _ -> None

member this.SettingString = (sprintf "%O %s %s %O %s" this.VersionRequirement (if this.ResolverStrategyForTransitives.IsSome then (sprintf "%O" this.ResolverStrategyForTransitives) else "") (if this.ResolverStrategyForDirectDependencies.IsSome then (sprintf "%O" this.ResolverStrategyForDirectDependencies) else "") this.Settings (if this.TransitivePrereleases then "TransitivePrereleases-true" else ""))

member this.HasPackageSettings = String.IsNullOrWhiteSpace this.SettingString |> not
Expand Down
1 change: 1 addition & 0 deletions tests/Paket.Tests/Paket.Tests.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="Resolver\AddingDependenciesSpecs.fs" />
<Compile Include="Resolver\GlobalPessimisticStrategySpecs.fs" />
<Compile Include="Resolver\GlobalOptimisticStrategySpecs.fs" />
<Compile Include="Resolver\GlobalKeepLatestPatchStrategySpecs.fs" />
<Compile Include="Resolver\StrategySpecs.fs" />
<Compile Include="Resolver\PropertyTestGenerators.fs" />
<Compile Include="Resolver\PropertyTests.fs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module DependencyGroupsAndRestrictions
module Paket.Resolver.DependencyGroupsAndRestrictions

open Paket
open NUnit.Framework
Expand Down
128 changes: 128 additions & 0 deletions tests/Paket.Tests/Resolver/GlobalKeepLatestPatchStrategySpecs.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
module Paket.Resolver.GlobalKeepLatestPatchStrategySpecs

open Paket
open NUnit.Framework
open FsUnit
open TestHelpers
open Paket.Domain
open Paket.PackageResolver

let resolve graph updateMode (cfg : DependenciesFile) =
let groups = [Constants.MainDependencyGroup, None ] |> Map.ofSeq
cfg.Resolve(true,noSha1,VersionsFromGraphAsSeq graph, (fun _ _ -> []),PackageDetailsFromGraph graph,(fun _ _ _ -> None),groups,updateMode).[Constants.MainDependencyGroup].ResolvedPackages.GetModelOrFail()

let graph =
OfSimpleGraph [
"Nancy.Bootstrappers.Windsor","0.23",["Castle.Windsor",VersionRequirement(VersionRange.AtLeast "3.2.1",PreReleaseStatus.No)]
"Castle.Windsor","3.2.1",[]
"Castle.Windsor","3.2.2",[]
"Castle.Windsor","3.3.0",[]
"Castle.Windsor","4.1.0",[]
]

let config1 = sprintf """
strategy %s
source http://www.nuget.org/api/v2
nuget Nancy.Bootstrappers.Windsor ~> 0.23
"""

[<Test>]
let ``should resolve simple config1 with latest-patch``() =
let resolved =
DependenciesFile.FromSource(config1 "latest-patch")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.2.2"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"


[<Test>]
let ``should resolve simple config1 with latest-minor``() =
let resolved =
DependenciesFile.FromSource(config1 "latest-minor")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.3.0"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"

let config2 = sprintf """
strategy %s
source http://www.nuget.org/api/v2
nuget Castle.Windsor
nuget Nancy.Bootstrappers.Windsor ~> 0.23
"""

[<Test>]
let ``should resolve simple config2 with latest-patch``() =
let resolved =
DependenciesFile.FromSource(config2 "latest-patch")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.2.2"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"


[<Test>]
let ``should resolve simple config2 with latest-minor``() =
let resolved =
DependenciesFile.FromSource(config2 "latest-minor")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.3.0"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"

let config3 = sprintf """
strategy %s
source http://www.nuget.org/api/v2
nuget Nancy.Bootstrappers.Windsor ~> 0.23
nuget Castle.Windsor
"""

[<Test>]
let ``should resolve simple config3 with latest-patch``() =
let resolved =
DependenciesFile.FromSource(config3 "latest-patch")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.2.2"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"


[<Test>]
let ``should resolve simple config3 with latest-minor``() =
let resolved =
DependenciesFile.FromSource(config3 "latest-minor")
|> resolve graph UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.3.0"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"

let graph2 =
OfSimpleGraph [
"Nancy.Bootstrappers.Windsor","0.23",["Castle.Windsor",VersionRequirement(VersionRange.AtLeast "3.2.1",PreReleaseStatus.No)]
"Castle.Windsor","3.2.0",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.2.0",PreReleaseStatus.No)]
"Castle.Windsor","3.2.1",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.2.0",PreReleaseStatus.No)]
"Castle.Windsor","3.3.0",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.3.0",PreReleaseStatus.No)]
"Castle.Windsor-NLog","3.2.0.1",["Castle.Core-NLog",VersionRequirement(VersionRange.AtLeast "3.2.0",PreReleaseStatus.No)]
"Castle.Windsor-NLog","3.3.0",["Castle.Core-NLog",VersionRequirement(VersionRange.AtLeast "3.3.0",PreReleaseStatus.No)]
"Castle.Core-NLog","3.2.0",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.2.0",PreReleaseStatus.No)]
"Castle.Core-NLog","3.3.0",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.3.0",PreReleaseStatus.No)]
"Castle.Core-NLog","3.3.1",["Castle.Core",VersionRequirement(VersionRange.AtLeast "3.3.1",PreReleaseStatus.No)]
"Castle.Core","3.2.0",[]
"Castle.Core","3.2.1",[]
"Castle.Core","3.2.2",[]
"Castle.Core","3.3.0",[]
"Castle.Core","3.3.1",[]
]

let config5 = sprintf """
strategy %s
source http://www.nuget.org/api/v2
nuget Nancy.Bootstrappers.Windsor !~> 0.23
"""

[<Test>]
let ``should override global strategy with latest-patch``() =
let resolved =
DependenciesFile.FromSource(config5 "latest-patch")
|> resolve graph2 UpdateMode.UpdateAll
getVersion resolved.[PackageName "Castle.Windsor"] |> shouldEqual "3.2.1"
getVersion resolved.[PackageName "Nancy.Bootstrappers.Windsor"] |> shouldEqual "0.23"
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module GlobalOptimisticStrategySpecs
module Paket.Resolver.GlobalOptimisticStrategySpecs

open Paket
open NUnit.Framework
Expand Down
2 changes: 1 addition & 1 deletion tests/Paket.Tests/Resolver/ResolverErrorSituationTests.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module ResolverErrorSituationTests
module Paket.Resolver.ResolverErrorSituationTests

open Paket
open NUnit.Framework
Expand Down
2 changes: 1 addition & 1 deletion tests/Paket.Tests/Resolver/StrategySpecs.fs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module StrategySpecs
module Paket.Resolver.StrategySpecs

open Paket
open NUnit.Framework
Expand Down

0 comments on commit f340ca9

Please sign in to comment.