Skip to content

Commit a7e68e3

Browse files
maisteart-w
andauthored
fix: use build and install instructions from opam on pinning (#11513)
* fix: use build and install instructions from opam on pinning When building instructions, pinning packages relied on dune for all the cases. This commit changes this behavior: if there is an opam file, it extracts the instructions from it. Signed-off-by: Etienne Marais <[email protected]> Co-authored-by: ArthurW <[email protected]> Signed-off-by: Etienne Marais <[email protected]> * test: upgrade test with expected output Signed-off-by: Etienne Marais <[email protected]> * test: add missing test case Signed-off-by: Etienne Marais <[email protected]> * fix: use sum type instead of list Signed-off-by: Etienne Marais <[email protected]> * doc: add type explanation Signed-off-by: Etienne Marais <[email protected]> * refactor(naming): change to clearer names Signed-off-by: Etienne Marais <[email protected]> --------- Signed-off-by: Etienne Marais <[email protected]> Co-authored-by: ArthurW <[email protected]>
1 parent 1c359df commit a7e68e3

File tree

7 files changed

+119
-17
lines changed

7 files changed

+119
-17
lines changed

bin/lock_dev_tool.ml

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ let make_local_package_wrapping_dev_tool ~dev_tool ~dev_tool_version ~extra_depe
6161
; pins = Package_name.Map.empty
6262
; conflict_class = []
6363
; loc = Loc.none
64+
; command_source = Opam_file { build = []; install = [] }
6465
}
6566
;;
6667

src/dune_pkg/local_package.ml

+55-3
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ type pin =
1212

1313
type pins = pin Package_name.Map.t
1414

15+
type command_source =
16+
| Assume_defaults
17+
| Opam_file of
18+
{ build : OpamTypes.command list
19+
; install : OpamTypes.command list
20+
}
21+
1522
type t =
1623
{ name : Package_name.t
1724
; version : Package_version.t option
@@ -21,6 +28,7 @@ type t =
2128
; depopts : Package_dependency.t list
2229
; pins : pins
2330
; loc : Loc.t
31+
; command_source : command_source
2432
}
2533

2634
module Dependency_hash = struct
@@ -56,9 +64,21 @@ module For_solver = struct
5664
; depopts : Package_dependency.t list
5765
; conflict_class : Package_name.t list
5866
; pins : pins
67+
; build : OpamTypes.command list
68+
; install : OpamTypes.command list
5969
}
6070

61-
let to_opam_file { name; dependencies; conflicts; conflict_class; depopts; pins = _ } =
71+
let to_opam_file
72+
{ name
73+
; dependencies
74+
; conflicts
75+
; conflict_class
76+
; depopts
77+
; pins = _
78+
; build
79+
; install
80+
}
81+
=
6282
(* CR-rgrinberg: it's OK to ignore pins here since the solver doesn't touch
6383
them *)
6484
OpamFile.OPAM.empty
@@ -72,6 +92,8 @@ module For_solver = struct
7292
|> OpamFile.OPAM.with_depopts
7393
(List.map depopts ~f:Package_dependency.to_opam_filtered_formula
7494
|> OpamFormula.ands)
95+
|> OpamFile.OPAM.with_install install
96+
|> OpamFile.OPAM.with_build build
7597
;;
7698

7799
let non_local_dependencies local_deps =
@@ -93,9 +115,23 @@ let for_solver
93115
; loc = _
94116
; depopts
95117
; pins
118+
; command_source
96119
}
97120
=
98-
{ For_solver.name; dependencies; conflicts; conflict_class; depopts; pins }
121+
let build, install =
122+
match command_source with
123+
| Assume_defaults -> [], []
124+
| Opam_file { build; install } -> build, install
125+
in
126+
{ For_solver.name
127+
; dependencies
128+
; conflicts
129+
; conflict_class
130+
; depopts
131+
; pins
132+
; build
133+
; install
134+
}
99135
;;
100136

