Skip to content

Commit fc3996b

Browse files
authored
pkg: handle opam string interp special case (#11498)
Handle the opam variable string interpolation syntax necessary for packages with a '+' in their name. Read more at https://opam.ocaml.org/doc/Manual.html#Variables. Signed-off-by: Stephen Sherratt <[email protected]>
1 parent 00c32ba commit fc3996b

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/dune_pkg/opam_solver.ml

+26-1
Original file line numberDiff line numberDiff line change
@@ -1497,8 +1497,33 @@ let opam_variable_to_slang ~loc packages variable =
14971497
Blang.Expr (convert_with_package_name package_name))))
14981498
;;
14991499

1500+
(* Handles the special case for packages whose names contain '+' characters
1501+
where a special form of string interpolation is used. From the opam manual:
1502+
Warning: if the package name contains a + character (e.g. conf-g++), their
1503+
variables may only be accessed using opam 2.2 via string interpolation,
1504+
with the following syntax:
1505+
1506+
"%{?conf-g++:your-variable:}%"
1507+
*)
1508+
let desugar_special_string_interpolation_syntax
1509+
((packages, variable, string_converter) as fident)
1510+
=
1511+
match string_converter with
1512+
| Some (package_and_variable, "")
1513+
when List.is_empty packages && String.is_empty (OpamVariable.to_string variable) ->
1514+
(match String.lsplit2 package_and_variable ~on:':' with
1515+
| Some (package, variable) ->
1516+
( [ Some (OpamPackage.Name.of_string package) ]
1517+
, OpamVariable.of_string variable
1518+
, None )
1519+
| None -> fident)
1520+
| _ -> fident
1521+
;;
1522+
15001523
let opam_fident_to_slang ~loc fident =
1501-
let packages, variable, string_converter = OpamFilter.desugar_fident fident in
1524+
let packages, variable, string_converter =
1525+
OpamFilter.desugar_fident fident |> desugar_special_string_interpolation_syntax
1526+
in
15021527
let slang = opam_variable_to_slang ~loc packages variable in
15031528
match string_converter with
15041529
| None -> slang
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Ensure dune can handle the special string interpolation syntax used by opam for
2+
packages whose names contain a '+' character. This syntax is described in
3+
https://opam.ocaml.org/doc/Manual.html#Variables
4+
5+
$ . ./helpers.sh
6+
$ mkrepo
7+
$ add_mock_repo_if_needed
8+
9+
This is based on the build command of mingw-w64-shims.0.2.0
10+
$ mkpkg foo <<EOF
11+
> build: [
12+
> [ "echo" "i686-gcc-g++-%{?conf-mingw-w64-g++-i686:installed:}%" ]
13+
> ]
14+
> EOF
15+
16+
$ cat > dune-project <<EOF
17+
> (lang dune 3.17)
18+
> (package
19+
> (name bar)
20+
> (depends foo))
21+
> EOF
22+
23+
$ dune pkg lock
24+
Solution for dune.lock:
25+
- foo.0.0.1

0 commit comments

Comments
 (0)