Skip to content

Commit 92d2f25

Browse files
committed
Handle tilde in Path.expand/1,2 properly
1 parent 9d62e67 commit 92d2f25

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

lib/elixir/lib/path.ex

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -585,11 +585,23 @@ defmodule Path do
585585

586586
defp expand_home(type) do
587587
case IO.chardata_to_string(type) do
588-
"~" <> rest -> System.user_home! <> rest
588+
"~" <> rest -> resolve_home(rest)
589589
rest -> rest
590590
end
591591
end
592592

593+
defp resolve_home(""), do: System.user_home!
594+
595+
defp resolve_home(rest) do
596+
case {rest, major_os_type} do
597+
{"\\" <> _, :win32} ->
598+
System.user_home! <> rest
599+
{"/" <> _, _} ->
600+
System.user_home! <> rest
601+
_ -> rest
602+
end
603+
end
604+
593605
defp normalize(path), do: normalize(split(path), [])
594606

595607
defp normalize([".."|t], ["/"|_] = acc) do

lib/elixir/test/elixir/path_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ defmodule PathTest do
112112
assert Path.expand("~/file", "whatever") == Path.join(home, "file")
113113
assert Path.expand("file", Path.expand("~")) == Path.expand("~/file")
114114
assert Path.expand("file", "~") == Path.join(home, "file")
115+
assert Path.expand("~file") == Path.join(System.cwd!, "file")
115116
end
116117

117118
test :expand_path do

0 commit comments

Comments
 (0)