Skip to content

Commit 18c1f60

Browse files
Relocate error messages
1 parent 502e507 commit 18c1f60

File tree

10 files changed

+79
-4
lines changed

10 files changed

+79
-4
lines changed

src/dune_lang/action.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ val encode : t Encoder.t
124124
val decode_dune_file : t Decoder.t
125125
val decode_pkg : t Decoder.t
126126

127+
(** Transform the string in the action *)
128+
val map_string_with_vars : t -> f:(String_with_vars.t -> String_with_vars.t) -> t
129+
127130
(** Raises User_error on invalid action. *)
128131
val validate : loc:Loc.t -> t -> unit
129132

src/dune_lang/slang.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ let loc = function
194194
| Form (loc, _form) -> loc
195195
;;
196196

197+
let map_loc ~f = function
198+
| Nil -> Nil
199+
| Literal sw ->
200+
let loc = f (String_with_vars.loc sw) in
201+
Literal (String_with_vars.with_loc ~loc sw)
202+
| Form (loc, form) ->
203+
let loc = f loc in
204+
Form (loc, form)
205+
;;
206+
197207
let concat ?(loc = Loc.none) ts = Form (loc, Concat ts)
198208
let when_ ?(loc = Loc.none) condition t = Form (loc, When (condition, t))
199209

src/dune_lang/slang.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ val decode : t Decoder.t
4040
val encode : t Encoder.t
4141
val to_dyn : t -> Dyn.t
4242
val loc : t -> Loc.t
43+
val map_loc : f:(Loc.t -> Loc.t) -> t -> t
4344
val concat : ?loc:Loc.t -> t list -> t
4445
val when_ : ?loc:Loc.t -> blang -> t -> t
4546
val if_ : ?loc:Loc.t -> blang -> then_:t -> else_:t -> t

src/dune_lang/string_with_vars.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ let decode_manually f =
113113

114114
let decode = decode_manually Pform.Env.parse
115115
let loc t = t.loc
116+
let with_loc t ~loc = { t with loc }
116117

117118
let virt_pform ?quoted pos pform =
118119
let loc = Loc.of_pos pos in

src/dune_lang/string_with_vars.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ val equal_no_loc : t -> t -> bool
1616
(** [loc t] returns the location of [t] — typically, in the [dune] file. *)
1717
val loc : t -> Loc.t
1818

19+
(** [with_loc t ~loc] returns a new value with the new location set. *)
20+
val with_loc : t -> loc:Loc.t -> t
21+
1922
val to_dyn : t Dyn.builder
2023

2124
include Conv.S with type t := t

src/dune_pkg/lock_dir.ml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,6 +1699,33 @@ let merge_conditionals a b =
16991699
{ a with packages; solved_for_platforms }
17001700
;;
17011701

1702+
let in_source_tree path =
1703+
match (path : Path.t) with
1704+
| In_source_tree s -> s
1705+
| In_build_dir b ->
1706+
let in_source = Path.drop_build_context_exn path in
1707+
(match Path.Source.explode in_source with
1708+
| "default" :: ".lock" :: components ->
1709+
Path.Source.L.relative Path.Source.root components
1710+
| _otherwise ->
1711+
Code_error.raise
1712+
"Unexpected location of lock directory in build directory"
1713+
[ "path", Path.Build.to_dyn b; "in_source", Path.Source.to_dyn in_source ])
1714+
| External e ->
1715+
Code_error.raise
1716+
"External path returned when loading a lock dir"
1717+
[ "path", Path.External.to_dyn e ]
1718+
;;
1719+
1720+
let loc_in_source_tree loc =
1721+
loc
1722+
|> Loc.map_pos ~f:(fun ({ pos_fname; _ } as pos) ->
1723+
let path = Path.of_string pos_fname in
1724+
let new_path = in_source_tree path in
1725+
let pos_fname = Path.Source.to_string new_path in
1726+
{ pos with pos_fname })
1727+
;;
1728+
17021729
let check_if_solved_for_platform { solved_for_platforms; _ } ~platform =
17031730
let loc, solved_for_platforms = solved_for_platforms in
17041731
if List.is_empty solved_for_platforms
@@ -1710,6 +1737,7 @@ let check_if_solved_for_platform { solved_for_platforms; _ } ~platform =
17101737
match Solver_env_disjunction.matches_platform solved_for_platforms ~platform with
17111738
| true -> ()
17121739
| false ->
1740+
let loc = loc_in_source_tree loc in
17131741
User_error.raise
17141742
~loc
17151743
[ Pp.text

src/dune_pkg/lock_dir.mli

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,11 @@ val merge_conditionals : t -> t -> t
181181
the lockdir does not contain a solution compatible with the given platform
182182
then a [User_error] is raised. *)
183183
val packages_on_platform : t -> platform:Solver_env.t -> Pkg.t Package_name.Map.t
184+
185+
(** Returns the path that the lock dir would be in the source. Might return
186+
paths that don't exist, if the lock dir wasn't copied from there. *)
187+
val in_source_tree : Path.t -> Path.Source.t
188+
189+
(** Returns a [Loc.t] which refers to the source tree lock dir path instead of
190+
the build dir lock dir path. *)
191+
val loc_in_source_tree : Loc.t -> Loc.t

