From b3b87fbe4da8afe2834f5e477018a83010568de2 Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 28 Apr 2026 11:43:36 +0200 Subject: [PATCH 1/3] Consistently return path as binary in relative_to_cwd Previously if path parameter was passed in as chardata it was returned as is on `:file.get_cwd` failure All `relative_to` and `join` code paths normalize to binary --- lib/elixir/lib/path.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/lib/path.ex b/lib/elixir/lib/path.ex index 46461f116c2..a72071b8574 100644 --- a/lib/elixir/lib/path.ex +++ b/lib/elixir/lib/path.ex @@ -485,7 +485,7 @@ defmodule Path do def relative_to_cwd(path, opts \\ []) when is_list(opts) do case :file.get_cwd() do {:ok, base} -> relative_to(path, IO.chardata_to_string(base), opts) - _ -> path + _ -> IO.chardata_to_string(path) end end From a9b7bc4dd84cc42f98f2f8ea1986c3103471072a Mon Sep 17 00:00:00 2001 From: Lukasz Samson Date: Tue, 28 Apr 2026 14:34:43 +0200 Subject: [PATCH 2/3] Add test coverage for relative_to_cwd when :file.get_cwd fails --- lib/elixir/test/elixir/path_test.exs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/lib/elixir/test/elixir/path_test.exs b/lib/elixir/test/elixir/path_test.exs index a95aaea74fd..718bb2c215e 100644 --- a/lib/elixir/test/elixir/path_test.exs +++ b/lib/elixir/test/elixir/path_test.exs @@ -430,3 +430,27 @@ defmodule PathTest do defp strip_drive_letter_if_windows(path), do: path end end + +defmodule PathCwdFailureTest do + use ExUnit.Case, async: false + + @tag :tmp_dir + @tag :unix + test "relative_to_cwd/2 returns a binary even when cwd cannot be retrieved", config do + {:ok, original} = :file.get_cwd() + tmp = Path.join(config.tmp_dir, "deleted") + File.mkdir_p!(tmp) + + try do + File.cd!(tmp) + File.rm_rf!(tmp) + assert {:error, _} = :file.get_cwd() + + assert Path.relative_to_cwd("foo/bar") == "foo/bar" + assert Path.relative_to_cwd(~c"foo/bar") == "foo/bar" + assert Path.relative_to_cwd(["foo", ?/, "bar"]) == "foo/bar" + after + :file.set_cwd(original) + end + end +end From 7e27b821d88cc17cc07039ffef5a269c13d20f07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Tue, 28 Apr 2026 19:34:41 +0200 Subject: [PATCH 3/3] Apply suggestion from @josevalim --- lib/elixir/test/elixir/path_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/elixir/test/elixir/path_test.exs b/lib/elixir/test/elixir/path_test.exs index 718bb2c215e..aaed0fc0861 100644 --- a/lib/elixir/test/elixir/path_test.exs +++ b/lib/elixir/test/elixir/path_test.exs @@ -431,7 +431,7 @@ defmodule PathTest do end end -defmodule PathCwdFailureTest do +defmodule Path.SyncTest do use ExUnit.Case, async: false @tag :tmp_dir