Skip to content

Commit

Permalink
feature(pkg): add implicit constraint on dune (#8899)
Browse files Browse the repository at this point in the history
This constraint is almost always what the user wants if they are
planning to use the same dune that generated the build plan to build it

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
  • Loading branch information
rgrinberg authored Oct 10, 2023
1 parent 5644576 commit aa7e081
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
14 changes: 12 additions & 2 deletions src/dune_pkg/opam_solver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,15 @@ module Context_for_dune = struct
; version_preference : Version_preference.t
; local_packages : OpamFile.OPAM.t OpamPackage.Name.Map.t
; solver_env : Solver_env.t
; dune_version : OpamPackage.Version.t
}

let create ~solver_env ~repos ~local_packages ~version_preference =
{ repos; version_preference; local_packages; solver_env }
let dune_version =
let major, minor = Dune_lang.Stanza.latest_version in
OpamPackage.Version.of_string @@ sprintf "%d.%d" major minor
in
{ repos; version_preference; local_packages; solver_env; dune_version }
;;

type rejection = Unavailable
Expand Down Expand Up @@ -122,7 +127,12 @@ module Context_for_dune = struct
OpamFile.OPAM.version opam_file, opam_file_result))
;;

let user_restrictions _ _ = None
let user_restrictions : t -> OpamPackage.Name.t -> OpamFormula.version_constraint option
=
let dune = OpamPackage.Name.of_string "dune" in
fun t pkg ->
if OpamPackage.Name.equal dune pkg then Some (`Eq, t.dune_version) else None
;;

let map_filters ~(f : filter -> filter) : filtered_formula -> filtered_formula =
OpamFilter.gen_filter_formula
Expand Down
13 changes: 7 additions & 6 deletions src/dune_rules/pkg_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -845,12 +845,13 @@ module DB = struct
let get context =
let+ all = Lock_dir.get context in
let system_provided =
if Env.mem Env.initial ~var:"DUNE_PKG_OVERRIDE_OCAML"
then (
match all.ocaml with
| None -> Package.Name.Set.singleton ocaml_package_name
| Some (_, name) -> Package.Name.Set.singleton name)
else Package.Name.Set.empty
let base = Package.Name.Set.singleton (Package.Name.of_string "dune") in
match Env.mem Env.initial ~var:"DUNE_PKG_OVERRIDE_OCAML" with
| false -> base
| true ->
(match all.ocaml with
| None -> Package.Name.Set.add base ocaml_package_name
| Some (_, name) -> Package.Name.Set.singleton name)
in
{ all = all.packages; system_provided }
;;
Expand Down
31 changes: 31 additions & 0 deletions test/blackbox-tests/test-cases/pkg/implicit-dune-constraint.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
By default, we introduce a constraint on in the build plan that will require
the dune version to match the version of dune being used to generate the
constraint.

$ . ./helpers.sh
$ mkrepo

$ mkpkg dune 3.11.0 <<EOF
> EOF

$ test() {
> mkpkg foo <<EOF
> depends: [ "dune" {<= "$1"} ]
> EOF
> solve foo
> }

$ test "2.0.0"
Error: Unable to solve dependencies in build context: default
Can't find all required versions.
Selected: foo.0.0.1 x.dev
- dune -> (problem)
User requested = 3.11
Rejected candidates:
dune.3.11.0: Incompatible with restriction: = 3.11
[1]
$ test "4.0.0"
Solution for dune.lock:
dune.3.11.0
foo.0.0.1

0 comments on commit aa7e081

Please sign in to comment.