Skip to content

Commit

Permalink
Merge pull request #1096 from rgrinberg/missing-loc
Browse files Browse the repository at this point in the history
Missing loc
  • Loading branch information
rgrinberg authored Aug 6, 2018
2 parents 295ba11 + 66f2004 commit 7c597d8
Show file tree
Hide file tree
Showing 30 changed files with 141 additions and 61 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ next
- Fix placeholders in `dune subst` documentation (#1090, @emillon, thanks
@trefis for the bug report)

- Add locations to errors when a missing binary in PATH comes from a dune file
(#1096, fixes #1095, @rgrinberg)

1.0.1 (19/07/2018)
------------------

Expand Down
47 changes: 28 additions & 19 deletions src/action.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
open Import
open Sexp.Of_sexp

let ignore_loc k ~loc:_ = k

module Outputs = struct
include Action_intf.Outputs

Expand Down Expand Up @@ -257,10 +259,11 @@ module Prog = struct
{ context : string
; program : string
; hint : string option
; loc : Loc.t option
}

let raise { context ; program ; hint } =
Utils.program_not_found ?hint ~context program
let raise { context ; program ; hint ; loc } =
Utils.program_not_found ?hint ~loc ~context program
end

type t = (Path.t, Not_found.t) result
Expand Down Expand Up @@ -320,13 +323,13 @@ module Unresolved = struct
module Program = struct
type t =
| This of Path.t
| Search of string
| Search of Loc.t option * string

let of_string ~dir s =
let of_string ~dir ~loc s =
if String.contains s '/' then
This (Path.relative dir s)
else
Search s
Search (loc, s)
end

module type Uast = Action_intf.Ast
Expand All @@ -345,18 +348,20 @@ module Unresolved = struct
~f_string:(fun ~dir:_ x -> x)
~f_program:(fun ~dir:_ -> function
| This p -> Ok p
| Search s -> Ok (f s))
| Search (loc, s) -> Ok (f loc s))
end

let prog_and_args_of_values p ~dir =
let prog_and_args_of_values ~loc p ~dir =
match p with
| [] -> (Unresolved.Program.Search "", [])
| [] -> (Unresolved.Program.Search (loc, ""), [])
| Value.Dir p :: _ ->
die "%s is a directory and cannot be used as an executable"
(Path.to_string_maybe_quoted p)
| Value.Path p :: xs -> (This p, Value.L.to_strings ~dir xs)
| String s :: xs ->
(Unresolved.Program.of_string ~dir s, Value.L.to_strings ~dir xs)
( Unresolved.Program.of_string ~loc ~dir s
, Value.L.to_strings ~dir xs
)

module Unexpanded = struct
module type Uast = Action_intf.Ast
Expand Down Expand Up @@ -398,17 +403,19 @@ module Unexpanded = struct
module E = struct
let expand ~dir ~mode ~f ~l ~r =
Either.map ~l
~r:(fun s -> r (String_with_vars.expand s ~dir ~f ~mode) ~dir)
~r:(fun s ->
r ~loc:(Some (String_with_vars.loc s))
(String_with_vars.expand s ~dir ~f ~mode) ~dir)

let string =
expand ~mode:Single
~l:(fun x -> x)
~r:Value.to_string
~r:(ignore_loc Value.to_string)

let strings =
expand ~mode:Many
~l:(fun x -> [x])
~r:Value.L.to_strings
~r:(ignore_loc Value.L.to_strings)

let path e =
let error_loc =
Expand All @@ -417,7 +424,7 @@ module Unexpanded = struct
| Right r -> Some (String_with_vars.loc r) in
expand ~mode:Single
~l:(fun x -> x)
~r:Value.(to_path ?error_loc) e
~r:(ignore_loc (Value.(to_path ?error_loc))) e

let prog_and_args =
expand ~mode:Many
Expand Down Expand Up @@ -488,15 +495,17 @@ module Unexpanded = struct
module E = struct
let expand ~dir ~mode ~f ~map x =
match String_with_vars.partial_expand ~mode ~dir ~f x with
| Expanded e -> Left (map e ~dir)
| Expanded e ->
let loc = Some (String_with_vars.loc x) in
Left (map ~loc e ~dir)
| Unexpanded x -> Right x

let string = expand ~mode:Single ~map:Value.to_string
let strings = expand ~mode:Many ~map:Value.L.to_strings
let cat_strings = expand ~mode:Many ~map:Value.L.concat
let string = expand ~mode:Single ~map:(ignore_loc Value.to_string)
let strings = expand ~mode:Many ~map:(ignore_loc Value.L.to_strings)
let cat_strings = expand ~mode:Many ~map:(ignore_loc Value.L.concat)
let path x =
let error_loc = String_with_vars.loc x in
expand ~mode:Single ~map:(Value.to_path ~error_loc) x
expand ~mode:Single ~map:(fun ~loc v ~dir ->
Value.to_path ?error_loc:loc v ~dir) x
let prog_and_args = expand ~mode:Many ~map:prog_and_args_of_values
end

