Skip to content

compile --preprocess panics if #included file not found #1947

Open
@per1234

Description

@per1234

Describe the problem

If a sketch contains an #include directive for a file not present in any of the search paths, the arduino-cli compile --preprocess command panics.

To reproduce

$ arduino-cli version
arduino-cli.exe  Version: git-snapshot Commit: 13f22553 Date: 2022-10-26T06:09:43Z

$ mkdir /tmp/FooSketch

$ printf "#include <nonexistent.h>\nvoid setup() {}\nvoid loop() {}" > /tmp/FooSketch/FooSketch.ino

$ arduino-cli compile --fqbn arduino:avr:uno --preprocess /tmp/FooSketch/FooSketch.ino
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:1:10: fatal error: nonexistent.h: No such file or directory
 #include <nonexistent.h>
          ^~~~~~~~~~~~~~~
compilation terminated.
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0xc1f6f6]

goroutine 1 [running]:
github.com/arduino/arduino-cli/commands/compile.Compile({0x17432a0, 0xe5c9cd}, 0xc00014c160, {0x10cb900, 0xc000006018}, {0x10cb900, 0xc000006020}, 0x0, 0xc0)
        E:/electronics/git-nobackup/arduino-cli/commands/compile/compile.go:198 +0x1456
github.com/arduino/arduino-cli/cli/compile.runCompileCommand(0xc000359400, {0xc0002a47c0, 0x1, 0x4})
        E:/electronics/git-nobackup/arduino-cli/cli/compile/compile.go:235 +0xaaa
github.com/spf13/cobra.(*Command).execute(0xc000359400, {0xc0002a4780, 0x4, 0x4})
        C:/Users/per/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:860 +0x5f8
github.com/spf13/cobra.(*Command).ExecuteC(0xc000275b80)
        C:/Users/per/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:974 +0x3bc
github.com/spf13/cobra.(*Command).Execute(...)
        C:/Users/per/go/pkg/mod/github.com/spf13/cobra@v1.2.1/command.go:902
main.main()
        E:/electronics/git-nobackup/arduino-cli/main.go:31 +0x77

🐛 The stack trace from the panic does not provide any information of value to the user and may give them the impression the error was the result of a bug in Arduino CLI instead of with their sketch code.

Expected behavior

Arduino CLI only panics on unexpected (i.e., caused by a bug in the Arduino CLI codebase) failures. Any failure that can result from normal (even if incorrect) usage should be handled gracefully.

Arduino CLI version

13f2255

Operating system

Windows

Operating system version

10

Additional context

I bisected the introduction of the bug to 75b9760 / #1251

Before that, the output under these conditions was the more friendly:

$ arduino-cli compile --fqbn arduino:avr:uno --preprocess /tmp/FooSketch/FooSketch.ino
C:\Users\per\AppData\Local\Temp\FooSketch\FooSketch.ino:1:10: fatal error: nonexistent.h: No such file or directory
 #include <nonexistent.h>
          ^~~~~~~~~~~~~~~
compilation terminated.

Error during build: exit status 1

Issue checklist

  • I searched for previous reports in the issue tracker
  • I verified the problem still occurs when using the nightly build
  • My report contains all necessary details

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic: codeRelated to content of the project itselftype: imperfectionPerceived defect in any part of project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions