Skip to content

Commit e39a1ca

Browse files
authored
Consistently return path as binary in relative_to_cwd (#15316)
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
1 parent 007efde commit e39a1ca

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

lib/elixir/lib/path.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ defmodule Path do
485485
def relative_to_cwd(path, opts \\ []) when is_list(opts) do
486486
case :file.get_cwd() do
487487
{:ok, base} -> relative_to(path, IO.chardata_to_string(base), opts)
488-
_ -> path
488+
_ -> IO.chardata_to_string(path)
489489
end
490490
end
491491

lib/elixir/test/elixir/path_test.exs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,3 +430,27 @@ defmodule PathTest do
430430
defp strip_drive_letter_if_windows(path), do: path
431431
end
432432
end
433+
434+
defmodule Path.SyncTest do
435+
use ExUnit.Case, async: false
436+
437+
@tag :tmp_dir
438+
@tag :unix
439+
test "relative_to_cwd/2 returns a binary even when cwd cannot be retrieved", config do
440+
{:ok, original} = :file.get_cwd()
441+
tmp = Path.join(config.tmp_dir, "deleted")
442+
File.mkdir_p!(tmp)
443+
444+
try do
445+
File.cd!(tmp)
446+
File.rm_rf!(tmp)
447+
assert {:error, _} = :file.get_cwd()
448+
449+
assert Path.relative_to_cwd("foo/bar") == "foo/bar"
450+
assert Path.relative_to_cwd(~c"foo/bar") == "foo/bar"
451+
assert Path.relative_to_cwd(["foo", ?/, "bar"]) == "foo/bar"
452+
after
453+
:file.set_cwd(original)
454+
end
455+
end
456+
end

0 commit comments

Comments
 (0)