From 26c6d26c07532db8f07d6db3e9d75794f62cd79c Mon Sep 17 00:00:00 2001 From: Masatoshi Nishiguchi <7563926+mnishiguchi@users.noreply.github.com> Date: Thu, 8 Jan 2026 20:56:52 +0900 Subject: [PATCH] Run ESP-IDF esptool.py via python when IDF_PATH is set --- lib/mix/tasks/esp32_flash.ex | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/lib/mix/tasks/esp32_flash.ex b/lib/mix/tasks/esp32_flash.ex index c8a4201..beb2b15 100644 --- a/lib/mix/tasks/esp32_flash.ex +++ b/lib/mix/tasks/esp32_flash.ex @@ -141,7 +141,14 @@ defmodule Mix.Tasks.Atomvm.Esp32.Flash do _ -> IO.puts("Flashing using esptool..") tool_full_path = get_esptool_path(idf_path) - System.cmd(tool_full_path, tool_args, stderr_to_stdout: true, into: IO.stream(:stdio, 1)) + {tool_exec, prefix_args} = resolve_esptool_exec(tool_full_path, idf_path) + + System.cmd( + tool_exec, + prefix_args ++ tool_args, + stderr_to_stdout: true, + into: IO.stream(:stdio, 1) + ) end end @@ -153,6 +160,33 @@ defmodule Mix.Tasks.Atomvm.Esp32.Flash do "#{idf_path}#{@esp_tool_path}" end + defp resolve_esptool_exec(tool_full_path, <<"">>) do + # IDF_PATH is not set: run esptool from PATH (usually "esptool.py"). + {tool_full_path, []} + end + + defp resolve_esptool_exec(tool_full_path, _idf_path) do + # IDF_PATH is set: tool_full_path is ESP-IDF's esptool.py. + # Some ESP-IDF installs ship it without the executable bit, so run it via python. + if not File.exists?(tool_full_path) do + Mix.raise(""" + IDF_PATH is set, but esptool.py was not found: #{tool_full_path} + Try: env -u IDF_PATH mix atomvm.esp32.flash ... (or install esptool) + """) + end + + python = System.find_executable("python") || System.find_executable("python3") + + if is_nil(python) do + Mix.raise(""" + IDF_PATH is set, but python is missing from PATH + Try: env -u IDF_PATH mix atomvm.esp32.flash ... (or install python3) + """) + end + + {python, [tool_full_path]} + end + defp parse_args(args) do parse_args(args, %{}) end