101137
let of_package (t : Dune_lang.Package.t) =
@@ -114,11 +150,18 @@ let of_package (t : Dune_lang.Package.t) =
114150
; loc
115151
; conflict_class = []
116152
; pins = Package_name.Map.empty
153+
; command_source = Assume_defaults
117154
}
118155
| Some { file; contents = opam_file_string } ->
119156
let opam_file =
120157
Opam_file.read_from_string_exn ~contents:opam_file_string (Path.source file)
121158
in
159+
let command_source =
160+
Opam_file
161+
{ build = opam_file |> OpamFile.OPAM.build
162+
; install = opam_file |> OpamFile.OPAM.install
163+
}
164+
in
122165
let dependencies =
123166
opam_file |> OpamFile.OPAM.depends |> Dependency_formula.of_filtered_formula
124167
in
@@ -150,5 +193,14 @@ let of_package (t : Dune_lang.Package.t) =
150193
~loc:pkg.loc
151194
[ Pp.textf "package %s is already pinned" (Package_name.to_string pkg.name) ]
152195
in
153-
{ name; version; dependencies; conflicts; depopts; loc; conflict_class; pins }
196+
{ name
197+
; version
198+
; dependencies
199+
; conflicts
200+
; depopts
201+
; loc
202+
; conflict_class
203+
; pins
204+
; command_source
205+
}
154206
;;

src/dune_pkg/local_package.mli

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,18 @@ type pin =
1010

1111
type pins = pin Package_name.Map.t
1212

13+
(** Describe the commands to execute from the source. [Assume_defaults] means
14+
that no opam file is available and dune will use [dune] to build the
15+
package. [Opam_file { build ; install }] describe where there is an opam
16+
file from which we extract the build and the install command. Note that
17+
both of them can be empty. *)
18+
type command_source =
19+
| Assume_defaults
20+
| Opam_file of
21+
{ build : OpamTypes.command list
22+
; install : OpamTypes.command list
23+
}
24+
1325
(** Information about a local package that's relevant for package management.
1426
This is intended to represent local packages defined in a dune-project file
1527
(rather than packages in a lockdir). This is distinct from a
@@ -26,6 +38,7 @@ type t =
2638
; depopts : Package_dependency.t list
2739
; pins : pins
2840
; loc : Loc.t
41+
; command_source : command_source
2942
}
3043

3144
module Dependency_hash : sig
@@ -48,6 +61,8 @@ module For_solver : sig
4861
; depopts : Package_dependency.t list
4962
; conflict_class : Package_name.t list
5063
; pins : pins
64+
; build : OpamTypes.command list
65+
; install : OpamTypes.command list
5166
}
5267

5368
(** [to_opam_file t] returns an [OpamFile.OPAM.t] whose fields are based on the

src/dune_pkg/pin_stanza.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,9 @@ let resolve (t : DB.t) ~(scan_project : Scan_project.t)
349349
]
350350
| Some pkg ->
351351
let resolved_package =
352+
let local_package = Local_package.of_package pkg in
352353
let opam_file =
353-
Local_package.of_package pkg
354+
local_package
354355
|> Local_package.for_solver
355356
|> Local_package.For_solver.to_opam_file
356357
|> OpamFile.OPAM.with_url (OpamFile.URL.create (snd package.url))
@@ -360,7 +361,11 @@ let resolve (t : DB.t) ~(scan_project : Scan_project.t)
360361
(Package_name.to_opam_package_name package.name)
361362
(Package_version.to_opam_package_version package.version)
362363
in
363-
Resolved_package.dune_package package.loc opam_file opam_package
364+
Resolved_package.local_package
365+
~command_source:local_package.command_source
366+
package.loc
367+
opam_file
368+
opam_package
364369
in
365370
resolve package.name resolved_package
366371
in

src/dune_pkg/resolved_package.ml

+7-2
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,15 @@ let scan_files_entries path =
8484
])
8585
;;
8686

