Skip to content

Commit da60b75

Browse files
committed
Print compile error and suggestions as part of the result
1 parent 3f62806 commit da60b75

File tree

3 files changed

+27
-18
lines changed

3 files changed

+27
-18
lines changed

internal/cli/compile/compile.go

+15-12
Original file line numberDiff line numberDiff line change
@@ -305,15 +305,16 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
305305
}
306306

307307
stdIO := stdIORes()
308-
feedback.PrintResult(&compileResult{
308+
res := &compileResult{
309309
CompilerOut: stdIO.Stdout,
310310
CompilerErr: stdIO.Stderr,
311311
BuilderResult: compileRes,
312312
ProfileOut: profileOut,
313313
Success: compileError == nil,
314-
})
314+
}
315+
315316
if compileError != nil {
316-
msg := tr("Error during build: %v", compileError)
317+
res.Error = tr("Error during build: %v", compileError)
317318

318319
// Check the error type to give the user better feedback on how
319320
// to resolve it
@@ -333,17 +334,18 @@ func runCompileCommand(cmd *cobra.Command, args []string) {
333334
release()
334335

335336
if profileArg.String() == "" {
336-
msg += "\n"
337+
res.Error += fmt.Sprintln()
337338
if platform != nil {
338339
suggestion := fmt.Sprintf("`%s core install %s`", version.VersionInfo.Application, platformErr.Platform)
339-
msg += tr("Try running %s", suggestion)
340+
res.Error += tr("Try running %s", suggestion)
340341
} else {
341-
msg += tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)
342+
res.Error += tr("Platform %s is not found in any known index\nMaybe you need to add a 3rd party URL?", platformErr.Platform)
342343
}
343344
}
344345
}
345-
feedback.Fatal(msg, feedback.ErrGeneric)
346+
feedback.FatalResult(res, feedback.ErrGeneric)
346347
}
348+
feedback.PrintResult(res)
347349
}
348350

349351
type compileResult struct {
@@ -352,6 +354,7 @@ type compileResult struct {
352354
BuilderResult *rpc.CompileResponse `json:"builder_result"`
353355
Success bool `json:"success"`
354356
ProfileOut string `json:"profile_out,omitempty"`
357+
Error string `json:"error,omitempty"`
355358
}
356359

357360
func (r *compileResult) Data() interface{} {
@@ -365,8 +368,8 @@ func (r *compileResult) String() string {
365368
build := r.BuilderResult
366369

367370
res := "\n"
368-
libraries := table.New()
369371
if len(build.GetUsedLibraries()) > 0 {
372+
libraries := table.New()
370373
libraries.SetHeader(
371374
table.NewCell(tr("Used library"), titleColor),
372375
table.NewCell(tr("Version"), titleColor),
@@ -377,8 +380,8 @@ func (r *compileResult) String() string {
377380
l.GetVersion(),
378381
table.NewCell(l.GetInstallDir(), pathColor))
379382
}
383+
res += fmt.Sprintln(libraries.Render())
380384
}
381-
res += libraries.Render() + "\n"
382385

383386
if boardPlatform := build.GetBoardPlatform(); boardPlatform != nil {
384387
platforms := table.New()
@@ -398,10 +401,10 @@ func (r *compileResult) String() string {
398401
buildPlatform.GetVersion(),
399402
table.NewCell(buildPlatform.GetInstallDir(), pathColor))
400403
}
401-
res += platforms.Render()
404+
res += fmt.Sprintln(platforms.Render())
402405
}
403406
if r.ProfileOut != "" {
404-
res += "\n" + fmt.Sprintln(r.ProfileOut)
407+
res += fmt.Sprintln(r.ProfileOut)
405408
}
406-
return res
409+
return strings.TrimRight(res, fmt.Sprintln())
407410
}

internal/cli/feedback/feedback.go

+6
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ func FatalError(err error, exitCode ExitCode) {
168168
Fatal(err.Error(), exitCode)
169169
}
170170

171+
// FatalResult outputs the result and exits with status exitCode.
172+
func FatalResult(res Result, exitCode ExitCode) {
173+
PrintResult(res)
174+
os.Exit(int(exitCode))
175+
}
176+
171177
// Fatal outputs the errorMsg and exits with status exitCode.
172178
func Fatal(errorMsg string, exitCode ExitCode) {
173179
if format == Text {

internal/integrationtest/compile_2/compile_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -421,16 +421,16 @@ func TestCompileNonInstalledPlatformWithWrongPackagerAndArch(t *testing.T) {
421421
require.NoError(t, err)
422422

423423
// Compile with wrong packager
424-
_, stderr, err := cli.Run("compile", "-b", "wrong:avr:uno", sketchPath.String())
424+
stdout, stderr, err := cli.Run("compile", "-b", "wrong:avr:uno", sketchPath.String())
425425
require.Error(t, err)
426426
require.Contains(t, string(stderr), "Error during build: Platform 'wrong:avr' not found: platform not installed")
427-
require.Contains(t, string(stderr), "Platform wrong:avr is not found in any known index")
427+
require.Contains(t, string(stdout), "Platform wrong:avr is not found in any known index")
428428

429429
// Compile with wrong arch
430-
_, stderr, err = cli.Run("compile", "-b", "arduino:wrong:uno", sketchPath.String())
430+
stdout, stderr, err = cli.Run("compile", "-b", "arduino:wrong:uno", sketchPath.String())
431431
require.Error(t, err)
432432
require.Contains(t, string(stderr), "Error during build: Platform 'arduino:wrong' not found: platform not installed")
433-
require.Contains(t, string(stderr), "Platform arduino:wrong is not found in any known index")
433+
require.Contains(t, string(stdout), "Platform arduino:wrong is not found in any known index")
434434
}
435435

436436
func TestCompileWithKnownPlatformNotInstalled(t *testing.T) {
@@ -446,9 +446,9 @@ func TestCompileWithKnownPlatformNotInstalled(t *testing.T) {
446446
require.NoError(t, err)
447447

448448
// Try to compile using a platform found in the index but not installed
449-
_, stderr, err := cli.Run("compile", "-b", "arduino:avr:uno", sketchPath.String())
449+
stdout, stderr, err := cli.Run("compile", "-b", "arduino:avr:uno", sketchPath.String())
450450
require.Error(t, err)
451451
require.Contains(t, string(stderr), "Error during build: Platform 'arduino:avr' not found: platform not installed")
452452
// Verifies command to fix error is shown to user
453-
require.Contains(t, string(stderr), "Try running `arduino-cli core install arduino:avr`")
453+
require.Contains(t, string(stdout), "Try running `arduino-cli core install arduino:avr`")
454454
}

0 commit comments

Comments
 (0)