Expand Down
5 changes: 3 additions & 2 deletions src/action.mli
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module Prog : sig
{ context : string
; program : string
; hint : string option
; loc : Loc.t option
}

val raise : t -> _
Expand Down Expand Up @@ -54,15 +55,15 @@ module Unresolved : sig
module Program : sig
type t =
| This of Path.t
| Search of string
| Search of Loc.t option * string
end

include Action_intf.Ast
with type program := Program.t
with type path := Path.t
with type string := string

val resolve : t -> f:(string -> Path.t) -> action
val resolve : t -> f:(Loc.t option -> string -> Path.t) -> action
end with type action := t

module Unexpanded : sig
Expand Down
3 changes: 2 additions & 1 deletion src/artifacts.ml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let create (context : Context.t) ~public_libs l ~f =
; public_libs
}

let binary t ?hint name =
let binary t ?hint ~loc name =
if not (Filename.is_relative name) then
Ok (Path.of_filename_relative_to_initial_cwd name)
else
Expand All @@ -66,6 +66,7 @@ let binary t ?hint name =
program = name
; hint
; context = t.context.Context.name
; loc
}

let file_of_lib t ~loc ~lib ~file =
Expand Down
1 change: 1 addition & 0 deletions src/artifacts.mli
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ val create
val binary
: t
-> ?hint:string
-> loc:Loc.t option
-> string
-> Action.Prog.t

Expand Down
38 changes: 20 additions & 18 deletions src/build_system.ml
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ module Dir_status = struct
; action : (unit, Action.t) Build.t
; locks : Path.t list
; context : Context.t
; loc : Loc.t option
}


Expand Down Expand Up @@ -684,23 +685,23 @@ let remove_old_artifacts t ~dir ~subdirs_to_keep =
if not (Path.Table.mem t.files path) then Path.unlink path)

let no_rule_found =
let fail fn =
die "No rule found for %s" (Utils.describe_target fn)
let fail fn ~loc =
Loc.fail_opt loc "No rule found for %s" (Utils.describe_target fn)
in
fun t fn ->
fun t ~loc fn ->
match Utils.analyse_target fn with
| Other _ -> fail fn
| Other _ -> fail fn ~loc
| Regular (ctx, _) ->
if String.Map.mem t.contexts ctx then
fail fn
fail fn ~loc
else
die "Trying to build %s but build context %s doesn't exist.%s"
(Path.to_string_maybe_quoted fn)
ctx
(hint ctx (String.Map.keys t.contexts))
| Alias (ctx, fn') ->
if String.Map.mem t.contexts ctx then
fail fn
fail fn ~loc
else
let fn = Path.append (Path.relative Path.build_dir ctx) fn' in
die "Trying to build alias %s but build context %s doesn't exist.%s"
Expand Down Expand Up @@ -729,18 +730,18 @@ let rec compile_rule t ?(copy_source=false) pre_rule =

let eval_rule () =
t.hook Rule_started;
wait_for_deps t (Lazy.force static_deps).rule_deps
wait_for_deps t (Lazy.force static_deps).rule_deps ~loc
>>| fun () ->
Build_exec.exec t build ()
in
let exec_rule (rule_evaluation : Exec_status.rule_evaluation) =
let static_deps = (Lazy.force static_deps).action_deps in
Fiber.fork_and_join_unit
(fun () ->
wait_for_deps t static_deps)
wait_for_deps ~loc t static_deps)
(fun () ->
Fiber.Future.wait rule_evaluation >>= fun (action, dyn_deps) ->
wait_for_deps t (Path.Set.diff dyn_deps static_deps)
wait_for_deps ~loc t (Path.Set.diff dyn_deps static_deps)
>>| fun () ->
(action, dyn_deps))
>>= fun (action, dyn_deps) ->
Expand Down Expand Up @@ -949,13 +950,13 @@ and load_dir_step2_exn t ~dir ~collector ~lazy_generators =
let rules, deps =
List.fold_left actions ~init:(rules, deps)
~f:(fun (rules, deps)
{ Dir_status. stamp; action; locks ; context } ->
{ Dir_status. stamp; action; locks ; context ; loc } ->
let path =
Path.extend_basename base_path
~suffix:("-" ^ Digest.to_hex stamp)
in
let rule =
Pre_rule.make ~locks ~context:(Some context)
Pre_rule.make ~locks ~context:(Some context) ?loc
(Build.progn [ action; Build.create_file path ])
in
(rule :: rules, Path.Set.add deps path))
Expand Down Expand Up @@ -1107,7 +1108,7 @@ The following targets are not:

targets

and wait_for_file t fn =
and wait_for_file t ~loc fn =
match Path.Table.find t.files fn with
| Some file -> wait_for_file_found fn file
| None ->
Expand All @@ -1116,7 +1117,7 @@ and wait_for_file t fn =
load_dir t ~dir;
match Path.Table.find t.files fn with
| Some file -> wait_for_file_found fn file
| None -> no_rule_found t fn
| None -> no_rule_found t ~loc fn
end else if Path.exists fn then
Fiber.return ()
else
Expand Down Expand Up @@ -1146,8 +1147,8 @@ and wait_for_file_found fn (File_spec.T file) =
};
Fiber.Future.wait rule_execution)

