@@ -18,6 +18,7 @@ package debug
18
18
import (
19
19
"context"
20
20
"encoding/json"
21
+ "errors"
21
22
"slices"
22
23
"strconv"
23
24
"strings"
@@ -41,25 +42,66 @@ func GetDebugConfig(ctx context.Context, req *rpc.GetDebugConfigRequest) (*rpc.G
41
42
return nil , & arduino.InvalidInstanceError {}
42
43
}
43
44
defer release ()
44
- return getDebugProperties (req , pme )
45
+ return getDebugProperties (req , pme , false )
45
46
}
46
47
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
52
67
}
53
- sketchPath := paths .New (req .GetSketchPath ())
54
- sk , err := sketch .New (sketchPath )
55
68
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" )
57
99
}
58
100
59
101
// XXX Remove this code duplication!!
60
102
fqbnIn := req .GetFqbn ()
61
- if fqbnIn == "" && sk != nil {
62
- fqbnIn = sk . GetDefaultFQBN ()
103
+ if fqbnIn == "" {
104
+ fqbnIn = sketchDefaultFQBN
63
105
}
64
106
if fqbnIn == "" {
65
107
return nil , & arduino.MissingFQBNError {}
@@ -109,16 +151,18 @@ func getDebugProperties(req *rpc.GetDebugConfigRequest, pme *packagemanager.Expl
109
151
if importDir := req .GetImportDir (); importDir != "" {
110
152
importPath = paths .New (importDir )
111
153
} else {
112
- importPath = sk . DefaultBuildPath ()
154
+ importPath = sketchDefaultBuildPath
113
155
}
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
+ }
119
163
}
120
164
toolProperties .SetPath ("build.path" , importPath )
121
- toolProperties .Set ("build.project_name" , sk . Name + ".ino" )
165
+ toolProperties .Set ("build.project_name" , sketchName + ".ino" )
122
166
123
167
// Set debug port property
124
168
port := req .GetPort ()
0 commit comments