Skip to content

Commit bfe7315

Browse files
committed
File.cp_r reports non-existing dest dir properly (#14929)
1 parent 28f4fdc commit bfe7315

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

lib/elixir/lib/file.ex

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1224,7 +1224,13 @@ defmodule File do
12241224
defp do_cp_r(src, dest, on_conflict, dereference?, acc) when is_list(acc) do
12251225
case :elixir_utils.read_link_type(src) do
12261226
{:ok, :regular} ->
1227-
do_cp_file(src, dest, on_conflict, acc)
1227+
case do_cp_file(src, dest, on_conflict, acc) do
1228+
# we don't have a way to make a distinction between a non-existing src
1229+
# or dest being a non-existing dir in the case of :enoent,
1230+
# but we already know that src exists here.
1231+
{:error, :enoent, _} -> {:error, :enoent, dest}
1232+
other -> other
1233+
end
12281234

12291235
{:ok, :symlink} ->
12301236
case :file.read_link(src) do

lib/elixir/test/elixir/file_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,18 @@ defmodule FileTest do
787787
end
788788
end
789789

790+
test "cp_r! with file src and dest unknown" do
791+
src = fixture_path("cp_r/a/1.txt")
792+
dest = tmp_path("tmp/unknown/")
793+
794+
message =
795+
"could not copy recursively from #{inspect(src)} to #{inspect(dest)}. #{dest}: no such file or directory"
796+
797+
assert_raise File.CopyError, message, fn ->
798+
File.cp_r!(src, dest)
799+
end
800+
end
801+
790802
test "cp preserves mode" do
791803
File.mkdir_p!(tmp_path("tmp"))
792804
src = fixture_path("cp_mode")

0 commit comments

Comments
 (0)