Skip to content

Commit c403d67

Browse files
committed
Surface the real download error instead of a misleading file-not-found
When a precompiled NIF download failed, the error from Artefact.download/2 was discarded and verify_and_decompress/2 then ran on the missing file, reporting a misleading :enoent. Capture the download result and only verify on success.
1 parent fbfab22 commit c403d67

2 files changed

Lines changed: 41 additions & 7 deletions

File tree

lib/mix/tasks/compile.elixir_make.ex

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -231,16 +231,20 @@ defmodule Mix.Tasks.Compile.ElixirMake do
231231
{:ok, target, nif_version_to_use, url} ->
232232
archived_fullpath = Artefact.archive_path(config, target, nif_version_to_use)
233233

234-
unless File.exists?(archived_fullpath) do
235-
Mix.shell().info("Downloading precompiled NIF to #{archived_fullpath}")
234+
download =
235+
unless File.exists?(archived_fullpath) do
236+
Mix.shell().info("Downloading precompiled NIF to #{archived_fullpath}")
236237

237-
with {:ok, archived_data} <- Artefact.download(config, url) do
238-
File.mkdir_p(Path.dirname(archived_fullpath))
239-
File.write(archived_fullpath, archived_data)
238+
with {:ok, archived_data} <- Artefact.download(config, url) do
239+
File.mkdir_p(Path.dirname(archived_fullpath))
240+
File.write(archived_fullpath, archived_data)
241+
end
240242
end
241-
end
242243

243-
Artefact.verify_and_decompress(archived_fullpath, app_priv)
244+
case download do
245+
{:error, _} = error -> error
246+
_ -> Artefact.verify_and_decompress(archived_fullpath, app_priv)
247+
end
244248

245249
{:error, msg} ->
246250
{:error, msg}

test/mix/tasks/compile.make_test.exs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,29 @@ defmodule Mix.Tasks.Compile.ElixirMakeTest do
421421
end)
422422
end
423423

424+
test "surfaces the download error instead of a misleading file-not-found" do
425+
in_fixture(fn ->
426+
File.mkdir!("priv")
427+
428+
File.write("Makefile", """
429+
all:
430+
\t@touch priv/my_app
431+
""")
432+
433+
with_project_config(
434+
[
435+
make_precompiler: {:nif, MyApp.Precompiler},
436+
make_precompiler_url: "https://example.com/@{artefact_filename}",
437+
make_precompiler_downloader: MyApp.FailingDownloader
438+
],
439+
fn ->
440+
System.put_env("ELIXIR_MAKE_CACHE_DIR", "./cache")
441+
assert capture_io(:stderr, fn -> run([]) end) =~ "download boom"
442+
end
443+
)
444+
end)
445+
end
446+
424447
defp in_fixture(fun) do
425448
File.cd!(@fixture_project, fun)
426449
end
@@ -440,3 +463,10 @@ defmodule MyApp.Downloader do
440463
File.read(path)
441464
end
442465
end
466+
467+
defmodule MyApp.FailingDownloader do
468+
@behaviour ElixirMake.Downloader
469+
470+
@impl true
471+
def download(_url), do: {:error, "download boom"}
472+
end

0 commit comments

Comments
 (0)