Skip to content

Commit 36702e8

Browse files
committed
fix: vendor Path.relative_to to get 1.18.4 behavior
1 parent 1c3f114 commit 36702e8

1 file changed

Lines changed: 79 additions & 5 deletions

File tree

lib/igniter.ex

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,8 +1510,8 @@ defmodule Igniter do
15101510
def format(igniter, adding_paths, reevaluate_igniter_config? \\ true) do
15111511
igniter =
15121512
igniter
1513-
|> include_existing_elixir_file("config/config.exs", require?: false)
1514-
|> include_existing_elixir_file("config/#{Mix.env()}.exs", require?: false)
1513+
|> include_existing_elixir_file("config/config.exs")
1514+
|> include_existing_elixir_file("config/#{Mix.env()}.exs")
15151515

15161516
if adding_paths &&
15171517
Enum.any?(List.wrap(adding_paths), &(Path.basename(&1) == ".formatter.exs")) do
@@ -1706,7 +1706,13 @@ defmodule Igniter do
17061706
source_handler.from_string(content, path: path)
17071707
|> Map.put(:from, :file)
17081708
else
1709-
raise "File #{path} not found in test files."
1709+
raise """
1710+
File #{path} not found in test files.
1711+
1712+
Available Files:
1713+
1714+
#{Enum.map_join(Map.keys(igniter.assigns[:test_files]), "\n", &" * #{&1}")}
1715+
"""
17101716
end
17111717
else
17121718
source_handler.read!(path)
@@ -1985,8 +1991,8 @@ defmodule Igniter do
19851991
:unavailable
19861992
end
19871993

1988-
defp subdirectory?(path, base_path) do
1989-
case Path.relative_to(path, base_path) do
1994+
def subdirectory?(path, base_path) do
1995+
case relative_to(path, base_path) do
19901996
# Same path, not a subdirectory
19911997
^base_path ->
19921998
false
@@ -2001,4 +2007,72 @@ defmodule Igniter do
20012007
end
20022008
end
20032009
end
2010+
2011+
defp relative_to(path, cwd, opts \\ []) when is_list(opts) do
2012+
os_type = :os.type() |> elem(0)
2013+
split_path = Path.split(path)
2014+
split_cwd = Path.split(cwd)
2015+
force = Keyword.get(opts, :force, false)
2016+
2017+
case {split_absolute?(split_path, os_type), split_absolute?(split_cwd, os_type)} do
2018+
{true, true} ->
2019+
split_path = expand_split(split_path)
2020+
split_cwd = expand_split(split_cwd)
2021+
2022+
case force do
2023+
true -> relative_to_forced(split_path, split_cwd, split_path)
2024+
false -> relative_to_unforced(split_path, split_cwd, split_path)
2025+
end
2026+
2027+
{false, false} ->
2028+
split_path = expand_relative(split_path, [], [])
2029+
split_cwd = expand_relative(split_cwd, [], [])
2030+
relative_to_forced(split_path, split_cwd, [])
2031+
2032+
{_, _} ->
2033+
Path.join(expand_relative(split_path, [], []))
2034+
end
2035+
end
2036+
2037+
defp relative_to_unforced(path, path, _original), do: "."
2038+
2039+
defp relative_to_unforced([h | t1], [h | t2], original),
2040+
do: relative_to_unforced(t1, t2, original)
2041+
2042+
defp relative_to_unforced([_ | _] = l1, [], _original), do: Path.join(l1)
2043+
defp relative_to_unforced(_, _, original), do: Path.join(original)
2044+
2045+
defp relative_to_forced(path, path, _original), do: "."
2046+
defp relative_to_forced(["."], _path, _original), do: "."
2047+
defp relative_to_forced(path, ["."], _original), do: Path.join(path)
2048+
defp relative_to_forced([h | t1], [h | t2], original), do: relative_to_forced(t1, t2, original)
2049+
2050+
# this should only happen if we have two paths on different drives on windows
2051+
defp relative_to_forced(original, _, original), do: Path.join(original)
2052+
2053+
defp relative_to_forced(l1, l2, _original) do
2054+
base = List.duplicate("..", length(l2))
2055+
Path.join(base ++ l1)
2056+
end
2057+
2058+
defp expand_relative([".." | t], [_ | acc], up), do: expand_relative(t, acc, up)
2059+
defp expand_relative([".." | t], acc, up), do: expand_relative(t, acc, [".." | up])
2060+
defp expand_relative(["." | t], acc, up), do: expand_relative(t, acc, up)
2061+
defp expand_relative([h | t], acc, up), do: expand_relative(t, [h | acc], up)
2062+
defp expand_relative([], [], []), do: ["."]
2063+
defp expand_relative([], acc, up), do: up ++ :lists.reverse(acc)
2064+
2065+
defp expand_split([head | tail]), do: expand_split(tail, [head])
2066+
defp expand_split([".." | t], [_, last | acc]), do: expand_split(t, [last | acc])
2067+
defp expand_split([".." | t], acc), do: expand_split(t, acc)
2068+
defp expand_split(["." | t], acc), do: expand_split(t, acc)
2069+
defp expand_split([h | t], acc), do: expand_split(t, [h | acc])
2070+
defp expand_split([], acc), do: :lists.reverse(acc)
2071+
2072+
defp split_absolute?(split, :win32), do: win32_split_absolute?(split)
2073+
defp split_absolute?(split, _), do: match?(["/" | _], split)
2074+
2075+
defp win32_split_absolute?(["//" | _]), do: true
2076+
defp win32_split_absolute?([<<_, ":/">> | _]), do: true
2077+
defp win32_split_absolute?(_), do: false
20042078
end

0 commit comments

Comments
 (0)