In what environment does the Arduino CLI issue commands?

System details:
Arduino IDE version 2.3.4 (CLI version 1.1.1)
Operating system: Windows 10 Home 22H2 10.0.19045.5247
Board: Arduino UNO

I'm trying to use platform.local.txt to perform a prebuild action. I first tried using echo as shown in the documentation here: arduino cli > pre- and postbuild hooks

I created a new file "AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\platform.local.txt" and added this line:
recipe.hooks.sketch.prebuild.1.pattern=echo foobar

However, I run into the following error.
exec: "echo": executable file not found in %PATH%

Output in Arduino IDE

FQBN: arduino:avr:uno
Using board 'uno' from platform in folder: C:\Users\user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Detecting libraries used...
[...]
Compiling sketch...
echo foobar
exec: "echo": executable file not found in %PATH%

Compilation error: exec: "echo": executable file not found in %PATH%

I can "fix" it with:
recipe.hooks.sketch.prebuild.1.pattern=cmd /.k echo foobar

Output in Arduino IDE

FQBN: arduino:avr:uno
Using board 'uno' from platform in folder: C:\Users\user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6
Using core 'arduino' from platform in folder: C:\Users\user\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6

Detecting libraries used...
[...]
Compiling sketch...
cmd /k echo foobar
foobar
[...]

I don't understand what environment I'm running the prebuild hooks commands from. I read in the documentation that the Arduino IDE is using the Arduino CLI in deamon mode and communicating with it using gRPC calls. I'll admit I don't know how that works, but I think I understand that on a basic level. I assumed that the CLI is then running other processes from within some shell and that "echo" would be a built-in command within that shell.

All of this left me quite confused... Is it just the fact that I'm on windows that the echo command does not work?

On Linux (and Mac probably)

$ which echo
/usr/bin/echo

But on Windows

PS C:\> Get-Command echo

CommandType     Name
-----------     ----
Alias           echo -> Write-Output

it's an alias or built-in

C:\>where where
C:\Windows\System32\where.exe

C:\>where echo
INFO: Could not find files for the given pattern(s).

When a program execs another program, that bypasses the shell: you can't run built-ins that way. You can of course exec the shell, as you did.

Hi @tkoopman. I think @kenb4 already explained the subject well. In case it might be useful, I'll add the detail that the exec package of the Go programming language is used to invoke the commands defined by Arduino boards platforms:

One thing that might be the cause of confusion is that the Git for Windows installation includes Git Bash, and Git Bash does provide an executable named echo.exe. So the build hook will work as expected for those who happen to have Git for Windows installed (assuming they have the bin folder of Git Bash in their system path, which is likely).

"Git for windows" isn't the only thing that will install an "echo.exe"
For example, Atmel Studio also has one, and there is/was a general purpose "shell utils" package of tools that installed "all the common unix commands" for people who wanted it.

@kenb4, @ptillisch, @westfw
That makes a lot of sense, thanks for the explanations and the link to the exec package docs!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.