diff --git a/src/import.ml b/src/import.ml index a50f77bd..31f1b9d7 100644 --- a/src/import.ml +++ b/src/import.ml @@ -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 diff --git a/src/path.ml b/src/path.ml index e7d2a1b8..5e663100 100644 --- a/src/path.ml +++ b/src/path.ml @@ -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 = diff --git a/src/path.mli b/src/path.mli index 3cc9d9cb..e2a45fed 100644 --- a/src/path.mli +++ b/src/path.mli @@ -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 diff --git a/src/super_context.ml b/src/super_context.ml index 6f91de29..f0179ba1 100644 --- a/src/super_context.ml +++ b/src/super_context.ml @@ -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 diff --git a/src/utils.ml b/src/utils.ml index 7f3af677..0223395e 100644 --- a/src/utils.ml +++ b/src/utils.ml @@ -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@}: Program %s not found in%s PATH%s%a" prog + die "@{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@}: Library %s not found%s%a" lib + die "@{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 "" (Path.to_string dir)) + sprintf "" + (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 "" (Path.to_string dir)) + ~desc:(fun _ -> sprintf "" + (Path.to_string_maybe_quoted dir)) let obj_name_of_basename fn = match String.index fn '.' with