Skip to content

Commit 600c39a

Browse files
committed
fix(pkg): make dune pin itelf
Automatically make dune pin itself. We're going to assume that the dune we're using right now is the one that will be used to run the build plan. Perhaps this behavior isn't always deired. We could imagine wanting to produce build plans for older or newer dunes. For now, this is a good default though. Addresses #12381 Signed-off-by: Rudi Grinberg <[email protected]>
1 parent 55db3f3 commit 600c39a

File tree

8 files changed

+128
-42
lines changed

8 files changed

+128
-42
lines changed

src/dune_pkg/dune_dep.ml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@ let version =
66
let major, minor = Dune_lang.Stanza.latest_version in
77
OpamPackage.Version.of_string @@ sprintf "%d.%d" major minor
88
;;
9+
10+
let package = OpamPackage.create (Package_name.to_opam_package_name name) version
11+
let opam_file = OpamFile.OPAM.create package

src/dune_pkg/dune_dep.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
val name : Package_name.t
22
val version : OpamPackage.Version.t
3+
val package : OpamPackage.t
4+
val opam_file : OpamFile.OPAM.t

src/dune_pkg/opam_solver.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,8 @@ module Context = struct
348348
let user_restrictions : t -> OpamPackage.Name.t -> OpamFormula.version_constraint option
349349
=
350350
fun t pkg ->
351+
(* This isn't really needed because we already pin dune, but it seems to
352+
help the error messages *)
351353
if Package_name.equal Dune_dep.name (Package_name.of_opam_package_name pkg)
352354
then Some (`Eq, t.dune_version)
353355
else None
@@ -1658,6 +1660,18 @@ let solve_lock_dir
16581660
~selected_depopts
16591661
~portable_lock_dir
16601662
=
1663+
let pinned_packages =
1664+
Package_name.Map.update pinned_packages Dune_dep.name ~f:(function
1665+
| None -> Some Resolved_package.dune
1666+
| Some p ->
1667+
let loc = Resolved_package.loc p in
1668+
User_error.raise
1669+
~loc
1670+
[ Pp.text
1671+
"Dune cannot be pinned. The currently running version is the only one that \
1672+
may be used"
1673+
])
1674+
in
16611675
let pinned_package_names = Package_name.Set.of_keys pinned_packages in
16621676
let stats_updater = Solver_stats.Updater.init () in
16631677
let context =

src/dune_pkg/resolved_package.ml

Lines changed: 53 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,39 @@ type extra_files =
44
| Inside_files_dir of Path.t option
55
| Git_files of Path.Local.t option * Rev_store.At_rev.t
66

7-
type nonrec t =
7+
type rest =
88
{ opam_file : OpamFile.OPAM.t
99
; package : OpamPackage.t
1010
; extra_files : extra_files
1111
; loc : Loc.t
1212
; dune_build : bool
1313
}
1414

15-
let dune_build t = t.dune_build
16-
let loc t = t.loc
17-
let package t = t.package
18-
let opam_file t = t.opam_file
15+
type nonrec t =
16+
| Dune
17+
| Rest of rest
18+
19+
let dune = Dune
20+
21+
let dune_build = function
22+
| Dune -> false
23+
| Rest t -> t.dune_build
24+
;;
25+
26+
let loc = function
27+
| Dune -> Loc.none
28+
| Rest t -> t.loc
29+
;;
30+
31+
let package = function
32+
| Dune -> Dune_dep.package
33+
| Rest t -> t.package
34+
;;
35+
36+
let opam_file = function
37+
| Dune -> Dune_dep.opam_file
38+
| Rest t -> t.opam_file
39+
;;
1940

2041
let add_opam_package_to_opam_file package opam_file =
2142
opam_file
@@ -37,12 +58,13 @@ let git_repo package ~opam_file ~opam_file_contents rev ~files_dir ~url =
3758
let opam_file_path = Path.of_local opam_file in
3859
let opam_file = read_opam_file package ~opam_file_path ~opam_file_contents ~url in
3960
let loc = Loc.in_file opam_file_path in
40-
{ dune_build = false
41-
; loc
42-
; package
43-
; opam_file
44-
; extra_files = Git_files (files_dir, rev)
45-
}
61+
Rest
62+
{ dune_build = false
63+
; loc
64+
; package
65+
; opam_file
66+
; extra_files = Git_files (files_dir, rev)
67+
}
4668
;;
4769

4870
let local_fs package ~dir ~opam_file_path ~files_dir ~url =
@@ -53,12 +75,13 @@ let local_fs package ~dir ~opam_file_path ~files_dir ~url =
5375
read_opam_file package ~opam_file_path ~opam_file_contents ~url
5476
in
5577
let loc = Loc.in_file opam_file_path in
56-
{ dune_build = false
57-
; loc
58-
; package
59-
; extra_files = Inside_files_dir files_dir
60-
; opam_file
61-
}
78+
Rest
79+
{ dune_build = false
80+
; loc
81+
; package
82+
; extra_files = Inside_files_dir files_dir
83+
; opam_file
84+
}
6285
;;
6386

6487
(* Scan a path recursively down retrieving a list of all files together with their
@@ -92,18 +115,26 @@ let local_package ~command_source loc opam_file opam_package =
92115
in
93116
let opam_file = add_opam_package_to_opam_file opam_package opam_file in
94117
let package = OpamFile.OPAM.package opam_file in
95-
{ dune_build; opam_file; package; loc; extra_files = Inside_files_dir None }
118+
Rest { dune_build; opam_file; package; loc; extra_files = Inside_files_dir None }
96119
;;
97120

98121
open Fiber.O
99122

100123
let get_opam_package_files resolved_packages =
101124
let indexed = List.mapi resolved_packages ~f:(fun i w -> i, w) |> Int.Map.of_list_exn in
102125
let from_dirs, from_git =
103-
Int.Map.partition_map indexed ~f:(fun (resolved_package : t) ->
104-
match resolved_package.extra_files with
105-
| Git_files (files_dir, rev) -> Right (files_dir, rev)
106-
| Inside_files_dir dir -> Left dir)
126+
let _dune, without_dune =
127+
Int.Map.partition_map indexed ~f:(function
128+
| Dune -> Left ()
129+
| Rest t -> Right t)
130+
in
131+
let dirs, git =
132+
Int.Map.partition_map without_dune ~f:(fun (resolved_package : rest) ->
133+
match resolved_package.extra_files with
134+
| Git_files (files_dir, rev) -> Right (files_dir, rev)
135+
| Inside_files_dir dir -> Left dir)
136+
in
137+
dirs, git
107138
in
108139
let+ from_git =
109140
if Int.Map.is_empty from_git

src/dune_pkg/resolved_package.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ val package : t -> OpamPackage.t
66
val opam_file : t -> OpamFile.OPAM.t
77
val loc : t -> Loc.t
88
val dune_build : t -> bool
9+
val dune : t
910

1011
val git_repo
1112
: OpamPackage.t

test/blackbox-tests/test-cases/pkg/implicit-dune-constraint.t

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,19 @@ dependency.
1717
Error: Unable to solve dependencies for the following lock directories:
1818
Lock directory dune.lock:
1919
Couldn't solve the package dependency formula.
20-
The following packages couldn't be found: dune
20+
Selected candidates: foo.0.0.1 x.dev
21+
- dune -> (problem)
22+
User requested = 3.XX
23+
foo 0.0.1 requires <= 2.0.0
24+
Rejected candidates:
25+
dune.3.XX: Incompatible with restriction: <= 2.0.0
2126
$ test "4.0.0"
22-
Error: Unable to solve dependencies for the following lock directories:
23-
Lock directory dune.lock:
24-
Couldn't solve the package dependency formula.
25-
The following packages couldn't be found: dune
26-
[1]
27+
Solution for dune.lock:
28+
- foo.0.0.1
2729

2830
$ test "4.0.0" 2>&1 | sed -E 's/3.[0-9]+/3.XX/g'
29-
Error: Unable to solve dependencies for the following lock directories:
30-
Lock directory dune.lock:
31-
Couldn't solve the package dependency formula.
32-
The following packages couldn't be found: dune
31+
Solution for dune.lock:
32+
- foo.0.0.1
3333

3434
Create a fake project and ensure `dune` can be used as a dependency:
3535
$ cat > dune-project <<EOF
@@ -40,8 +40,5 @@ Create a fake project and ensure `dune` can be used as a dependency:
4040
> (depends dune))
4141
> EOF
4242
$ dune pkg lock
43-
Error: Unable to solve dependencies for the following lock directories:
44-
Lock directory dune.lock:
45-
Couldn't solve the package dependency formula.
46-
The following packages couldn't be found: dune
47-
[1]
43+
Solution for dune.lock:
44+
(no dependencies to lock)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
Pinning dune itself
2+
3+
$ . ../helpers.sh
4+
5+
$ mkrepo
6+
$ add_mock_repo_if_needed
7+
8+
# CR-someday rgrinberg: ideally, this source shouldn't be necessary and we
9+
# should disqualify this pin without resolving any sources
10+
11+
$ mkdir _extra_source
12+
$ cat >_extra_source/dune-project <<EOF
13+
> (lang dune 3.12)
14+
> (package (name dune))
15+
> EOF
16+
17+
$ cat >dune-project <<EOF
18+
> (lang dune 3.13)
19+
> (pin
20+
> (url "file://$PWD/_extra_source")
21+
> (package (name dune)))
22+
> (package
23+
> (name main))
24+
> EOF
25+
26+
For now, pinning dune is not allowed:
27+
28+
$ dune pkg lock
29+
File "dune-project", line 4, characters 1-22:
30+
4 | (package (name dune)))
31+
^^^^^^^^^^^^^^^^^^^^^
32+
Error: Dune cannot be pinned. The currently running version is the only one
33+
that may be used
34+
[1]

test/blackbox-tests/test-cases/pkg/unsatisfied-version-constraint-on-dune.t

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ project:
2222

2323
Solve the dependencies:
2424
$ dune pkg lock 2>&1 | sed -E 's/"3.[0-9]+"/"3.XX"/'
25-
Error: The current version of Dune does not satisfy the version constraints
26-
for Dune in this project's dependencies.
27-
Details:
28-
Found version "3.XX" of package "dune" which doesn't satisfy the required
29-
version constraint "< 3.0"
25+
Error: Unable to solve dependencies for the following lock directories:
26+
Lock directory dune.lock:
27+
Couldn't solve the package dependency formula.
28+
Selected candidates: foo.dev
29+
- dune -> (problem)
30+
User requested = 3.21
31+
foo dev requires < 3.0
32+
Rejected candidates:
33+
dune.3.21: Incompatible with restriction: < 3.0

0 commit comments

Comments
 (0)