Skip to content

Commit 7fc6ab3

Browse files
authored
Merge pull request #115 from joewreschnig/autopath
Automatically detect the path of `clangd`, `arduino-cli`, and its configuration
2 parents 5c1c667 + 6fa5799 commit 7fc6ab3

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ The prerequisites to run the Arduino Language Server are:
7070
- [Arduino CLI](https://github.com/arduino/arduino-cli)
7171
- [clangd](https://github.com/clangd/clangd/releases)
7272

73-
To start the language server the IDE must provide the path to Arduino CLI and clangd with the following flags in addition to the target board FQBN:
73+
To start the language server the IDE may provide the path to Arduino CLI and clangd with the following flags in addition to the target board FQBN:
7474

7575
```
7676
./arduino-language-server \

Diff for: main.go

+33-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ import (
77
"net/http"
88
_ "net/http/pprof"
99
"os"
10+
"os/exec"
1011
"os/signal"
12+
"os/user"
13+
"path"
1114

1215
"github.com/arduino/arduino-language-server/ls"
1316
"github.com/arduino/arduino-language-server/streams"
@@ -68,15 +71,41 @@ func main() {
6871
log.SetOutput(os.Stderr)
6972
}
7073

71-
if *cliPath != "" {
74+
if *cliDaemonAddress != "" || *cliDaemonInstanceNumber != -1 {
75+
// if one is set, both must be set
76+
if *cliDaemonAddress == "" || *cliDaemonInstanceNumber == -1 {
77+
log.Fatal("ArduinoCLI daemon address and instance number must be set.")
78+
}
79+
} else {
80+
if *cliConfigPath == "" {
81+
if user, _ := user.Current(); user != nil {
82+
candidate := path.Join(user.HomeDir, ".arduino15/arduino-cli.yaml")
83+
if _, err := os.Stat(candidate); err == nil {
84+
*cliConfigPath = candidate
85+
log.Printf("ArduinoCLI config file found at %s\n", candidate)
86+
}
87+
}
88+
}
7289
if *cliConfigPath == "" {
7390
log.Fatal("Path to ArduinoCLI config file must be set.")
7491
}
75-
} else if *cliDaemonAddress == "" || *cliDaemonInstanceNumber == -1 {
76-
log.Fatal("ArduinoCLI daemon address and instance number must be set.")
92+
if *cliPath == "" {
93+
bin, _ := exec.LookPath("arduino-cli")
94+
if bin == "" {
95+
log.Fatal("Path to ArduinoCLI must be set.")
96+
}
97+
log.Printf("arduino-cli found at %s\n", bin)
98+
*cliPath = bin
99+
}
77100
}
101+
78102
if *clangdPath == "" {
79-
log.Fatal("Path to Clangd must be set.")
103+
bin, _ := exec.LookPath("clangd")
104+
if bin == "" {
105+
log.Fatal("Path to Clangd must be set.")
106+
}
107+
log.Printf("clangd found at %s\n", bin)
108+
*clangdPath = bin
80109
}
81110

82111
config := &ls.Config{

0 commit comments

Comments
 (0)