src/dune_rules/fetch_rules.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ module Spec = struct
139139
~loc:loc_url
140140
[ Pp.text "No checksum provided. It should be:"; Checksum.pp actual_checksum ]
141141
| Some (loc, _) ->
142+
let loc = Dune_pkg.Lock_dir.loc_in_source_tree loc in
142143
User_error.raise
143144
~loc
144145
[ Pp.text "Invalid checksum, got"; Dune_pkg.Checksum.pp actual_checksum ])
145146
| Error (Unavailable message) ->
146-
let loc = loc_url in
147147
(match message with
148-
| None -> User_error.raise ~loc [ Pp.text "Unknown fetch failure" ]
149-
| Some msg -> User_error.raise ~loc [ User_message.pp msg ])
148+
| None -> User_error.raise ~loc:loc_url [ Pp.text "Unknown fetch failure" ]
149+
| Some msg -> User_error.raise ~loc:loc_url [ User_message.pp msg ])
150150
;;
151151
end
152152

@@ -220,6 +220,7 @@ let find_checksum, find_url =
220220
;;
221221

222222
let gen_rules_for_checksum_or_url (loc_url, (url : OpamUrl.t)) checksum =
223+
let loc_url = Dune_pkg.Lock_dir.loc_in_source_tree loc_url in
223224
let checksum_or_url =
224225
match checksum with
225226
| Some (_, checksum) -> `Checksum checksum

src/dune_rules/gen_rules.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,9 @@ let raise_on_lock_dir_out_of_sync =
680680
with
681681
| `Valid -> ()
682682
| `Invalid ->
683-
let loc = Loc.in_file (Path.relative path "lock.dune") in
683+
let source_path = Dune_pkg.Lock_dir.in_source_tree path in
684+
let loc_path = Path.source source_path in
685+
let loc = Loc.in_file (Path.relative loc_path "lock.dune") in
684686
let hints = Pp.[ text "run dune pkg lock" ] in
685687
User_error.raise
686688
~loc

src/dune_rules/pkg_rules.ml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,10 @@ module Action_expander = struct
796796
;;
797797

798798
let eval_slangs_located t slangs =
799+
let slangs =
800+
List.map slangs ~f:(fun slang ->
801+
Slang.map_loc slang ~f:Dune_pkg.Lock_dir.loc_in_source_tree)
802+
in
799803
Slang_expand.eval_multi_located slangs ~dir:t.paths.source_dir ~f:(slang_expander t)
800804
;;
801805

@@ -1157,6 +1161,18 @@ end = struct
11571161
let to_dyn = Dyn.opaque
11581162
end
11591163

1164+
let relocate action =
1165+
Dune_lang.Action.map_string_with_vars action ~f:(fun sw ->
1166+
let loc = sw |> String_with_vars.loc |> Dune_pkg.Lock_dir.loc_in_source_tree in
1167+
String_with_vars.with_loc sw ~loc)
1168+
;;
1169+
1170+
let relocate_build b =
1171+
match (b : Build_command.t) with
1172+
| Dune -> Build_command.Dune
1173+
| Action a -> Build_command.Action (relocate a)
1174+
;;
1175+
11601176
let resolve_impl { Input.db; package = name; universe = package_universe } =
11611177
match Package.Name.Map.find db.all name with
11621178
| None -> Memo.return None
@@ -1238,7 +1254,9 @@ end = struct
12381254
let id = Pkg.Id.gen () in
12391255
let write_paths = Paths.make package_universe name ~relative:Path.Build.relative in
12401256
let install_command = choose_for_current_platform install_command in
1257+
let install_command = Option.map install_command ~f:relocate in
12411258
let build_command = choose_for_current_platform build_command in
1259+
let build_command = Option.map build_command ~f:relocate_build in
12421260
let paths =
12431261
let paths = Paths.map_path write_paths ~f:Path.build in
12441262
match Pkg_toolchain.is_compiler_and_toolchains_enabled info.name with

0 commit comments

Comments
 (0)