Skip to content

Commit fcfc8a7

Browse files
committed
Whitelist all .arduino15/packages dir for clang driver detection
This is more simple and realiable than trying to figure out compilers from compilation database (that may also be empty on the first run...).
1 parent 23b5a34 commit fcfc8a7

File tree

1 file changed

+43
-6
lines changed

1 file changed

+43
-6
lines changed

Diff for: handler/handler.go

+43-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package handler
22

33
import (
4+
"bytes"
45
"context"
56
"encoding/json"
67
"fmt"
@@ -515,7 +516,7 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
515516
return errors.WithMessage(err, "reading generated cpp file from sketch")
516517
}
517518

518-
compilers := examineCompileCommandsJSON(handler.buildPath)
519+
canonicalizeCompileCommandsJSON(handler.buildPath)
519520

520521
if params == nil {
521522
// If we are restarting re-synchronize clangd
@@ -538,7 +539,11 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
538539
}
539540
} else {
540541
// Otherwise start clangd!
541-
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp, compilers)
542+
dataFolder, err := extractDataFolderFromArduinoCLI()
543+
if err != nil {
544+
log.Printf(" error: %s", err)
545+
}
546+
clangdStdout, clangdStdin, clangdStderr := startClangd(handler.buildPath, handler.buildSketchCpp, dataFolder)
542547
clangdStdio := streams.NewReadWriteCloser(clangdStdin, clangdStdout)
543548
if enableLogging {
544549
clangdStdio = streams.LogReadWriteCloserAs(clangdStdio, "inols-clangd.log")
@@ -572,6 +577,38 @@ func (handler *InoHandler) initializeWorkbench(ctx context.Context, params *lsp.
572577
return nil
573578
}
574579

580+
func extractDataFolderFromArduinoCLI() (*paths.Path, error) {
581+
// XXX: do this from IDE or via gRPC
582+
args := []string{globalCliPath,
583+
"config",
584+
"dump",
585+
"--format", "json",
586+
}
587+
cmd, err := executils.NewProcess(args...)
588+
if err != nil {
589+
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
590+
}
591+
cmdOutput := &bytes.Buffer{}
592+
cmd.RedirectStdoutTo(cmdOutput)
593+
log.Println("running: ", strings.Join(args, " "))
594+
if err := cmd.Run(); err != nil {
595+
return nil, errors.Errorf("running %s: %s", strings.Join(args, " "), err)
596+
}
597+
598+
type cmdRes struct {
599+
Directories struct {
600+
Data string `json:"data"`
601+
} `json:"directories"`
602+
}
603+
var res cmdRes
604+
if err := json.Unmarshal(cmdOutput.Bytes(), &res); err != nil {
605+
return nil, errors.Errorf("parsing arduino-cli output: %s", err)
606+
}
607+
// Return only the build path
608+
log.Println("Arduino Data Dir -> ", res.Directories.Data)
609+
return paths.New(res.Directories.Data), nil
610+
}
611+
575612
func (handler *InoHandler) refreshCppDocumentSymbols(prefix string) error {
576613
// Query source code symbols
577614
cppURI := lsp.NewDocumentURIFromPath(handler.buildSketchCpp)
@@ -668,7 +705,7 @@ func (handler *InoHandler) CheckCppIncludesChanges() {
668705
}
669706
}
670707

671-
func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
708+
func canonicalizeCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool {
672709
// Open compile_commands.json and find the main cross-compiler executable
673710
compileCommandsJSONPath := compileCommandsDir.Join("compile_commands.json")
674711
compileCommands, err := builder.LoadCompilationDatabase(compileCommandsJSONPath)
@@ -698,15 +735,15 @@ func examineCompileCommandsJSON(compileCommandsDir *paths.Path) map[string]bool
698735
return compilers
699736
}
700737

701-
func startClangd(compileCommandsDir, sketchCpp *paths.Path, compilers map[string]bool) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
738+
func startClangd(compileCommandsDir, sketchCpp *paths.Path, dataFolder *paths.Path) (io.WriteCloser, io.ReadCloser, io.ReadCloser) {
702739
// Start clangd
703740
args := []string{
704741
globalClangdPath,
705742
"-log=verbose",
706743
fmt.Sprintf(`--compile-commands-dir=%s`, compileCommandsDir),
707744
}
708-
for compiler := range compilers {
709-
args = append(args, fmt.Sprintf("-query-driver=%s", compiler))
745+
if dataFolder != nil {
746+
args = append(args, fmt.Sprintf("-query-driver=%s", dataFolder.Join("packages", "**")))
710747
}
711748
if enableLogging {
712749
log.Println(" Starting clangd:", strings.Join(args, " "))

0 commit comments

Comments
 (0)