and wait_for_deps t deps =
Fiber.parallel_iter (Path.Set.to_list deps) ~f:(wait_for_file t)
and wait_for_deps ~loc t deps =
Fiber.parallel_iter (Path.Set.to_list deps) ~f:(wait_for_file ~loc t)

let stamp_file_for_files_of t ~dir ~ext =
let files_of_dir =
Expand Down Expand Up @@ -1260,7 +1261,7 @@ let eval_request t ~request ~process_target =
Fiber.fork_and_join_unit
(fun () -> process_targets static_deps)
(fun () ->
wait_for_deps t rule_deps
wait_for_deps t ~loc:None rule_deps
>>= fun () ->
let result, dyn_deps = Build_exec.exec t request () in
process_targets (Path.Set.diff dyn_deps static_deps)
Expand All @@ -1285,7 +1286,7 @@ let update_universe t =
let do_build t ~request =
entry_point t ~f:(fun () ->
update_universe t;
eval_request t ~request ~process_target:(wait_for_file t))
eval_request t ~request ~process_target:(wait_for_file ~loc:None t))

module Ir_set = Set.Make(Internal_rule)

Expand Down Expand Up @@ -1616,12 +1617,13 @@ module Alias = struct
Build.fanout def.dyn_deps build >>^ fun (a, b) ->
Path.Set.union a b

let add_action build_system t ~context ?(locks=[]) ~stamp action =
let add_action build_system t ~context ~loc ?(locks=[]) ~stamp action =
let def = get_alias_def build_system t in
def.actions <- { stamp = Digest.string (Sexp.to_string ~syntax:Dune stamp)
; action
; locks
; context
; loc
} :: def.actions
end

Expand Down
1 change: 1 addition & 0 deletions src/build_system.mli
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ module Alias : sig
: build_system
-> t
-> context:Context.t
-> loc:Loc.t option
-> ?locks:Path.t list
-> stamp:Sexp.t
-> (unit, Action.t) Build.t
Expand Down
4 changes: 2 additions & 2 deletions src/context.ml
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ let create ~(kind : Kind.t) ~path ~env ~env_nodes ~name ~merlin ~targets
Hashtbl.add opam_var_cache "root" root
| Default -> ());
let prog_not_found_in_path prog =
Utils.program_not_found prog ~context:name
Utils.program_not_found prog ~context:name ~loc:None
in
let which_cache = Hashtbl.create 128 in
let which x = which ~cache:which_cache ~path x in
Expand Down Expand Up @@ -424,7 +424,7 @@ let default ?(merlin=true) ~env_nodes ~env ~targets () =
let create_for_opam ?root ~env ~env_nodes ~targets ~profile ~switch ~name
?(merlin=false) () =
match Bin.opam with
| None -> Utils.program_not_found "opam"
| None -> Utils.program_not_found "opam" ~loc:None
| Some fn ->
(match root with
| Some root -> Fiber.return root
Expand Down
3 changes: 2 additions & 1 deletion src/gen_rules.ml
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module Gen(P : Install_rules.Params) = struct
(* We have to execute the rule in the library directory as
the .o is produced in the current directory *)
~dir:(Path.parent_exn src)
(SC.resolve_program sctx ctx.c_compiler)
(SC.resolve_program ~loc:None sctx ctx.c_compiler)
([ S [A "-I"; Path ctx.stdlib_dir]
; As (SC.cxx_flags sctx)
; includes
Expand Down Expand Up @@ -608,6 +608,7 @@ module Gen(P : Install_rules.Params) = struct
; deps = t.deps
; action = None
; enabled_if = t.enabled_if
; loc
} in
match test_kind (loc, s) with
| `Regular ->
Expand Down
1 change: 1 addition & 0 deletions src/inline_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ include Sub_system.Register_end_point(
in

SC.add_alias_action sctx
~loc:(Some info.loc)
(Build_system.Alias.runtest ~dir)
~stamp:(List [ Sexp.unsafe_atom_of_string "ppx-runner"
; Quoted_string name
Expand Down
3 changes: 3 additions & 0 deletions src/jbuild.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1558,6 +1558,7 @@ module Alias_conf = struct
; locks : String_with_vars.t list
; package : Package.t option
; enabled_if : String_with_vars.t Blang.t option
; loc : Loc.t
}

let alias_name =
Expand All @@ -1570,6 +1571,7 @@ module Alias_conf = struct
let t =
record
(let%map name = field "name" alias_name
and loc = loc
and package = field_o "package" Pkg.t
and action = field_o "action" (located Action.Unexpanded.t)
and locks = field "locks" (list String_with_vars.t) ~default:[]
Expand All @@ -1582,6 +1584,7 @@ module Alias_conf = struct
; package
; locks
; enabled_if
; loc
})
end

Expand Down
Loading

0 comments on commit 7c597d8

Please sign in to comment.