@@ -120,17 +120,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
120
120
appendBuildProperties (r , builderCtx )
121
121
}()
122
122
r = & rpc.CompileResponse {}
123
- if newSketchErr != nil {
124
- if req .GetShowProperties () {
125
- // Just get build properties and exit
126
- compileErr := builder .RunParseHardware (builderCtx )
127
- if compileErr != nil {
128
- compileErr = & arduino.CompileFailedError {Message : compileErr .Error ()}
129
- }
130
- return r , compileErr
131
- }
132
- return nil , & arduino.CantOpenSketchError {Cause : err }
133
- }
123
+
134
124
builderCtx .Sketch = sk
135
125
builderCtx .ProgressCB = progressCB
136
126
@@ -142,30 +132,11 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
142
132
builderCtx .OtherLibrariesDirs = paths .NewPathList (req .GetLibraries ()... )
143
133
builderCtx .OtherLibrariesDirs .Add (configuration .LibrariesDir (configuration .Settings ))
144
134
builderCtx .LibraryDirs = paths .NewPathList (req .Library ... )
145
- if req .GetBuildPath () == "" {
146
- builderCtx .BuildPath = sk .DefaultBuildPath ()
147
- } else {
148
- builderCtx .BuildPath = paths .New (req .GetBuildPath ()).Canonical ()
149
- if in , err := builderCtx .BuildPath .IsInsideDir (sk .FullPath ); err != nil {
150
- return nil , & arduino.NotFoundError {Message : tr ("Cannot find build path" ), Cause : err }
151
- } else if in && builderCtx .BuildPath .IsDir () {
152
- if sk .AdditionalFiles , err = removeBuildFromSketchFiles (sk .AdditionalFiles , builderCtx .BuildPath ); err != nil {
153
- return nil , err
154
- }
155
- }
156
- }
157
- if err = builderCtx .BuildPath .MkdirAll (); err != nil {
158
- return nil , & arduino.PermissionDeniedError {Message : tr ("Cannot create build directory" ), Cause : err }
135
+ err = prepareBuildPath (sk , req , builderCtx , err )
136
+ if err != nil {
137
+ return r , err
159
138
}
160
139
161
- buildcache .New (builderCtx .BuildPath .Parent ()).GetOrCreate (builderCtx .BuildPath .Base ())
162
- // cache is purged after compilation to not remove entries that might be required
163
- defer maybePurgeBuildCache ()
164
-
165
- builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
166
- builderCtx .BuildPath .Join ("compile_commands.json" ),
167
- )
168
-
169
140
builderCtx .Verbose = req .GetVerbose ()
170
141
171
142
// Optimize for debug
@@ -221,6 +192,15 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
221
192
return r , compileErr
222
193
}
223
194
195
+ if newSketchErr != nil {
196
+ // newSketchErr causes to exit only here since the request could have
197
+ // been to only show properties until now
198
+ return r , & arduino.CantOpenSketchError {Cause : err }
199
+ }
200
+
201
+ // cache is purged after compilation to not remove entries that might be required
202
+ defer maybePurgeBuildCache ()
203
+
224
204
if req .GetPreprocess () {
225
205
// Just output preprocessed source code and exit
226
206
compileErr := builder .RunPreprocess (builderCtx )
@@ -300,6 +280,35 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
300
280
return r , nil
301
281
}
302
282
283
+ func prepareBuildPath (sk * sketch.Sketch , req * rpc.CompileRequest , builderCtx * types.Context , err error ) error {
284
+ if sk == nil {
285
+ return nil
286
+ }
287
+
288
+ if req .GetBuildPath () == "" {
289
+ builderCtx .BuildPath = sk .DefaultBuildPath ()
290
+ } else {
291
+ builderCtx .BuildPath = paths .New (req .GetBuildPath ()).Canonical ()
292
+ if in , err := builderCtx .BuildPath .IsInsideDir (sk .FullPath ); err != nil {
293
+ return & arduino.NotFoundError {Message : tr ("Cannot find build path" ), Cause : err }
294
+ } else if in && builderCtx .BuildPath .IsDir () {
295
+ if sk .AdditionalFiles , err = removeBuildFromSketchFiles (sk .AdditionalFiles , builderCtx .BuildPath ); err != nil {
296
+ return err
297
+ }
298
+ }
299
+ }
300
+
301
+ if err = builderCtx .BuildPath .MkdirAll (); err != nil {
302
+ return & arduino.PermissionDeniedError {Message : tr ("Cannot create build directory" ), Cause : err }
303
+ }
304
+
305
+ builderCtx .CompilationDatabase = bldr .NewCompilationDatabase (
306
+ builderCtx .BuildPath .Join ("compile_commands.json" ),
307
+ )
308
+ buildcache .New (builderCtx .BuildPath .Parent ()).GetOrCreate (builderCtx .BuildPath .Base ())
309
+ return nil
310
+ }
311
+
303
312
func appendUserLibraries (r * rpc.CompileResponse , builderCtx * types.Context , errStream io.Writer ) {
304
313
importedLibs := []* rpc.Library {}
305
314
for _ , lib := range builderCtx .ImportedLibraries {
0 commit comments