87-
let dune_package loc opam_file opam_package =
87+
let local_package ~command_source loc opam_file opam_package =
88+
let dune_build =
89+
match (command_source : Local_package.command_source) with
90+
| Assume_defaults -> true
91+
| Opam_file _ -> false
92+
in
8893
let opam_file = add_opam_package_to_opam_file opam_package opam_file in
8994
let package = OpamFile.OPAM.package opam_file in
90-
{ dune_build = true; opam_file; package; loc; extra_files = Inside_files_dir None }
95+
{ dune_build; opam_file; package; loc; extra_files = Inside_files_dir None }
9196
;;
9297

9398
open Fiber.O

src/dune_pkg/resolved_package.mli

+7-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,11 @@ val local_fs
2323
-> files_dir:Path.Local.t option
2424
-> t
2525

26-
val dune_package : Loc.t -> OpamFile.OPAM.t -> OpamPackage.t -> t
26+
val local_package
27+
: command_source:Local_package.command_source
28+
-> Loc.t
29+
-> OpamFile.OPAM.t
30+
-> OpamPackage.t
31+
-> t
32+
2733
val get_opam_package_files : t list -> File_entry.t list list Fiber.t

test/blackbox-tests/test-cases/pkg/pin-stanza/build-command-dune-project.t

+27-9
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@ Demonstrate the build command we construct for different types of projects:
55
$ mkrepo
66
$ add_mock_repo_if_needed
77

8-
$ mkdir _template _dune-only _mixed
8+
$ mkdir _template _dune-only _mixed _opam-only
99

1010
$ cat >_template/dune-project <<EOF
1111
> (lang dune 3.13)
1212
> (generate_opam_files true)
13-
> (package (name template))
13+
> (package (name template) (allow_empty))
1414
> EOF
1515
$ cat >_template/mixed.opam.template <<EOF
1616
> build: [ "echo" "template" ]
1717
> EOF
1818

1919
$ cat >_dune-only/dune-project <<EOF
2020
> (lang dune 3.13)
21-
> (package (name dune-only))
21+
> (package (name dune-only) (allow_empty))
2222
> EOF
2323

2424
$ cat >_mixed/dune-project <<EOF
@@ -29,6 +29,11 @@ Demonstrate the build command we construct for different types of projects:
2929
> build: [ "echo" "mixed" ]
3030
> EOF
3131

32+
$ cat > _opam-only/opam-only.opam <<EOF
33+
> opam-version: "2.0"
34+
> build: [ "echo" "opam only" ]
35+
> EOF
36+
3237
$ cat >dune-project <<EOF
3338
> (lang dune 3.13)
3439
> (pin
@@ -40,22 +45,35 @@ Demonstrate the build command we construct for different types of projects:
4045
> (pin
4146
> (url "$PWD/_mixed")
4247
> (package (name mixed)))
48+
> (pin
49+
> (url "$PWD/_opam-only")
50+
> (package (name opam-only)))
4351
> (package
4452
> (name main)
45-
> (depends dune-only mixed template))
53+
> (allow_empty)
54+
> (depends dune-only mixed template opam-only))
4655
> EOF
4756

4857
$ dune pkg lock
4958
Solution for dune.lock:
5059
- dune-only.dev
5160
- mixed.dev
61+
- opam-only.dev
5262
- template.dev
5363
$ build_command() {
54-
> grep "(dune)" dune.lock/$1.pkg
64+
> grep "$1" dune.lock/$2.pkg
5565
> }
56-
$ build_command dune-only
57-
(dune)
58-
$ build_command mixed
66+
$ build_command "(dune)" dune-only
5967
(dune)
60-
$ build_command template
68+
$ build_command "(dune)" template
6169
(dune)
70+
$ build_command "(build" mixed
71+
(build
72+
$ build_command "(build" opam-only
73+
(build
74+
75+
If we build the deps, everything works fine and we see the output of the opam
76+
pins:
77+
$ dune build @pkg-install
78+
mixed
79+
opam only

0 commit comments

Comments
 (0)