Skip to content

Commit

Permalink
fix: name suggestion on opam context (ocaml#10723)
Browse files Browse the repository at this point in the history
Fixes ocaml#10721

Signed-off-by: Etienne Marais <[email protected]>
  • Loading branch information
maiste authored Jul 16, 2024
1 parent 55680a1 commit f4e4dab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
15 changes: 12 additions & 3 deletions src/dune_rules/workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,14 @@ module Context = struct
Common.equal base t.base && Opam_switch.equal switch t.switch
;;

let name_hint_opt name =
if String.is_prefix ~prefix:"/" name
then (
let context_name = Filename.basename name in
Some [ Pp.textf "(name %s) would be a valid context name" context_name ])
else None
;;

let decode =
let+ loc_switch, switch = field "switch" (located string)
and+ name = field_o "name" Context_name.decode
Expand All @@ -479,9 +487,10 @@ module Context = struct
| None ->
User_error.raise
~loc:loc_switch
[ Pp.textf "Generated context name %S is invalid" name
; Pp.text
"Please specify a context name manually with the (name ..) field"
?hints:(name_hint_opt name)
[ Pp.textf
"The name generated from this switch can not be used as a context \
name. Please use (name) to disambiguate."
])
in
let base = { base with targets = Target.add base.targets x; name } in
Expand Down
13 changes: 7 additions & 6 deletions test/blackbox-tests/test-cases/github10721.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
> (lang dune 3.0)
> (context
> (opam
> (switch /absolute/path/to/opam/switch)))
> (switch /absolute/path/to/opam/switchname)))
> EOF

$ dune build
File "dune-workspace", line 4, characters 10-39:
4 | (switch /absolute/path/to/opam/switch)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: Generated context name "/absolute/path/to/opam/switch" is invalid
Please specify a context name manually with the (name ..) field
File "dune-workspace", line 4, characters 10-43:
4 | (switch /absolute/path/to/opam/switchname)))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: The name generated from this switch can not be used as a context name.
Please use (name) to disambiguate.
Hint: (name switchname) would be a valid context name
[1]

0 comments on commit f4e4dab

Please sign in to comment.