Quote a few paths in error messages when necessary

This commit is contained in:
Jeremie Dimino 2017-06-26 13:33:01 +01:00
parent 52afe84c83
commit aed36a76dd
5 changed files with 30 additions and 12 deletions

View File

@ -475,6 +475,16 @@ let hint name candidates =
in
sprintf "\nHint: did you mean %s?" (mk_hint l)
(* [maybe_quoted s] is [s] if [s] doesn't need escaping according to OCaml lexing
conventions and [sprintf "%S" s] otherwise. *)
let maybe_quoted s =
let escaped = String.escaped s in
if s == escaped || s = escaped then
s
else
sprintf {|"%s"|} escaped
(* Disable file operations to force to use the IO module *)
let open_in = `Use_Io
let open_in_bin = `Use_Io

View File

@ -238,6 +238,9 @@ let to_string = function
| "" -> "."
| t -> t
let to_string_maybe_quoted t =
maybe_quoted (to_string t)
let root = ""
let relative t fn =

View File

@ -46,6 +46,9 @@ val kind : t -> Kind.t
val of_string : string -> t
val to_string : t -> string
(** [to_string_maybe_quoted t] is [maybe_quoted (to_string t)] *)
val to_string_maybe_quoted : t -> string
val root : t
val is_root : t -> bool

View File

@ -413,13 +413,13 @@ module Deps = struct
>>^ fun _ -> []
| Glob_files s -> begin
let path = Path.relative dir (expand_vars t ~scope ~dir s) in
let dir = Path.parent path in
let s = Path.basename path in
match Glob_lexer.parse_string s with
match Glob_lexer.parse_string (Path.basename path) with
| Ok re ->
let dir = Path.parent path in
Build.paths_glob ~dir (Re.compile re)
| Error (_pos, msg) ->
die "invalid glob in %s/jbuild: %s" (Path.to_string dir) msg
let loc = String_with_vars.loc s in
Loc.fail loc "invalid glob: %s" msg
end
| Files_recursively_in s ->
let path = Path.relative dir (expand_vars t ~scope ~dir s) in

View File

@ -67,10 +67,10 @@ let signal_name =
let jbuild_name_in ~dir =
match Path.extract_build_context dir with
| None ->
Path.to_string (Path.relative dir "jbuild")
Path.to_string_maybe_quoted (Path.relative dir "jbuild")
| Some (ctx_name, dir) ->
sprintf "%s (context %s)"
(Path.to_string (Path.relative dir "jbuild"))
(Path.to_string_maybe_quoted (Path.relative dir "jbuild"))
ctx_name
let describe_target fn =
@ -84,12 +84,12 @@ let describe_target fn =
assert (String.length digest = 32);
name
in
sprintf "alias %s" name
sprintf "alias %s" (maybe_quoted name)
| _ ->
Path.to_string fn
Path.to_string_maybe_quoted fn
let program_not_found ?context ?(in_the_tree=false) ?hint prog =
die "@{<error>Error@}: Program %s not found in%s PATH%s%a" prog
die "@{<error>Error@}: Program %s not found in%s PATH%s%a" (maybe_quoted prog)
(if in_the_tree then
" the tree or in"
else
@ -103,7 +103,7 @@ let program_not_found ?context ?(in_the_tree=false) ?hint prog =
hint
let library_not_found ?context ?hint lib =
die "@{<error>Error@}: Library %s not found%s%a" lib
die "@{<error>Error@}: Library %s not found%s%a" (maybe_quoted lib)
(match context with
| None -> ""
| Some name -> sprintf " (context: %s)" name)
@ -122,12 +122,14 @@ let find_module ~dir modules name =
String_map.find_exn name modules
~string_of_key:(sprintf "%S")
~desc:(fun _ ->
sprintf "<module name to module info in %s>" (Path.to_string dir))
sprintf "<module name to module info in %s>"
(Path.to_string_maybe_quoted dir))
let find_deps ~dir dep_graph name =
String_map.find_exn name dep_graph
~string_of_key:(sprintf "%S")
~desc:(fun _ -> sprintf "<dependency graph in %s>" (Path.to_string dir))
~desc:(fun _ -> sprintf "<dependency graph in %s>"
(Path.to_string_maybe_quoted dir))
let obj_name_of_basename fn =
match String.index fn '.' with