@@ -120,17 +120,7 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
120120 appendBuildProperties (r , builderCtx )
121121 }()
122122 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+
134124 builderCtx .Sketch = sk
135125 builderCtx .ProgressCB = progressCB
136126
@@ -142,30 +132,11 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
142132 builderCtx .OtherLibrariesDirs = paths .NewPathList (req .GetLibraries ()... )
143133 builderCtx .OtherLibrariesDirs .Add (configuration .LibrariesDir (configuration .Settings ))
144134 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 .GetBuildPath (), builderCtx )
136+ if err != nil {
137+ return r , err
159138 }
160139
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-
169140 builderCtx .Verbose = req .GetVerbose ()
170141
171142 // Optimize for debug
@@ -221,6 +192,15 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
221192 return r , compileErr
222193 }
223194
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+
224204 if req .GetPreprocess () {
225205 // Just output preprocessed source code and exit
226206 compileErr := builder .RunPreprocess (builderCtx )
@@ -300,6 +280,35 @@ func Compile(ctx context.Context, req *rpc.CompileRequest, outStream, errStream
300280 return r , nil
301281}
302282
283+ func prepareBuildPath (sk * sketch.Sketch , requestedBuildPath string , builderCtx * types.Context ) error {
284+ if sk == nil {
285+ return nil
286+ }
287+
288+ if requestedBuildPath == "" {
289+ builderCtx .BuildPath = sk .DefaultBuildPath ()
290+ } else {
291+ builderCtx .BuildPath = paths .New (requestedBuildPath ).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+
303312func appendUserLibraries (r * rpc.CompileResponse , builderCtx * types.Context , errStream io.Writer ) {
304313 importedLibs := []* rpc.Library {}
305314 for _ , lib := range builderCtx .ImportedLibraries {
0 commit comments