@@ -115,22 +115,35 @@ let rename_optional_file ~src ~dst =
115115(* Recursively move regular files from [src] to [dst] and return the set of
116116 moved files. *)
117117let rename_dir_recursively ~loc ~src_dir ~dst_dir =
118- let rec loop ~src ~ dst ~ dst_parent =
119- (match Fpath. mkdir dst with
118+ let rec loop ~src_dir ~ dst_dir =
119+ (match Fpath. mkdir ( Path.Build. to_string dst_dir) with
120120 | Created -> ()
121- | Already_exists -> (* CR-someday amokhov: Should we clear it? *) ()
121+ | Already_exists ->
122+ User_error. raise ~loc
123+ ~annots: [ User_error.Annot.Needs_stack_trace. make () ]
124+ [ Pp. textf
125+ " This rule defines a directory target %S whose name conflicts with \
126+ an internal directory used by Dune. Please use a different name."
127+ (Path.Build. drop_build_context_exn dst_dir
128+ |> Path.Source. to_string_maybe_quoted)
129+ ]
122130 | Missing_parent_directory -> assert false );
123- match Dune_filesystem_stubs. read_directory_with_kinds src with
131+ match
132+ Dune_filesystem_stubs. read_directory_with_kinds
133+ (Path.Build. to_string src_dir)
134+ with
124135 | Ok files ->
125136 List. concat_map files ~f: (fun (file , kind ) ->
126- let src = Filename. concat src file in
127- let dst = Filename. concat dst file in
128137 match (kind : Dune_filesystem_stubs.File_kind.t ) with
129138 | S_REG ->
130- Unix. rename src dst;
131- [ Path.Build. relative dst_parent file ]
139+ let src = Path.Build. relative src_dir file in
140+ let dst = Path.Build. relative dst_dir file in
141+ Unix. rename (Path.Build. to_string src) (Path.Build. to_string dst);
142+ [ dst ]
132143 | S_DIR ->
133- loop ~src ~dst ~dst_parent: (Path.Build. relative dst_parent file)
144+ loop
145+ ~src_dir: (Path.Build. relative src_dir file)
146+ ~dst_dir: (Path.Build. relative dst_dir file)
134147 | _ ->
135148 User_error. raise ~loc
136149 [ Pp. textf " Rule produced a file with unrecognised kind %S"
@@ -139,31 +152,27 @@ let rename_dir_recursively ~loc ~src_dir ~dst_dir =
139152 | Error (ENOENT, _ , _ ) ->
140153 User_error. raise ~loc
141154 [ Pp. textf " Rule failed to produce directory %S"
142- (Path. of_string src
143- |> Path. drop_optional_build_context_maybe_sandboxed
144- |> Path. to_string_maybe_quoted)
155+ (Path.Build. drop_build_context_maybe_sandboxed_exn src_dir
156+ |> Path.Source. to_string_maybe_quoted)
145157 ]
146158 | Error (unix_error , _ , _ ) ->
147159 User_error. raise ~loc
148160 [ Pp. textf " Rule produced unreadable directory %S"
149- (Path. of_string src
150- |> Path. drop_optional_build_context_maybe_sandboxed
151- |> Path. to_string_maybe_quoted)
161+ (Path.Build. drop_build_context_maybe_sandboxed_exn src_dir
162+ |> Path.Source. to_string_maybe_quoted)
152163 ; Pp. verbatim (Unix. error_message unix_error)
153164 ]
154165 in
155- loop
156- ~src: (Path.Build. to_string src_dir)
157- ~dst: (Path.Build. to_string dst_dir)
158- ~dst_parent: dst_dir
159- |> Path.Build.Set. of_list
166+ loop ~src_dir ~dst_dir |> Path.Build.Set. of_list
160167
161168let move_targets_to_build_dir t ~loc ~targets =
162- Targets. to_list_map targets
163- ~file: (fun target ->
164- rename_optional_file ~src: (map_path t target) ~dst: target)
165- ~dir: (fun target ->
166- rename_dir_recursively ~loc ~src_dir: (map_path t target) ~dst_dir: target)
167- |> snd |> Path.Build.Set. union_all
169+ let (_file_targets_renamed : unit list ), files_moved_in_directory_targets =
170+ Targets. to_list_map targets
171+ ~file: (fun target ->
172+ rename_optional_file ~src: (map_path t target) ~dst: target)
173+ ~dir: (fun target ->
174+ rename_dir_recursively ~loc ~src_dir: (map_path t target) ~dst_dir: target)
175+ in
176+ Path.Build.Set. union_all files_moved_in_directory_targets
168177
169178let destroy t = Path. rm_rf (Path. build t.dir)
0 commit comments