Skip to content

Commit 033c738

Browse files
Dmitry KakurinJosé Valim
authored andcommitted
Fix Path.absname/1 to correctly handle UNC paths on Windows (#9689)
1 parent a72a388 commit 033c738

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/elixir/lib/path.ex

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ defmodule Path do
9595
absname(absname_join(name), cwd)
9696
end
9797

98+
@slash [?/, ?\\]
99+
98100
# Joins a list
99101
defp absname_join([name1, name2 | rest]), do: absname_join([absname_join(name1, name2) | rest])
100102

@@ -110,6 +112,11 @@ defmodule Path do
110112
do_absname_join(rest, relativename, [?:, uc_letter + ?a - ?A], :win32)
111113
end
112114

115+
defp do_absname_join(<<c1, c2, rest::binary>>, relativename, [], :win32)
116+
when c1 in @slash and c2 in @slash do
117+
do_absname_join(rest, relativename, '//', :win32)
118+
end
119+
113120
defp do_absname_join(<<?\\, rest::binary>>, relativename, result, :win32),
114121
do: do_absname_join(<<?/, rest::binary>>, relativename, result, :win32)
115122

@@ -254,8 +261,6 @@ defmodule Path do
254261
defp unix_pathtype([list | rest]) when is_list(list), do: unix_pathtype(list ++ rest)
255262
defp unix_pathtype(relative), do: {:relative, relative}
256263

257-
@slash [?/, ?\\]
258-
259264
defp win32_pathtype([list | rest]) when is_list(list), do: win32_pathtype(list ++ rest)
260265

261266
defp win32_pathtype([char, list | rest]) when is_list(list),

lib/elixir/test/elixir/path_test.exs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ defmodule PathTest do
3939
describe "Windows" do
4040
@describetag :windows
4141

42+
test "absname/1" do
43+
assert Path.absname("//host/path") == "//host/path"
44+
assert Path.absname("\\\\host\\path") == "//host/path"
45+
assert Path.absname("\\/host\\path") == "//host/path"
46+
assert Path.absname("/\\host\\path") == "//host/path"
47+
end
48+
4249
test "relative/1" do
4350
assert Path.relative("C:/usr/local/bin") == "usr/local/bin"
4451
assert Path.relative("C:\\usr\\local\\bin") == "usr\\local\\bin"
@@ -67,6 +74,11 @@ defmodule PathTest do
6774
assert Path.type("/usr/local/bin") == :volumerelative
6875
assert Path.type('usr/local/bin') == :relative
6976
assert Path.type("../usr/local/bin") == :relative
77+
78+
assert Path.type("//host/path") == :absolute
79+
assert Path.type("\\\\host\\path") == :absolute
80+
assert Path.type("/\\host\\path") == :absolute
81+
assert Path.type("\\/host\\path") == :absolute
7082
end
7183

7284
test "split/1" do

0 commit comments

Comments
 (0)