Skip to content

Commit 90ab8c9

Browse files
Skip version-manager hint when .tool-versions is unreadable
The Mix task should not suggest mise/asdf when failure is an I/O error from File.read. Document read failures in Toolchain.verify/2 and add a regression test using a directory named .tool-versions. Co-authored-by: Dominic Letz <dominicletz@users.noreply.github.com>
1 parent 4926e7f commit 90ab8c9

3 files changed

Lines changed: 21 additions & 7 deletions

File tree

lib/desktop/toolchain.ex

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ defmodule Desktop.Toolchain do
55
Verifies that the running Erlang/OTP and Elixir versions match `.tool-versions` in
66
`project_root` when that file declares `erlang` and/or `elixir` entries.
77
8-
Returns `{:ok, :no_tool_versions}` if the file is missing, or `{:ok, :verified}` on success.
8+
Returns `{:ok, :no_tool_versions}` if the file is missing, `{:ok, :verified}` on success,
9+
or `{:error, [message]}` when `.tool-versions` cannot be read or versions do not match.
910
1011
`opts` may override runtime versions for testing:
1112

lib/mix/tasks/desktop.check_toolchain.ex

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ defmodule Mix.Tasks.Desktop.CheckToolchain do
3232
{:error, messages} ->
3333
for msg <- messages, do: Mix.shell().error(msg)
3434

35-
Mix.shell().error("""
36-
Activate the versions in .tool-versions for this project, for example:
37-
mise install && mise exec -- mix desktop.check_toolchain
38-
or:
39-
asdf install && asdf exec mix desktop.check_toolchain
40-
""")
35+
unless toolchain_hint_irrelevant?(messages) do
36+
Mix.shell().error("""
37+
Activate the versions in .tool-versions for this project, for example:
38+
mise install && mise exec -- mix desktop.check_toolchain
39+
or:
40+
asdf install && asdf exec mix desktop.check_toolchain
41+
""")
42+
end
4143

4244
System.halt(1)
4345
end
@@ -54,4 +56,8 @@ defmodule Mix.Tasks.Desktop.CheckToolchain do
5456
defp mix_project? do
5557
Mix.Project.get() != nil
5658
end
59+
60+
defp toolchain_hint_irrelevant?(messages) do
61+
Enum.any?(messages, &match?("Could not read .tool-versions:" <> _, &1))
62+
end
5763
end

test/desktop/toolchain_test.exs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,11 @@ defmodule Desktop.ToolchainTest do
3939

4040
assert msg =~ "Elixir mismatch"
4141
end
42+
43+
test "error when .tool-versions cannot be read", %{root: root} do
44+
File.mkdir!(Path.join(root, ".tool-versions"))
45+
46+
assert {:error, [msg]} = Desktop.Toolchain.verify(root)
47+
assert msg =~ "Could not read .tool-versions"
48+
end
4249
end

0 commit comments

Comments
 (0)