Skip to content

Commit f5c0877

Browse files
committed
Implementation of 'debug check' command
1 parent 867e6c9 commit f5c0877

File tree

3 files changed

+69
-19
lines changed

3 files changed

+69
-19
lines changed

commands/daemon/debug.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ func (s *ArduinoCoreServerImpl) GetDebugConfig(ctx context.Context, req *rpc.Get
6666
res, err := cmd.GetDebugConfig(ctx, req)
6767
return res, convertErrorToRPCStatus(err)
6868
}
69+
70+
// IsDebugSupported checks if debugging is supported for a given configuration
71+
func (s *ArduinoCoreServerImpl) IsDebugSupported(ctx context.Context, req *rpc.IsDebugSupportedRequest) (*rpc.IsDebugSupportedResponse, error) {
72+
res, err := cmd.IsDebugSupported(ctx, req)
73+
return res, convertErrorToRPCStatus(err)
74+
}

commands/debug/debug.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func Debug(ctx context.Context, req *rpc.GetDebugConfigRequest, inStream io.Read
118118

119119
// getCommandLine compose a debug command represented by a core recipe
120120
func getCommandLine(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer) ([]string, error) {
121-
debugInfo, err := getDebugProperties(req, pme)
121+
debugInfo, err := getDebugProperties(req, pme, false)
122122
if err != nil {
123123
return nil, err
124124
}

commands/debug/debug_info.go

Lines changed: 62 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package debug
1818
import (
1919
"context"
2020
"encoding/json"
21+
"errors"
2122
"slices"
2223
"strconv"
2324
"strings"
@@ -41,25 +42,66 @@ func GetDebugConfig(ctx context.Context, req *rpc.GetDebugConfigRequest) (*rpc.G
4142
return nil, &arduino.InvalidInstanceError{}
4243
}
4344
defer release()
44-
return getDebugProperties(req, pme)
45+
return getDebugProperties(req, pme, false)
4546
}
4647

47-
func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer) (*rpc.GetDebugConfigResponse, error) {
48-
// TODO: make a generic function to extract sketch from request
49-
// and remove duplication in commands/compile.go
50-
if req.GetSketchPath() == "" {
51-
return nil, &arduino.MissingSketchPathError{}
48+
// IsDebugSupported checks if the given board/programmer configuration supports debugging.
49+
func IsDebugSupported(ctx context.Context, req *rpc.IsDebugSupportedRequest) (*rpc.IsDebugSupportedResponse, error) {
50+
pme, release := instances.GetPackageManagerExplorer(req.GetInstance())
51+
if pme == nil {
52+
return nil, &arduino.InvalidInstanceError{}
53+
}
54+
defer release()
55+
_, err := getDebugProperties(&rpc.GetDebugConfigRequest{
56+
Instance: req.GetInstance(),
57+
Fqbn: req.GetFqbn(),
58+
SketchPath: "",
59+
Port: req.GetPort(),
60+
Interpreter: req.GetInterpreter(),
61+
ImportDir: "",
62+
Programmer: req.GetProgrammer(),
63+
}, pme, true)
64+
var x *arduino.FailedDebugError
65+
if errors.As(err, &x) {
66+
return &rpc.IsDebugSupportedResponse{Supported: false}, nil
5267
}
53-
sketchPath := paths.New(req.GetSketchPath())
54-
sk, err := sketch.New(sketchPath)
5568
if err != nil {
56-
return nil, &arduino.CantOpenSketchError{Cause: err}
69+
return nil, err
70+
}
71+
return &rpc.IsDebugSupportedResponse{Supported: true}, nil
72+
}
73+
74+
func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Explorer, skipSketchChecks bool) (*rpc.GetDebugConfigResponse, error) {
75+
var (
76+
sketchName string
77+
sketchDefaultFQBN string
78+
sketchDefaultBuildPath *paths.Path
79+
)
80+
if !skipSketchChecks {
81+
// TODO: make a generic function to extract sketch from request
82+
// and remove duplication in commands/compile.go
83+
if req.GetSketchPath() == "" {
84+
return nil, &arduino.MissingSketchPathError{}
85+
}
86+
sketchPath := paths.New(req.GetSketchPath())
87+
sk, err := sketch.New(sketchPath)
88+
if err != nil {
89+
return nil, &arduino.CantOpenSketchError{Cause: err}
90+
}
91+
sketchName = sk.Name
92+
sketchDefaultFQBN = sk.GetDefaultFQBN()
93+
sketchDefaultBuildPath = sk.DefaultBuildPath()
94+
} else {
95+
// Use placeholder sketch data
96+
sketchName = "Sketch"
97+
sketchDefaultFQBN = ""
98+
sketchDefaultBuildPath = paths.New("SketchBuildPath")
5799
}
58100

59101
// XXX Remove this code duplication!!
60102
fqbnIn := req.GetFqbn()
61-
if fqbnIn == "" && sk != nil {
62-
fqbnIn = sk.GetDefaultFQBN()
103+
if fqbnIn == "" {
104+
fqbnIn = sketchDefaultFQBN
63105
}
64106
if fqbnIn == "" {
65107
return nil, &arduino.MissingFQBNError{}
@@ -109,16 +151,18 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
109151
if importDir := req.GetImportDir(); importDir != "" {
110152
importPath = paths.New(importDir)
111153
} else {
112-
importPath = sk.DefaultBuildPath()
154+
importPath = sketchDefaultBuildPath
113155
}
114-
if !importPath.Exist() {
115-
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
116-
}
117-
if !importPath.IsDir() {
118-
return nil, &arduino.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
156+
if !skipSketchChecks {
157+
if !importPath.Exist() {
158+
return nil, &arduino.NotFoundError{Message: tr("Compiled sketch not found in %s", importPath)}
159+
}
160+
if !importPath.IsDir() {
161+
return nil, &arduino.NotFoundError{Message: tr("Expected compiled sketch in directory %s, but is a file instead", importPath)}
162+
}
119163
}
120164
toolProperties.SetPath("build.path", importPath)
121-
toolProperties.Set("build.project_name", sk.Name+".ino")
165+
toolProperties.Set("build.project_name", sketchName+".ino")
122166

123167
// Set debug port property
124168
port := req.GetPort()

0 commit comments

Comments
 (0)