Skip to content

Commit

Permalink
Ensure full directory match on mix format, closes #12969
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Sep 28, 2023
1 parent c2a0d8e commit 08d2bb1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
11 changes: 9 additions & 2 deletions lib/mix/lib/mix/tasks/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,15 @@ defmodule Mix.Tasks.Format do

defp recur_formatter_opts_for_file(file, {formatter_opts, subs}) do
Enum.find_value(subs, formatter_opts, fn {sub, formatter_opts_and_subs} ->
if String.starts_with?(file, sub) do
recur_formatter_opts_for_file(file, formatter_opts_and_subs)
size = byte_size(sub)

case file do
<<prefix::binary-size(size), dir_separator, _::binary>>
when prefix == sub and dir_separator in [?\\, ?/] ->
recur_formatter_opts_for_file(file, formatter_opts_and_subs)

_ ->
nil
end
end)
end
Expand Down
9 changes: 8 additions & 1 deletion lib/mix/test/mix/tasks/format_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -573,11 +573,18 @@ defmodule Mix.Tasks.FormatTest do
test "reads exported configuration from subdirectories", context do
in_tmp(context.test, fn ->
File.write!(".formatter.exs", """
[subdirectories: ["lib"]]
[subdirectories: ["li", "lib"]]
""")

# We also create a directory called li to ensure files
# from lib won't accidentally match on li.
File.mkdir_p!("li")
File.mkdir_p!("lib")

File.write!("li/.formatter.exs", """
[inputs: "**/*", locals_without_parens: [other_fun: 2]]
""")

File.write!("lib/.formatter.exs", """
[inputs: "a.ex", locals_without_parens: [my_fun: 2]]
""")
Expand Down

0 comments on commit 08d2bb1

Please sign in to comment.