Skip to content

Commit 1aa53c8

Browse files
authored
Always output build path in compile json-output / Always write a valid compilation database (#1156)
* Always report buildpath (even in case of build failed) * Always write a correct compilation database Even if the Contents field is empty.
1 parent 37e04d7 commit 1aa53c8

File tree

2 files changed

+18
-14
lines changed

2 files changed

+18
-14
lines changed

arduino/builder/compilation_database.go

+3-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ type CompilationCommand struct {
4141
// NewCompilationDatabase creates an empty CompilationDatabase
4242
func NewCompilationDatabase(filename *paths.Path) *CompilationDatabase {
4343
return &CompilationDatabase{
44-
File: filename,
44+
File: filename,
45+
Contents: []CompilationCommand{},
4546
}
4647
}
4748

@@ -51,10 +52,7 @@ func LoadCompilationDatabase(file *paths.Path) (*CompilationDatabase, error) {
5152
if err != nil {
5253
return nil, err
5354
}
54-
res := &CompilationDatabase{
55-
File: file,
56-
Contents: []CompilationCommand{},
57-
}
55+
res := NewCompilationDatabase(file)
5856
return res, json.Unmarshal(f, &res.Contents)
5957
}
6058

commands/compile/compile.go

+15-9
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,23 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
194194

195195
builderCtx.SourceOverride = req.GetSourceOverride()
196196

197+
r = &rpc.CompileResp{}
198+
defer func() {
199+
if p := builderCtx.BuildPath; p != nil {
200+
r.BuildPath = p.String()
201+
}
202+
}()
203+
197204
// if --preprocess or --show-properties were passed, we can stop here
198205
if req.GetShowProperties() {
199-
return &rpc.CompileResp{}, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
206+
return r, builder.RunParseHardwareAndDumpBuildProperties(builderCtx)
200207
} else if req.GetPreprocess() {
201-
return &rpc.CompileResp{}, builder.RunPreprocess(builderCtx)
208+
return r, builder.RunPreprocess(builderCtx)
202209
}
203210

204211
// if it's a regular build, go on...
205212
if err := builder.RunBuilder(builderCtx); err != nil {
206-
return &rpc.CompileResp{}, err
213+
return r, err
207214
}
208215

209216
// If the export directory is set we assume you want to export the binaries
@@ -219,17 +226,17 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
219226
}
220227
logrus.WithField("path", exportPath).Trace("Saving sketch to export path.")
221228
if err := exportPath.MkdirAll(); err != nil {
222-
return nil, errors.Wrap(err, "creating output dir")
229+
return r, errors.Wrap(err, "creating output dir")
223230
}
224231

225232
// Copy all "sketch.ino.*" artifacts to the export directory
226233
baseName, ok := builderCtx.BuildProperties.GetOk("build.project_name") // == "sketch.ino"
227234
if !ok {
228-
return nil, errors.New("missing 'build.project_name' build property")
235+
return r, errors.New("missing 'build.project_name' build property")
229236
}
230237
buildFiles, err := builderCtx.BuildPath.ReadDir()
231238
if err != nil {
232-
return nil, errors.Errorf("reading build directory: %s", err)
239+
return r, errors.Errorf("reading build directory: %s", err)
233240
}
234241
buildFiles.FilterPrefix(baseName)
235242
for _, buildFile := range buildFiles {
@@ -239,7 +246,7 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
239246
WithField("dest", exportedFile).
240247
Trace("Copying artifact.")
241248
if err = buildFile.CopyTo(exportedFile); err != nil {
242-
return nil, errors.Wrapf(err, "copying output file %s", buildFile)
249+
return r, errors.Wrapf(err, "copying output file %s", buildFile)
243250
}
244251
}
245252
}
@@ -248,15 +255,14 @@ func Compile(ctx context.Context, req *rpc.CompileReq, outStream, errStream io.W
248255
for _, lib := range builderCtx.ImportedLibraries {
249256
rpcLib, err := lib.ToRPCLibrary()
250257
if err != nil {
251-
return nil, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
258+
return r, fmt.Errorf("converting library %s to rpc struct: %w", lib.Name, err)
252259
}
253260
importedLibs = append(importedLibs, rpcLib)
254261
}
255262

256263
logrus.Tracef("Compile %s for %s successful", sketch.Name, fqbnIn)
257264

258265
return &rpc.CompileResp{
259-
BuildPath: builderCtx.BuildPath.String(),
260266
UsedLibraries: importedLibs,
261267
ExecutableSectionsSize: builderCtx.ExecutableSectionsSize.ToRPCExecutableSectionSizeArray(),
262268
}, nil

0 commit comments

Comments
 (0)